热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#控件自由拖动、边角拖拉缩放

效果就是在一幅图上画一个白框,可移动可缩放usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentMode

效果就是在一幅图上画一个白框,可移动可缩放 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu;
using Emgu.CV;
using Emgu.Util;
using Emgu.CV.Structure;
namespace huitugongju
{
public partial class Form1 : Form
{
Point start; //画框的起始点
Point end;//画框的结束点
bool blnDraw;//判断是否绘制
Rectangle rect;
List roi_location = new List();
List roi_size = new List();
MouseDirection direction;//指针形状
bool size_change;//画框大小改变
bool position_change;//画框位置改变
Point rectangle_start;//画框内的起始点
Point rectangle_end;//画框内结束点
public enum MouseDirection
{
East,
West,
South,
North,
Southeast,
Southwest,
Northeast,
Northwest,
None
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Mat src = CvInvoke.Imread("dog.jpg");
this.Width = src.Width;
this.Height = src.Height;
this.BackgroundImage = src.Bitmap;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
try
{
var p1 = this.Controls.Find("panel1", true);
if (p1[0].Size.Width !=0 || p1[0].Size.Height !=0)
{
roi_location.Add(p1[0].Location);
roi_size.Add(p1[0].Size);
}
this.Controls.Clear();
}
catch (Exception)
{
}
finally
{
Panel p = new Panel();
p.Visible = false;
p.Name = "panel1";
p.BackColor = Color.White;
this.Controls.Add(p);
start = e.Location;
blnDraw = true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (blnDraw)
{
if (e.Button != MouseButtons.Left)//判断是否按下左键
return;
Point tempEndPoint = e.Location; //记录框的位置和大小
//var p = this.Controls.Find("panel1", true);
rect.Location = new Point(
Math.Min(start.X, tempEndPoint.X),
Math.Min(start.Y, tempEndPoint.Y));
rect.Size = new Size(Math.Abs(start.X - tempEndPoint.X),Math.Abs(start.Y - tempEndPoint.Y));
this.Invalidate();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (blnDraw)
{
this.Invalidate();
var p = this.Controls.Find("panel1", true);
p[0].BackColor = Color.White;
p[0].Location = start;
p[0].Size = rect.Size;
p[0].Visible = true;
p[0].MouseDown += new MouseEventHandler(this.panel_MouseDown);
p[0].MouseMove += new MouseEventHandler(this.panel_MouseMove);
}
blnDraw = false; //结束绘制
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (blnDraw)
{
if (this.BackgroundImage != null)
{
if (rect != null && rect.Width > 0 && rect.Height > 0)
{
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), rect);//重新绘制颜色为红色
}
}
}
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
rectangle_start = e.Location;
}
private void panel_MouseMove(object sender, MouseEventArgs e)
{
var p = this.Controls.Find("panel1", true);
Point tempEndPoint = e.Location;
if (e.Button == MouseButtons.Left)
{
if (direction == MouseDirection.None)
{
int x = tempEndPoint.X - rectangle_start.X;
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y + y);
}
else if (direction == MouseDirection.East)
{
p[0].Width = tempEndPoint.X;
}
else if (direction == MouseDirection.South)
{
p[0].Height = tempEndPoint.Y;
}
else if (direction == MouseDirection.West)
{
int x = tempEndPoint.X - rectangle_start.X;
p[0].Width = p[0].Width - x;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y);
}
else if (direction == MouseDirection.North)
{
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Height = p[0].Height - y;
p[0].Location = new Point(p[0].Location.X, p[0].Location.Y + y);
}
else if (direction == MouseDirection.Southeast)
{
p[0].Width = tempEndPoint.X;
p[0].Height = tempEndPoint.Y;
}
else if (direction == MouseDirection.Northeast)
{
p[0].Width = tempEndPoint.X;
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Height = p[0].Height - y;
p[0].Location = new Point(p[0].Location.X, p[0].Location.Y + y);
}
else if (direction == MouseDirection.Southwest)
{
p[0].Height = tempEndPoint.Y;
int x = tempEndPoint.X - rectangle_start.X;
p[0].Width = p[0].Width - x;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y);
}
else if (direction == MouseDirection.Northwest)
{
int x = tempEndPoint.X - rectangle_start.X;
int y = tempEndPoint.Y - rectangle_start.Y;
p[0].Height = p[0].Height - y;
p[0].Width = p[0].Width - x;
p[0].Location = new Point(p[0].Location.X + x, p[0].Location.Y + y);
}
}
else
{
if (e.Location.X <10 && e.Location.Y <10)
{
p[0].Cursor = Cursors.SizeNWSE;
direction = MouseDirection.Northwest;
}
else if (e.Location.X > p[0].Width - 10 && e.Location.Y <10)
{
p[0].Cursor = Cursors.SizeNESW;
direction = MouseDirection.Northeast;
}
else if (e.Location.X > p[0].Width - 10 && e.Location.Y > p[0].Height - 10)
{
p[0].Cursor = Cursors.SizeNWSE;
direction = MouseDirection.Southeast;
}
else if (e.Location.X <10 && e.Location.Y > p[0].Height - 10)
{
p[0].Cursor = Cursors.SizeNESW;
direction = MouseDirection.Southwest;
}
else if (e.Location.X <10 && e.Location.Y 10)
{
p[0].Cursor = Cursors.SizeWE;
direction = MouseDirection.West;
}
else if (e.Location.X > 10 && e.Location.X {
p[0].Cursor = Cursors.SizeNS;
direction = MouseDirection.North;
}
else if (e.Location.X > p[0].Width - 10 && e.Location.Y 10)
{
p[0].Cursor = Cursors.SizeWE;
direction = MouseDirection.East;
}
else if (e.Location.X > 10 && e.Location.X p[0].Height - 10)
{
p[0].Cursor = Cursors.SizeNS;
direction = MouseDirection.South;
}
else
{
p[0].Cursor = Cursors.SizeAll;
direction = MouseDirection.None;
}
}
}
}
}

大概效果这样:



推荐阅读
author-avatar
淘气111006
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有