2016-08-15 167 views
3

我正在一個小應用程序中繪製矩形並用鼠標移動它們。用鼠標移動矩形的C#bug

我有一些邏輯裏面有一些錯誤,我找不到它們。

首先單擊並拖動總是穿繞位置(0,0)的矩形,第二次點擊和拖動將工作得很好。

第三次單擊將再次將矩形放在(0,0)上,然後用第四次單擊我可以再次拖動它。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Pr 
{ 
    public partial class Form1 : Form 
    { 
     int save=0; 
     Timer timer1 = new Timer(); 
     private Point MouseDownLocation; 

     private List<Rectangle> rectangles; 

     public Form1() 
     { 
      InitializeComponent(); 
      rectangles = new List<Rectangle>(); 
      panel1.Paint += panel1_Paint; 
      this.DoubleBuffered = true; 
      timer1.Interval = 10; 
      timer1.Start(); 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      Refresh(); 
     } 

     public void PopulateTable(int x, int y) 
     { 
      rectangles.Add(new Rectangle (500, 10, x, y)); 
      panel1.Invalidate(); 
     } 

     void panel1_Paint(object sender, PaintEventArgs e) 
     { 
      foreach(var rectangle in rectangles) 
      { 
       using (var b=new SolidBrush(Color.HotPink)) 
       { 
        e.Graphics.FillRectangle(b, rectangle); 
       } 
      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      this.WindowState = FormWindowState.Maximized; 
      this.MinimumSize = this.Size; 
      this.MaximumSize = this.Size; 
     } 

     private void panel1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       for (var i = 0; i < rectangles.Count; i++) 
       { 
        if (Cursor.Position.X >= rectangles[i].X && 
         Cursor.Position.X <= rectangles[i].X + rectangles[i].Width && 
         Cursor.Position.Y >= rectangles[i].Y && 
         Cursor.Position.Y <= rectangles[i].Y + rectangles[i].Height) 
        { 
         save = i; 
         Rectangle rect = rectangles[save]; 
         rect.X = e.X - MouseDownLocation.X; 
         rect.Y = e.Y - MouseDownLocation.Y; 

         rectangles[save] = rect; 
         panel1.Invalidate(); 
         break; 
        } 
       } 
      } 
     } 

     protected void panel1_OnMouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
       MouseDownLocation = e.Location; 
     } 

     private void panel2_Paint(object sender, PaintEventArgs e) 
     { 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      PopulateTable(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text)); 
     } 
    } 
} 

我敢打賭,我的移動邏輯不太好,所以我希望有人會提出一些想法。

編輯: 我只是改變

rect.X = e.X - MouseDownLocation.X; 
rect.Y = e.Y - MouseDownLocation.Y; 

rect.X = e.X; 
rect.Y = e.Y; 

它工作得更好,但我有一個問題。

當我點擊時,矩形會移動,以便矩形的左上角位於鼠標位置。

我想讓它保持在相同的位置,除非我移動它。

EDIT 2

我改變了部分代碼:

rect.X = rect.X + e.X - MouseDownLocation.X; 
rect.Y = rect.Y + e.Y - MouseDownLocation.Y; 

但是,現在的矩形移動太快,它會比鼠標要快得多。

這可能是定時器的問題嗎?

EDIT 3

MouseDownLocatioon必須的問題。如果沒有它的代碼有效(矩形在鼠標座標上移動),那意味着有什麼問題。

試圖這樣做:

protected void panel1_OnMouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
       MouseDownLocation = panel1.PointToScreen(e.Location); 
     } 

沒有幫助。

試過調試,看起來像Y座標有問題。它只是在Y座標上產生很大的偏移量。

我做了一個鼠標移動從左至右(僅X座標),和調試表明我:

rect.X = 500

rect.Y = 100

的eX = 848

e。Y = 216

MouseDownLocation.X = 850

MouseDownLocation.Y = 238

這意味着對於這種舉動差異僅是爲了×2,和y的22!

編輯4:管理解決它!

只需要添加兩行代碼:

MouseDownLocation.X = e.X; 
MouseDownLocation.Y = e.Y; 

因爲,如果我按住鼠標按鈕,它不會刷新新MouseDownLocation。

+0

請注意,矩形有(除其他外)一個很好的包含(點)功能!還要注意Cursor.Position是在屏幕座標中,而不是在Mousexxxevents中獲得的相對值!有兩個方向的轉換功能:Control.PointToClient和Control.PointToScreen – TaW

+0

感謝,@TaW在您的評論。這就是爲什麼我把面板(0,0),所以我不必改變 – omicito

+0

屏幕真的是屏幕,而不是形式!更好的做它真的是正確的,而不是扭曲你的佈局;-) – TaW

回答

1
rect.X = e.X - MouseDownLocation.X; 
rect.Y = e.Y - MouseDownLocation.Y; 

我還不能肯定是什麼MouseDownLocation是,而是基於名字,當你第一次開始移動框,當前位置(e.X,e.Y)等於MouseDownLocation,這意味着你的數學工程以(0,0) 。這似乎是你爲什麼走到角落的原因。

從技術上講,你是第二次和第四次點擊也是這樣做的,但既然你已經在角落裏,這是不明顯的。

編輯您的編輯矩形現在將角落移動到您的光標: 當您單擊鼠標時,您需要計算並存儲從矩形位置到鼠標的偏移量,然後使用該偏移量MouseMove事件處理程序。

+0

我很抱歉,看起來像我沒有複製/粘貼的一部分碼。 MouseDownLocation給出了按下游標時的位置 – omicito

+0

@omicito我覺得這是這種效果。我也在編輯中加入了一些關於答案的想法。 –

+0

是的,我做了這樣的事情,但現在我遇到了一些新問題。這是我的新編輯。 並感謝@泰勒 – omicito