Exemplo n.º 1
0
        private void BmpForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsLBtnPress == true)                                                                     // 鼠标左键按下时发生的事件
            {
                CopyBmp = (Bitmap)OriginBmp.Clone();
                CopyBmp.SetResolution(96, 96);
                CopyGraphics = Graphics.FromImage(CopyBmp);                          // 把屏幕原始图像复制到CopyBmp上
                DrawPen      = new Pen(Color.DeepSkyBlue, 2);                        // 新建画笔DrawPen
                BackSolidBr  = new SolidBrush(Color.FromArgb(130, Color.Gray));

                if (IsStart)                                                                                      // 开始截屏
                {
                    RecBeginPoint = e.Location;
                    RecWidth      = Math.Abs(BeginPoint.X - RecBeginPoint.X);       // 截屏区域矩形宽度
                    RecHeigth     = Math.Abs(BeginPoint.Y - RecBeginPoint.Y);       // 截屏区域矩形高度

                    if (BeginPoint.X < RecBeginPoint.X)
                    {
                        RecBeginPoint.X = BeginPoint.X;
                    }

                    if (BeginPoint.Y < RecBeginPoint.Y)
                    {
                        RecBeginPoint.Y = BeginPoint.Y;
                    }
                }
                else if (IsMove)                                                                     // 移动截屏区域
                {
                    offsetX = e.X - DownPoint.X;                                                     // X方向平移的偏移量
                    offsetY = e.Y - DownPoint.Y;                                                     // Y方向平移的偏移量

                    RecBeginPoint.Offset(offsetX, offsetY);

                    if (RecBeginPoint.Y <= 0)                                                          // 防止移动出屏幕外
                    {
                        RecBeginPoint.Y = 0;
                    }
                    else if (RecBeginPoint.Y + RecHeigth >= ScreenHeight)
                    {
                        RecBeginPoint.Y = ScreenHeight - RecHeigth;
                    }

                    if (RecBeginPoint.X <= 0)
                    {
                        RecBeginPoint.X = 0;
                    }
                    else if (RecBeginPoint.X + RecWidth >= ScreenWidth)
                    {
                        RecBeginPoint.X = ScreenWidth - RecWidth;
                    }

                    DownPoint = e.Location;
                }
                else if (IsResize)                                                                             // 改变截屏区域大小
                {
                    switch (resize_type)
                    {
                    case Resize_Type.LeftTop:                                                         // 从左上方拖动
                        RecWidth  = Math.Abs(e.X - RecRightBottom.X);
                        RecHeigth = Math.Abs(e.Y - RecRightBottom.Y);
                        if (e.X > RecRightBottom.X)
                        {
                            RecBeginPoint.X = RecRightBottom.X;
                        }
                        else
                        {
                            RecBeginPoint.X = e.X;
                        }
                        if (e.Y > RecRightBottom.Y)
                        {
                            RecBeginPoint.Y = RecRightBottom.Y;
                        }
                        else
                        {
                            RecBeginPoint.Y = e.Y;
                        }
                        break;

                    case Resize_Type.LeftMid:                                                         // 从左中拖动
                        RecWidth = Math.Abs(e.X - RecRightTop.X);
                        if (e.X > RecRightTop.X)
                        {
                            RecBeginPoint.X = RecRightTop.X;
                        }
                        else
                        {
                            RecBeginPoint.X = e.X;
                        }
                        break;

                    case Resize_Type.LeftBottom:                                                   // 从左下方拖动
                        RecWidth  = Math.Abs(e.X - RecRightTop.X);
                        RecHeigth = Math.Abs(e.Y - RecRightTop.Y);
                        if (e.X < RecRightTop.X)
                        {
                            RecBeginPoint.X = e.X;
                        }
                        else
                        {
                            RecBeginPoint.X = RecRightTop.X;
                        }
                        if (e.Y < RecRightTop.Y)
                        {
                            RecBeginPoint.Y = e.Y;
                        }
                        else
                        {
                            RecBeginPoint.Y = RecRightTop.Y;
                        }
                        break;

                    case Resize_Type.MidTop:
                        RecHeigth = Math.Abs(e.Y - RecLeftBottom.Y);
                        if (e.Y < RecLeftBottom.Y)
                        {
                            RecBeginPoint.Y = e.Y;
                        }
                        else
                        {
                            RecBeginPoint.Y = RecLeftBottom.Y;
                        }
                        break;

                    case Resize_Type.MidBottom:
                        RecHeigth = Math.Abs(e.Y - RecLeftTop.Y);
                        if (e.Y > RecLeftTop.Y)
                        {
                            RecBeginPoint = RecLeftTop;
                        }
                        else
                        {
                            RecBeginPoint.Y = e.Y;
                        }
                        break;

                    case Resize_Type.RightTop:
                        RecWidth  = Math.Abs(e.X - RecLeftBottom.X);
                        RecHeigth = Math.Abs(e.Y - RecLeftBottom.Y);
                        if (e.X < RecLeftBottom.X)
                        {
                            RecBeginPoint.X = e.X;
                        }
                        else
                        {
                            RecBeginPoint.X = RecLeftBottom.X;
                        }
                        if (e.Y < RecLeftBottom.Y)
                        {
                            RecBeginPoint.Y = e.Y;
                        }
                        else
                        {
                            RecBeginPoint.Y = RecLeftBottom.Y;
                        }
                        break;

                    case Resize_Type.RightMid:
                        RecWidth = Math.Abs(e.X - RecLeftTop.X);
                        if (e.X < RecLeftTop.X)
                        {
                            RecBeginPoint.X = e.X;
                        }
                        else
                        {
                            RecBeginPoint.X = RecLeftTop.X;
                        }
                        break;

                    case Resize_Type.RightBottom:
                        RecWidth  = Math.Abs(e.X - RecLeftTop.X);
                        RecHeigth = Math.Abs(e.Y - RecLeftTop.Y);
                        if (e.X < RecLeftTop.X)
                        {
                            RecBeginPoint.X = e.X;
                        }
                        else
                        {
                            RecBeginPoint.X = RecLeftTop.X;
                        }
                        if (e.Y < RecLeftTop.Y)
                        {
                            RecBeginPoint.Y = e.Y;
                        }
                        else
                        {
                            RecBeginPoint.Y = RecLeftTop.Y;
                        }
                        break;
                    }
                }
                RefreshRec();
                return;
            }
            else if (IsLBtnPress == true && this.paintmode == PaintMode.EditMode)
            {
                SolidBrush solidBrush = new SolidBrush(Color.Red);

                lineendpoint = e.Location;
                RefreshPaintRegion();
            }
            // 鼠标移动时没有任何键按下时发生的事件
            CurrentPoint = e.Location;
            if (LeftTop.Contains(CurrentPoint) || RightBottom.Contains(CurrentPoint))
            {
                this.Cursor = Cursors.SizeNWSE;
            }
            else if (RightTop.Contains(CurrentPoint) || LeftBottom.Contains(CurrentPoint))
            {
                this.Cursor = Cursors.SizeNESW;
            }
            else if (MidTop.Contains(CurrentPoint) || MidBottom.Contains(CurrentPoint))
            {
                this.Cursor = Cursors.SizeNS;
            }
            else if (LeftMid.Contains(CurrentPoint) || RightMid.Contains(CurrentPoint))
            {
                this.Cursor = Cursors.SizeWE;
            }
            else if (CapRec.Contains(CurrentPoint))
            {
                this.Cursor = Cursors.SizeAll;
            }
            else
            {
                this.Cursor = Cursors.Arrow;
            }
        }
