视频图像处理系列索引 || Arcgis/Engine/Server开发索引 || Web Map Gis开发索引 || jquery表格组件 JQGrid索引
WPF MVVM模式开发实现简明教程索引 || ArcGIS Runtime WPF(.net C#)开发简明教程索引

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();

        }

 

posted @ 2016-04-26 09:40  jhlong  阅读(12852)  评论(0编辑  收藏  举报
海龙的博客 jhlong@cnblogs 版权所有© 转载请注明链接.有用请推荐一下
代码全部经过本人测试,但不保证复制粘贴就正常运行,更不保证能解决你的问题,请结合前后代码及描述理解后修改和使用