-->

如何绘制和移动在C#中使用鼠标形状(How to draw and move shapes usin

2019-08-22 14:37发布

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

}

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


Answer 2:

试试这个:请注意,我在这个方法,你不需要调用invalidate鼠标事件timertick被调用,以刷新()这样的形式将自己画在每个刻度添加定时器的形式..

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


文章来源: How to draw and move shapes using mouse in C#