2012-06-18 62 views
0

我的意思是,我只想在用戶將鼠標指針移出pictureBox1客戶區域時激活鼠標離開事件。只有當他按下鼠標左鍵時纔會發生這種情況,但是如果用戶沒有按下鼠標左鍵,就會發生這種情況,即他可以將鼠標移動到空閒位置,事件不會執行任何操作。如果用戶處於鼠標向下事件中,如何檢查pictureBox1鼠標離開事件?

我在Form1的頂部有一個叫做mouseLeave類型布爾的變量。

在構造函數中,我將它設置爲false;

在pictureBox1鼠標向下事件中,我做了mouseLeave變量爲true。

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
     { 
      if (e.Button == MouseButtons.Left) 
      { 
       label1.Visible = true; 
       label4.Visible = true; 
       // find the index that is closest to the current mouse location 
       float t = wireObject1.GetIndexByXY(e.X, e.Y, 5); 

       if (t == -1) 
       { 
        button3.Enabled = false; 
       } 
       else 
       { 
        button3.Enabled = true; 
        { 
         selectedIndex = t; 
         mouseMove = true; 
         mouseLeave = true; 

在pictureBox1鼠標移動事件我檢查的mouseMove爲真,那麼移動該點拖動它周圍:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (mouseMove == true) 
      { 
       Point NewPoint = e.Location; 
       { 

        wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why ! 

        label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}"; 
        pictureBox1.Refresh(); 
       } 

      } 
      else 
      { 
       label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}"; 
      } 
     } 

沒有離開它並拖動所以,當鼠標在用戶點擊左鍵點,那麼點將移動pictureBox1客戶區。

現在在pictureBox1鼠標離開事件我所做的:

private void pictureBox1_MouseLeave(object sender, EventArgs e) 
     { 
      if (mouseLeave == true) 
      { 
       mouseMove = false; 
      } 

     } 

但它dosent工作。我添加一個點拖動它移動它,但是這個事件只有當我將鼠標指針移出pictureBox1區域時纔會執行某些操作,而不會在沒有點擊鼠標左鍵時才拖動點。

我要的是,只有當我點擊按下鼠標左鍵,並在鼠標移動事件移動它周圍像才把這個假期事件會做一些在這種情況下,將使的mouseMove是假的。

所以用戶將無法將該點拖出pictureBox1區域。那麼我應該怎麼做鼠標離開事件呢?

EDITED **

這Button1的單擊我在每次新的點添加到pictureBox1區域。 而我繪製點的繪畫事件。

也許這將有助於解決問題的地方停下,但沒有在正確的地方,當拖出圖片框的邊界。

private void button1_Click(object sender, EventArgs e) 
     { 
      halfX = pictureBox1.ClientRectangle.Width/2; 
      halfY = pictureBox1.ClientRectangle.Height/2; 
      Random rnd = new Random(); 
      offsetX = rnd.Next(-10, 10); 
      offsetY = rnd.Next(-10, 10); 
      wireObject1.addPoint(halfX + offsetX, halfY + offsetY); 
      if (wireObjectCoordinates1 == null) 
       wireObjectCoordinates1 = new WireObjectCoordinates() { FrameNumber = currentFrameIndex }; 
      wireObjectCoordinates1.Point_X.Add(halfX + offsetX); 
      wireObjectCoordinates1.Point_Y.Add(halfY + offsetY); 
      wireObjectAnimation1._coordinatesList.Add(wireObjectCoordinates1); 
      pictureBox1.Refresh(); 
      numberOfPoints++; 
      label5.Text = "{X = " + (halfX + offsetX) + "}" + " " + "{Y = " + (halfY + offsetY) + "}"; 
      label5.Visible = true; 
      label7.Visible = true; 
      label16.Text = numberOfPoints.ToString(); 
      label16.Visible = true; 
      label15.Visible = true; 
      buttonLockMode = true; 
      button8.Enabled = true; 
      button4.Enabled = true; 


     } 



private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
     { 
      Point connectionPointStart; 
      Point connectionPointEnd; 
      Graphics g = e.Graphics; 
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; 
      SolidBrush brush = new SolidBrush(Color.Red); 
      Pen p = new Pen(brush); 
       for (int idx = 0; idx < wireObject1._point_X.Count; ++idx) 
       { 
        Point dPoint = new Point((int)wireObject1._point_X[idx], (int)wireObject1._point_Y[idx]); 
        dPoint.X = dPoint.X - 5; // was - 2 
        dPoint.Y = dPoint.Y - 5; // was - 2 
        Rectangle rect = new Rectangle(dPoint, new Size(10, 10)); 
        g.FillEllipse(brush, rect); 

        // g.FillEllipse(brush, rect); 
       } 

       for (int i = 0; i < wireObject1._connectionstart.Count; i++) 
       { 

        int startIndex = wireObject1._connectionstart[i]; 
        int endIndex = wireObject1._connectionend[i]; 



        connectionPointStart = new Point((int)wireObject1._point_X[startIndex], (int)wireObject1._point_Y[startIndex]); 
        connectionPointEnd = new Point((int)wireObject1._point_X[endIndex], (int)wireObject1._point_Y[endIndex]); 
        p.Width = 4; 
        g.DrawLine(p, connectionPointStart, connectionPointEnd); 

       } 



     } 

我想我在將鼠標移動到這一點,如果改變解決了這個問題:

if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) || 
        !((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5))) 

