用鼠标绘制矩形

时间:2012-10-01 11:36:21

标签: c# winforms drawing

有没有办法用起点和终点绘制矩形而不是起始点和区域?我使用以下代码,通过鼠标在表单上绘制矩形:

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

我可以使用此代码绘制矩形,当我从其起点向后移动鼠标时,矩形应该是翻转的,但是不是这样做,我的矩形继续以与鼠标光标相反的方向绘制。简而言之,此代码在向前绘制矩形时工作正常,但不适用于向后方向。

3 个答案:

答案 0 :(得分:7)

正如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));

答案 1 :(得分:0)

此代码使用户能够使用鼠标在具有起点和终点的表单上绘制矩形或椭圆。表单只包含一个组合框。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsTestBed1
{
    public partial class Form1 : Form
    {
        int _pressedLocationX;
        int _pressedLocationY;
        int _releasedLocationX;
        int _releasedLocationY;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            _pressedLocationX = e.X;
            _pressedLocationY = e.Y;
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            _releasedLocationX = e.X;
            _releasedLocationY = e.Y;

            using (Graphics graphics = base.CreateGraphics())
            using (SolidBrush solidbrush = new SolidBrush(Color.Magenta))
            using (Pen pen = new Pen(Color.DarkRed))
            {
                graphics.Clear(SystemColors.Control);

                switch (comboBox1.SelectedItem.ToString())
                {
                    case "Filled Square":
                        graphics.FillRectangle(solidbrush,
                            Math.Min(_pressedLocationX, _releasedLocationX), // Left
                            Math.Min(_pressedLocationY, _releasedLocationY), // Top
                            Math.Abs(_releasedLocationX - _pressedLocationX), // Width
                            Math.Abs(_releasedLocationY - _pressedLocationY)); // Height
                        break;

                    case "Filled Circle":
                        graphics.FillEllipse(solidbrush,
                            Math.Min(_pressedLocationX, _releasedLocationX), // Left
                            Math.Min(_pressedLocationY, _releasedLocationY), // Top
                            Math.Abs(_releasedLocationX - _pressedLocationX), // Width
                            Math.Abs(_releasedLocationY - _pressedLocationY)); // Height
                        break;

                    case "Line Square":
                        graphics.DrawRectangle(pen,
                            Math.Min(_pressedLocationX, _releasedLocationX), // Left
                            Math.Min(_pressedLocationY, _releasedLocationY), // Top
                            Math.Abs(_releasedLocationX - _pressedLocationX), // Width
                            Math.Abs(_releasedLocationY - _pressedLocationY)); // Height
                        break;

                    case "Line Circle":
                        graphics.DrawEllipse(pen,
                            Math.Min(_pressedLocationX, _releasedLocationX), // Left
                            Math.Min(_pressedLocationY, _releasedLocationY), // Top
                            Math.Abs(_releasedLocationX - _pressedLocationX), // Width
                            Math.Abs(_releasedLocationY - _pressedLocationY)); // Height
                        break;
                }
            }
        }
    }
}

答案 2 :(得分:-2)

尝试一下:

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