天天看點

c# winform 動态畫矩形 矩形大小可以拖動

http://jhlong12345.blog.163.com/blog/static/1230631292015544450189/# 

結合上一篇,繼續

矩形大小的調整

還有小bug,思路有了,就行了,就不貼修改正常的代碼了,自己動動手,有思路基本都可以很快寫出來的

看完還寫不出來的建議另尋他法吧

不要想着複制粘貼就正常運作,也不一定能解決你的問題

思路:

畫一個矩形,然後在mousedown時,判斷滑鼠是否在區域内,如果在,就在四個角畫四個小正方形,然後在mousemove中判斷是否在四個方格中,如果在,就出現箭頭滑鼠,然後做個标記,表示開始調整尺寸

直接看代碼吧,有點複雜了

//是否開始畫了

        bool bDrawStart = false;

        //矩形的起始點

        Point pointStart = Point.Empty;

        //矩形的起始點的對角點,滑鼠移動點

        Point pointContinue = Point.Empty;

        //是否要修改矩形,以滑鼠點選點在矩形内部為準,修改時自動在4個端點建立4個小方框,不能建立其他的矩形

        bool bChange = false;

 

        //修改矩形時移動點的X、Y記錄,好根據它在dicPoints找到對應的矩形,或dicPointsChange找到對應小方框

        Point pMove1 = Point.Empty;

        Point pMove2 = Point.Empty;

        //修改矩形時滑鼠點選進4個小方框内,确認要根據滑鼠移動修改

        bool bChangeMove = false;

        //目前全部建立的矩形的記錄

        Dictionary<Point, Point> dicPoints = new Dictionary<Point, Point>();

        //矩形要修改大小時4個端點建立4個小方框的記錄

        Dictionary<Point, Point> dicPointsChange = new Dictionary<Point, Point>();

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

        {

            if (bDrawStart)

            {

                bDrawStart = false;

            }

            else

            {

                bDrawStart = true;

                pointStart = e.Location;

            }

 

            if (bChange)

            {

                bDrawStart = false;

                foreach (var item in dicPointsChange)

                {

                    Point p1 = item.Key;

                    Point p2 = item.Value;

 

                    //如果在4個小框裡

                    if ((e.Location.X > p1.X && e.Location.X < p2.X && e.Location.Y > p1.Y && e.Location.Y < p2.Y) ||

                        (e.Location.X > p2.X && e.Location.X < p1.X && e.Location.Y > p2.Y && e.Location.Y < p1.Y) ||

                        (e.Location.X > p2.X && e.Location.X < p1.X && e.Location.Y > p1.Y && e.Location.Y < p2.Y) ||

                        (e.Location.X > p1.X && e.Location.X < p2.X && e.Location.Y > p2.Y && e.Location.Y < p1.Y))

                    {

                        bChangeMove = true;

                       

                        //p1、p2為小方框的對角點,中心點為矩形的端點,首先擷取端點,

                        //然後根據pMove1,pMove2确定小方框在的端點是矩形的哪個端點

                        //最後确定起始點,起始點pointStart為移動點(滑鼠點選點、小方框在的端點)的對角點

                        //pointContinue為移動點(滑鼠點選點、小方框在的端點)

                        Point p = Point.Empty;

                        p.X = p1.X > p2.X ? p1.X - 5 : p1.X + 5;

                        p.Y = p1.Y > p2.Y ? p1.Y - 5 : p1.Y + 5;

 

                        if (p == pMove1)

                        {

                            pointStart = pMove2;

                        }

                        else

                            if (p == pMove2)

                            {

                                pointStart = pMove1;

                            }

                            else

                                if (p == new Point(pMove1.X, pMove2.Y))

                                {

                                    pointStart = new Point(pMove2.X, pMove1.Y);

                                }

                                else

                                    if (p == new Point(pMove2.X, pMove1.Y))

                                    {

                                        pointStart = new Point(pMove1.X, pMove2.Y);

                                    }

                        

                        break;

                    }

                }

 

                //如果沒點選4個小方框,則預設放棄修改

                if (!bChangeMove)

                {

                    bDrawStart = true;

                    dicPointsChange.Clear();

                    bChange = false;

                }

            }

            else

            {

                foreach (var item in dicPoints)

                {

                    Point p1 = item.Key;

                    Point p2 = item.Value;

                    

                    if ((e.Location.X > p1.X && e.Location.X < p2.X && e.Location.Y > p1.Y && e.Location.Y < p2.Y) ||

                        (e.Location.X > p2.X && e.Location.X < p1.X && e.Location.Y > p2.Y && e.Location.Y < p1.Y) ||

                        (e.Location.X > p2.X && e.Location.X < p1.X && e.Location.Y > p1.Y && e.Location.Y < p2.Y) ||

                        (e.Location.X > p1.X && e.Location.X < p2.X && e.Location.Y > p2.Y && e.Location.Y < p1.Y))

                    {

                        bChange = true;

                        bDrawStart = false; //不能畫新矩形了

                        dicPointsChange.Clear();

 

                        pMove1 = p1;

                        pMove2 = p2;

 

                        //畫出點選矩形的4個端點的4個小方框,并記錄入dicPointsChange

                        Graphics g = pictureBox1.CreateGraphics();

                        System.Drawing.Pen pen = new System.Drawing.Pen(Color.Red);

                        g.DrawRectangle(pen, p1.X - 5, p1.Y - 5, 10, 10);

                        g.DrawRectangle(pen, p2.X - 5, p2.Y - 5, 10, 10);

                        g.DrawRectangle(pen, p1.X - 5, p2.Y - 5, 10, 10);

                        g.DrawRectangle(pen, p2.X - 5, p1.Y - 5, 10, 10);

                        pen.Dispose();

 

                        dicPointsChange.Add(new Point(p1.X - 5, p1.Y - 5), new Point(p1.X + 5, p1.Y + 5));

                        dicPointsChange.Add(new Point(p2.X - 5, p2.Y - 5), new Point(p2.X + 5, p2.Y + 5));

                        dicPointsChange.Add(new Point(p2.X - 5, p1.Y - 5), new Point(p2.X + 5, p1.Y + 5));

                        dicPointsChange.Add(new Point(p1.X - 5, p2.Y - 5), new Point(p1.X + 5, p2.Y + 5));

                        break;

                    }

                }

            }

        }

 

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

        {

            if (bDrawStart)

            {

                pointContinue = e.Location;

                Refresh();

            }

 

            if (bChangeMove)

            {

                pointContinue = e.Location;

                Point p = Point.Empty;

                foreach (var item in dicPoints)

                {

                    Point p1 = item.Key;

                    Point p2 = item.Value;

                    //找到dicPoints裡的原始記錄

                    if (p1 == pointStart || p2 == pointStart || new Point(p1.X, p2.Y) == pointStart || new Point(p2.X, p1.Y) == pointStart)

                    {

                        p = p1;

                        break;

                    }

                }

 

                if (p != Point.Empty)

                {

                    //先删除dicPoints裡的原始記錄,再根據新點建立新的記錄

                    dicPoints.Remove(p);

                    dicPoints.Add(pointStart, pointContinue);

 

                    dicPointsChange.Clear();

 

                    Point p1 = pointStart;

                    Point p2 = pointContinue;

                    //移動過程即時建立4個小方框,看起來小方框和矩形一起變大變小的效果

                    dicPointsChange.Add(new Point(p1.X - 5, p1.Y - 5), new Point(p1.X + 5, p1.Y + 5));

                    dicPointsChange.Add(new Point(p2.X - 5, p2.Y - 5), new Point(p2.X + 5, p2.Y + 5));

                    dicPointsChange.Add(new Point(p2.X - 5, p1.Y - 5), new Point(p2.X + 5, p1.Y + 5));

                    dicPointsChange.Add(new Point(p1.X - 5, p2.Y - 5), new Point(p1.X + 5, p2.Y + 5));

                    Refresh();

                }

            }

        }

 

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)

        {

            if (bDrawStart)

            {   //此時矩形畫完,記錄矩形兩個對角點,在Paint裡畫

                dicPoints.Add(pointStart, pointContinue);

            }

 

            if (bChangeMove)

            {

                bChangeMove = false;

                bChange = false;

            }

 

            bDrawStart = false;

        }

 

        private void pictureBox1_Paint(object sender, PaintEventArgs e)

        {

            System.Drawing.Pen pen = new System.Drawing.Pen(Color.LimeGreen);

            pen.Width = 2;

            if (bDrawStart)

            {

                //實時的畫矩形

                Graphics g = e.Graphics;

                g.DrawRectangle(pen, pointStart.X, pointStart.Y, pointContinue.X - pointStart.X, pointContinue.Y - pointStart.Y);

            }

 

            //實時的畫之前已經畫好的矩形

            foreach (var item in dicPoints)

            {

                Point p1 = item.Key;

                Point p2 = item.Value;

 

                e.Graphics.DrawRectangle(pen, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);

            }

 

            if (bChangeMove)

            {

                pen.Color = Color.Red;

                foreach (var item in dicPointsChange)

                {

                    Point p1 = item.Key;

                    Point p2 = item.Value;

                   

                    e.Graphics.DrawRectangle(pen, p1.X, p1.Y, p2.X - p1.X, p2.Y - p1.Y);

                }

            }

 

            pen.Dispose();

        }