Exemplo n.º 2
0
 private void BmpForm_MouseDown(object sender, MouseEventArgs e)
 {
     if (this.paintmode == PaintMode.EditMode)
     {
         linebeginpoint = e.Location;
         IsLBtnPress    = true;
         Graphics grah = this.CreateGraphics();
         grah.DrawLine(new Pen(new SolidBrush(Color.Red)), new Point(0, 0), new Point(500, 500));
     }
     else if (this.paintmode == PaintMode.none)
     {
         if (e.Button == MouseButtons.Left)                   // 左键被按下时
         {
             this.toolStrip1.Hide();
             IsLBtnPress  = true;
             CurrentPoint = e.Location;
             Rectangle NewCapRec = new Rectangle(new Point(CapRec.X + 5, CapRec.Y + 5), new Size(CapRec.Width - 10, CapRec.Height - 10));
             RecRightBottom = new Point(RecBeginPoint.X + RecWidth, RecBeginPoint.Y + RecHeigth);
             RecRightTop    = new Point(RecBeginPoint.X + RecWidth, RecBeginPoint.Y);
             RecLeftTop     = RecBeginPoint;
             RecLeftBottom  = new Point(RecBeginPoint.X, RecBeginPoint.Y + RecHeigth);
             if (NewCapRec.Contains(CurrentPoint))                // 左键按下时,如果落在矩形区域内,则表示要移动已经选择了的矩形
             {
                 this.Cursor = Cursors.SizeAll;
                 IsMove      = true;
                 DownPoint   = e.Location;
             }
             else if (LeftTop.Contains(CurrentPoint))                      // 左键按下时,如果落在八个小矩形对应的范围内,则表示择了的矩形
             {
                 this.Cursor = Cursors.SizeNWSE;
                 IsResize    = true;
                 resize_type = Resize_Type.LeftTop;
             }
             else if (LeftMid.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.SizeWE;
                 IsResize    = true;
                 resize_type = Resize_Type.LeftMid;
             }
             else if (LeftBottom.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.SizeNESW;
                 IsResize    = true;
                 resize_type = Resize_Type.LeftBottom;
             }
             else if (MidTop.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.SizeNS;
                 IsResize    = true;
                 resize_type = Resize_Type.MidTop;
             }
             else if (MidBottom.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.SizeNS;
                 IsResize    = true;
                 resize_type = Resize_Type.MidBottom;
             }
             else if (RightTop.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.SizeNESW;
                 IsResize    = true;
                 resize_type = Resize_Type.RightTop;
             }
             else if (RightMid.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.SizeWE;
                 IsResize    = true;
                 resize_type = Resize_Type.RightMid;
             }
             else if (RightBottom.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.SizeNWSE;
                 IsResize    = true;
                 resize_type = Resize_Type.RightBottom;
             }
             else if (!CapRec.Contains(CurrentPoint))
             {
                 this.Cursor = Cursors.Cross;
                 IsStart     = true;
                 BeginPoint  = e.Location;
             }
         }
     }
 }