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

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

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<Point> roi_location = new List<Point>();
        List<Size> roi_size = new List<Size>();
        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 < p[0].Height - 10 && e.Location.Y > 10)
                {
                    p[0].Cursor = Cursors.SizeWE;
                    direction = MouseDirection.West;
                }
                else if (e.Location.X > 10 && e.Location.X < p[0].Width - 10 && e.Location.Y < 10)
                {
                    p[0].Cursor = Cursors.SizeNS;
                    direction = MouseDirection.North;
                }
                else if (e.Location.X > p[0].Width - 10 && e.Location.Y < p[0].Height - 10 && e.Location.Y > 10)
                {
                    p[0].Cursor = Cursors.SizeWE;
                    direction = MouseDirection.East;
                }
                else if (e.Location.X > 10 && e.Location.X < p[0].Width - 10 && e.Location.Y > p[0].Height - 10)
                {
                    p[0].Cursor = Cursors.SizeNS;
                    direction = MouseDirection.South;
                }
                else
                {
                    p[0].Cursor = Cursors.SizeAll;
                    direction = MouseDirection.None;
                }
            }

        }
    }
}

大概效果这样:

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值