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;
                }
            }

        }
    }
}

大概效果這樣:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章