因此,移動事件應該是這樣的:

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
     { 
      if (mouseMove == true) 
      { 
       mouseDrag = true; 
       Point NewPoint = e.Location; 
       if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X + 5,NewPoint.Y) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X - 5,NewPoint.Y) || 
        !((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y + 5)) || (!((PictureBox)sender).ClientRectangle.Contains(NewPoint.X, NewPoint.Y - 5))) 
       { 
        if (mouseDrag) 
        { 
         mouseMove = false; 
         return; 
        } 
       } 

       { 
        wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why ! 

        label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}"; 
        pictureBox1.Refresh(); 
       } 

      } 
      else 
      { 
       label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}"; 
      } 
     } 

回答

0

而不是使用MouseLeave事件,您可以檢查鼠標位置是否在您的MouseMove事件中的PictureBox的客戶端矩形中,如下所示。

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    { 
     if (mouseMove == true) 
     { 
      Point NewPoint = e.Location; 

      if (!((PictureBox)sender).ClientRectangle.Contains(NewPoint)) 
      { 
       if (mouseLeave) 
       { 
        mouseMove = false; 
        return; 
       } 
      } 

      wireObject1.MovePoint(selectedIndex, NewPoint, NewPoint); // when moving a point dragging the other point is vanished deleted. To check why ! 

      label1.Text = "{X = " + NewPoint.X + "}" + " " + "{Y = " + NewPoint.Y + "}"; 
      pictureBox1.Refresh(); 

     } 
     else 
     { 
      label19.Text = "{X = " + e.X + "}" + " " + "{Y = " + e.Y + "}"; 
     } 
    } 

} 
+0

馬克·霍爾在全面的工作,但如果我移動點拖動它慢到圖片框的一個邊界我拖累了它的一半或幾乎所有的它,我可以在一些搬出綁定的圖片框的角度。如果我快速移動鼠標到邊界,有時候這個點會在邊界之前卡住一些,比如之前的5-10像素。所以一般來說,它會停止,但有時候並非全部。 – user1434011

+0

馬克霍爾我編輯我的問題,並在那裏添加button1單擊事件和繪畫事件也許這將有助於以某種方式解決這個問題,我不知道該怎麼做。我上面提到的問題在哪裏停止,但有時候它的一半點會停止在poictureBox邊界之外,有時會在pictureBox邊界之前停止。 – user1434011

+0

@ user1434011我很高興你能夠使它工作。 –

0

做這樣的事情:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
    { 
     pictureBox1.MouseMove -= OnMouseMove; 
    } 

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
      pictureBox1.MouseMove += OnMouseMove; 
    } 

    private void OnMouseMove(object sender, MouseEventArgs e) 
    { 
     Debug.WriteLine(e.Location); 
    } 

Whe在默認情況下,「OnMouseMove」未附加到pictureBox事件。