2013-04-12 102 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

我已經完成了所有現在和程序開始,但它沒有繪製任何東西 –