2014-05-21 24 views
-2

我得到的圖片框移動。但是當我點擊圖片框時,它會跳到所有的地方。任何人都可以幫我解決這個問題嗎?正試圖讓C#事件益智遊戲的任務

namespace Move_Shapes 
{ 
    public partial class Form1 : Form 
    { 
     int X = 0; 
     int Y = 0; 
     int mX = 0; 
     int mY = 0; 
     bool Move = false; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void pic_TL_Click(object sender, EventArgs e) 
     { 
      //X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture 
      //Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y; 

      //label1.Text = X.ToString(); 
      //label2.Text = Y.ToString(); 

      //X = MousePosition.X - this.Location.X - 8; 
      //Y = MousePosition.Y - this.Location.Y - 30; 

      //label3.Text = X.ToString(); 
      //label4.Text = Y.ToString(); 
     } 

     private void pic_TL_MouseDown(object sender, MouseEventArgs e) 
     { 
      X = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture 
      Y = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y; 

      label1.Text = X.ToString(); 
      label2.Text = Y.ToString(); 
      Move = true; 
     } 

     private void pic_TL_MouseMove(object sender, MouseEventArgs e) 
     { 
      mX = MousePosition.X - this.Location.X - 8 - pic_TL.Location.X; // loc of cursor in picture 
      mY = MousePosition.Y - this.Location.Y - 30 - pic_TL.Location.Y; 

      if (Move) 
      { 
       pic_TL.Location = new Point(mX - X, mY - Y); 
      } 

     } 

     private void pic_TL_MouseUp(object sender, MouseEventArgs e) 
     { 
      Move = false; 
     } 
    } 
} 
+1

請修改您的標題以解釋您的*問題*而不是您的一般情況。 –

+0

對此很新穎,但基本上發生了什麼,當我點擊鼠標它picturebox跳回和第四,我不知道爲什麼 – user3635818

+0

什麼是那些魔術常數:8,30? – ad1Dima

回答

0

在MoseMove上,您將位置設置爲當前鼠標位置和初始鼠標位置的差異。

pic_TL.Location = new Point(mX - X, mY - Y); 

我把鼠標移過一個像素,圖片就會移動到左上角。