【问题标题】:How to draw and move shapes using mouse in C#如何在 C# 中使用鼠标绘制和移动形状
【发布时间】:2013-04-12 22:26:42
【问题描述】:

我是 C# 编程的新手,想寻求一点帮助。我目前正在尝试使用鼠标左键移动在 Windows 应用程序表单上绘制的填充颜色的矩形,并且我正在尝试使用鼠标右键将其拖放到另一个位置。目前我已经设法绘制了矩形,但是右键单击正在拖动整个表单。

这是我的代码:

public partial class Form1 : Form
{
    private Point MouseDownLocation;

    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
    }
    Rectangle rec = new Rectangle(0, 0, 0, 0);

    protected override void OnPaint(PaintEventArgs e)
    {   
        e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
    }


    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left) 
        {
            rec = new Rectangle(e.X, e.Y, 0, 0);
            Invalidate();

        }
        if (e.Button == MouseButtons.Right)
        {
            MouseDownLocation = e.Location;
        }
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {             

        if (e.Button == MouseButtons.Left)
        {
            rec.Width = e.X - rec.X;
            rec.Height = e.Y - rec.Y;
            Invalidate();
        }

        if (e.Button == MouseButtons.Right)
        {
            this.Left = e.X + this.Left - MouseDownLocation.X;
            this.Top = e.Y + this.Top - MouseDownLocation.Y;

        }
    }

}

我只需要用鼠标右键拖放矩形。

编辑:谢谢大家,我很快就得到了答案,下面是有效的代码:

public partial class Form1 : Form
{
    private Point MouseDownLocation;

    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;            
    }

    Rectangle rec = new Rectangle(0, 0, 0, 0);

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
        //Generates the shape            
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        //can also use this one:
        //if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            rec = new Rectangle(e.X, e.Y, 0, 0);
            Invalidate();

        }
        if (e.Button == MouseButtons.Right)
        {
            MouseDownLocation = e.Location;
        }
    }
    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            rec.Width = e.X - rec.X;
            rec.Height = e.Y - rec.Y;
            this.Invalidate();
        }

        if (e.Button == MouseButtons.Right)
        {
            rec.Location = new Point((e.X - MouseDownLocation.X) + rec.Left, (e.Y - MouseDownLocation.Y) + rec.Top);
            MouseDownLocation = e.Location;
            this.Invalidate();
        }
    }

}

【问题讨论】:

    标签: c# shapes mousemove mousedown


    【解决方案1】:

    这似乎有效

        protected override void OnMouseMove(MouseEventArgs e)
        {
    
    
            if (e.Button == MouseButtons.Left)
            {
                rec.Width = e.X - rec.X;
                rec.Height = e.Y - rec.Y;
                this.Invalidate();
            }
    
            if (e.Button == MouseButtons.Right)
            {
                rec.Location = new Point((e.X-MouseDownLocation.X) + rec.Left, (e.Y-MouseDownLocation.Y) + rec.Top);
                MouseDownLocation = e.Location;
                this.Invalidate();
            }
        }
    

    【讨论】:

      【解决方案2】:

      试试这个: 请注意,我在此方法中将计时器添加到表单中,您不需要调用 鼠标事件无效,计时器正在调用 Refresh(),因此表单 会在每个滴答声中画自己..

      public partial class Form1 : Form
      {
         private Point MouseDownLocation;
      
         public Form1()
         {
            InitializeComponent();
            this.DoubleBuffered = true;
            timer1.Start(); // add timer to the form
         }
         Rectangle rec = new Rectangle(0, 0, 0, 0);
      
         protected override void OnPaint(PaintEventArgs e)
         {
             e.Graphics.FillRectangle(Brushes.DeepSkyBlue, rec);
         }
      
         private void timer1_Tick(object sender, EventArgs e)
         {
             Refresh();
         }
      
      
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
               rec.Width = e.X - rec.X;
               rec.Height = e.Y - rec.Y;
            }
            else if (e.Button == MouseButtons.Right)
            {
              rec.X = e.X - MouseDownLocation.X;
              rec.Y = e.Y - MouseDownLocation.Y;
            }
        }
      
         protected override void OnMouseUp(object sender, MouseEventArgs e)
         {
             if (e.Button == MouseButtons.Right)
                MouseDownLocation = e.Location;
         }
       }
      

      【讨论】:

      • 作为新手,我还没有使用计时器。添加它似乎是合乎逻辑的。但是在这种情况下如何定义 Timer1 呢?
      • 在 WinForms 上进入设计视图并将计时器控件拖到您的表单中,然后您将在下面看到它,添加 timerTick 方法只需双击它,您可以在表单时启动计时器加载或在表单 c'tor 上,使用 timer.Start() 或 timer.Enabled = true 当你想停止计时器时,你可以使用 timer.Stop() 或 timer.Enabled = false
      • 我已经完成了所有这些,程序启动了,但它没有绘制任何东西
      • 它在开始时不绘制任何东西,因为初始矩形的宽度和高度为 0 ,您需要按左键并移动鼠标来更改矩形的宽度和高度以及它'将绘制矩形
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多