2013-04-12 20 views
5

C# programlama için yeni konum ve biraz yardım istemek istedim. Şu anda sol fare düğmemle bir Windows Uygulama formunda çizdiğim renk dolu bir dikdörtgeni taşımaya çalışıyorum ve sağ fare düğmesini kullanarak başka bir yere sürükleyip bırakmaya çalışıyorum. Şu anda dikdörtgeni çizmeyi başardım, ancak sağ tıklamayla tüm form sürükleniyor. Ben sadece sağ fare düğmesi ile dikdörtgenler sürükleyip-gerekirC# ile fare kullanarak şekilleri çizmek ve hareket ettirin # 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; 

     } 
    } 

} 

:

İşte benim kod.

DÜZENLEME: Size burada cevabım çok hızlı ve got tüm sayesinde çalışır kod:

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

} 

cevap

2

Bu iş gibi görünüyor

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

Çalışıyor! Çok teşekkür ederim. –

2

bu bir deneyin: Bildirimi o Bu yöntemde, zamanlayıcıyı fare olayı için aramaya gerek duymazsınız. Zamanlayıcı, Refresh() öğesine çağırarak, formunun kendisini boyayacak Her kene içinde ..

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

Bunun için yeni olduğum için henüz zamanlayıcı kullanmadım. Bunu eklemek mantıklı görünüyor. Ama bu durumda Timer1'i nasıl tanımlarsınız? –

+0

WinForms üzerinde tasarım görünümüne gidin ve zamanlayıcı kontrolünü formunuzda formuna sürükleyin, ardından aşağıda göreceksiniz, timerTick metodu 'u sadece çift tıklayın, formu yüklendiğinde veya formu c'tor, timer.Start() veya timer.Enabled = true kullanarak zamanlayıcıyı durdurmak istediğinizde timer.Stop() veya timer.Enabled = false seçeneğini kullanabilirsiniz – Elior

+0

Tüm bunları şimdi yaptım ve program başlar, ama hiçbir şey çizmiyor –

İlgili konular