Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中使用鼠标绘制和移动形状#_C#_Shapes_Mousemove_Mousedown - Fatal编程技术网

C# 如何在C中使用鼠标绘制和移动形状#

C# 如何在C中使用鼠标绘制和移动形状#,c#,shapes,mousemove,mousedown,C#,Shapes,Mousemove,Mousedown,我是C语言编程新手,想寻求一些帮助。我目前正试图移动一个用鼠标左键在Windows应用程序窗体上绘制的填充颜色的矩形,并试图用鼠标右键将其拖放到另一个位置。目前我已经成功地绘制了矩形,但是右键单击正在拖动整个表单 这是我的密码: public partial class Form1 : Form { private Point MouseDownLocation; public Form1() { InitializeComponent();

我是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();
        }
    }
试试这个: 注意,我在表单中添加了计时器,您不需要调用这个方法 timertick正在调用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;
   }
 }

因为我是新手,所以我还没有用过定时器。加上这一点似乎合乎逻辑。但是在这种情况下如何定义计时器1呢?在WinForms上,转到design视图并将timer控件拖到窗体上,然后您将在下面看到它,要添加timerTick方法,只需双击它,就可以在加载窗体时启动计时器,或者在窗体上启动计时器,使用timer.start()或者timer.Enabled=true当你想停止计时器时,你可以使用timer.stop()或者timer.Enabled=false我已经完成了所有这些,程序启动了,但是它没有绘制任何东西它在开始时没有绘制任何东西,因为初始矩形的宽度和高度为0,您需要按下左键并移动鼠标来更改矩形的宽度和高度,它将绘制矩形