代码之家  ›  专栏  ›  技术社区  ›  Farid-ur-Rahman

用鼠标绘制矩形

  •  3
  • Farid-ur-Rahman  · 技术社区  · 13 年前

    有没有什么方法可以用起点和终点而不是起点和面积来绘制矩形?。我使用以下代码,通过鼠标在表单上绘制矩形:

        System.Drawing.Graphics formGraphics;
        bool isDown = false;
        int initialX;
        int initialY;
    
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            isDown = true;
            initialX = e.X;
            initialY = e.Y;
        }
    
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDown == true)
            {
                this.Refresh();
                Pen drwaPen = new Pen(Color.Navy,1);
                int width = e.X - initialX, height = e.Y - initialY;
                //if (Math.Sign (width) == -1) width = width 
                //Rectangle rect = new Rectangle(initialPt.X, initialPt.Y, Cursor.Position.X - initialPt.X, Cursor.Position.Y - initialPt.Y);
                Rectangle rect = new Rectangle(initialX, initialY, width * Math.Sign(width), height * Math.Sign(height));
    
                formGraphics = this.CreateGraphics();
                formGraphics.DrawRectangle(drwaPen, rect);
            }
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isDown = false;
        }
    

    我可以用这个代码画矩形,当我把鼠标从起点移回来时,矩形应该是翻转的,但我的矩形不是这样做,而是继续沿着鼠标光标的相反方向画。简而言之,此代码在正向绘制矩形时工作良好,但在反向绘制矩形时不起作用。

    2 回复  |  直到 9 年前
        1
  •  8
  •   Mark Heath    13 年前

    正如Jamiec所提到的,只要打电话 Invalidate MouseMove 处理程序,并在 OnPaint 方法/ Paint 事件处理程序

    要向前或向后绘制正确的矩形,请尝试以下操作:

    Rectangle rect = new Rectangle(Math.Min(e.X, initialX), 
                                   Math.Min(e.Y, initialY), 
                                   Math.Abs(e.X - initialX), 
                                   Math.Abs(e.Y - initialY));
    
        2
  •  -2
  •   Rob    7 年前

    试试这个:

    private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            IsMouseDown = true; // If This Event Is Occured So This Variable Is True.
    
            LocationXY = e.Location; // Get The Starting Location Of Point X and Y.
        }
    
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown == true) // This Block Is Not Execute Until Mouse Down Event Is Not a Fire.
            {
                LocationX1Y1 = e.Location; // Get The Current Location Of Point X and Y.
    
                Refresh(); // Refresh the form.
            }
        }
    
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if(IsMouseDown == true) // This Block Is Not Execute Until Mouse Down Event Is Not a Fire.
            {
                LocationX1Y1 = e.Location; // Get The Ending Point of X and Y.
    
                IsMouseDown = false; // false this..
            }
        }
    
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (rect != null) // Check If Rectangle Is Not a null.
            {
                e.Graphics.DrawRectangle(Pens.Red, GetRect()); // GetRect() Is a Function, Now Creates this function.
            }
        }
    
        private Rectangle GetRect()
        {
        //Create Object Of rect. we define rect at TOP.
            rect = new Rectangle();
    
        //The x-value of our Rectangle should be the minimum between the start x-value and the current x-position.
            rect.X = Math.Min(LocationXY.X, LocationX1Y1.X);
    
        //same as above x-value. The y-value of our Rectangle should be the minimum between the start y-value and the current y-position.
            rect.Y = Math.Min(LocationXY.Y, LocationX1Y1.Y);
    
         //the width of our rectangle should be the maximum between the start x-position and current x-position MINUS.
            rect.Width = Math.Abs(LocationXY.X - LocationX1Y1.X); 
    
            rect.Height = Math.Abs(LocationXY.Y - LocationX1Y1.Y);
    
            return rect;
        }
    
    推荐文章