2013-04-12 231 views
5

Tôi mới lập trình trong C# và muốn yêu cầu trợ giúp một chút. Tôi hiện đang cố gắng di chuyển một hình chữ nhật đầy màu sắc mà tôi vẽ trên biểu mẫu Ứng dụng Windows bằng nút chuột trái của tôi và tôi đang cố gắng kéo và thả nó đến một vị trí khác bằng nút chuột phải của tôi. Hiện tại tôi đã quản lý để vẽ hình chữ nhật, nhưng nhấp chuột phải là kéo toàn bộ biểu mẫu dọc theo.Cách vẽ và di chuyển hình dạng bằng chuột trong C#

Dưới đây là mã của tôi:

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; 

     } 
    } 

} 

Tôi chỉ cần kéo và thả các hình chữ nhật với nút chuột phải.

EDIT: Nhờ tất cả tôi đã nhận câu trả lời của tôi rất nhanh và ở đây bạn là đoạn code mà hoạt động:

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

} 

Trả lời

2

Điều này dường như làm việc

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

Nó hoạt động! Cảm ơn nhiều. –

2

thử cái này: ý rằng Tôi thêm bộ đếm thời gian vào biểu mẫu trong phương thức này bạn không cần phải gọi số Sự cố trên chuột không hợp lệ timertick đang gọi đến Làm mới() để biểu mẫu sẽ tự vẽ trong mỗi đánh dấu ..

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

Như im mới này tôi đã không được sử dụng bộ đếm thời gian nào được nêu ra. Có vẻ hợp lý để thêm điều đó. Nhưng làm thế nào để bạn xác định Timer1 trong trường hợp này? –

+0

Trên WinForms chuyển sang dạng xem thiết kế và kéo điều khiển bộ đếm thời gian sang biểu mẫu sau đó bạn sẽ thấy nó bên dưới, để thêm phương thức timerTick chỉ cần nhấp đúp vào nó, bạn có thể bắt đầu hẹn giờ khi biểu mẫu tải hoặc trên form c'tor, sử dụng timer.Start() hoặc timer.Enabled = true khi bạn muốn dừng timer bạn có thể sử dụng timer.Stop() hoặc timer.Enabled = false – Elior

+0

Tôi đã thực hiện tất cả những gì bây giờ và chương trình bắt đầu, nhưng nó không vẽ bất cứ điều gì –