2013-04-12 103 views
5

我是C#编程新手,想要寻求一点帮助。我目前正试图移动一个填充了矩形的彩色矩形,这是我用鼠标左键在Windows应用程序窗体上绘制的,我试图用鼠标右键将它拖放到另一个位置。目前我已经设法绘制矩形,但右键点击拖动整个表单。如何在C中使用鼠标绘制和移动形状#

这里是我的代码:

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

} 

回答

2

这似乎是工作

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

它的工作!非常感谢你。 –

2

试试这个: 注意我将此计时器添加到此方法中的表单中,您不需要拨打 对鼠标事件无效,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; 
    } 
} 
+0

由于即时通讯新的这个我还没有使用计时器。似乎有理由补充说。但是如何在这种情况下定义Timer1? –

+0

在WinForms上转到设计视图并拖动计时器控件到您的表格 之后,您会在下面看到它,添加timerTick方法 只需双击它,您可以在窗体加载时或启动时启动计时器形式c'tor,使用timer.Start()或timer.Enabled = true 当你想停止计时器,你可以使用timer.Stop()或timer.Enabled = false – Elior

+0

我已经完成了所有现在和程序开始,但它没有绘制任何东西 –