簡體   English   中英

如何在C#中使用鼠標繪制和移動形狀

[英]How to draw and move shapes using mouse in C#

我是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();
        }
    }

}

這似乎有效

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

嘗試這一個:請注意,我在此方法中向表單添加計時器,您不需要在鼠標事件上調用Invalidate timertick調用Refresh(),因此表單將在每個tick中繪制自己。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM