winforms 如何使鼠标内部的事件代码只在按住鼠标左键的同时移动鼠标时执行?

n53p2ov0  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(242)

我尝试了一个全局变量标志bool name draw。但它不工作,它永远不会显示消息离开事件。
我用一个断点进行了检查,它确实到达了if行的leave事件,但没有产生我想要的效果。
我想要的效果,只有当按住鼠标左键,同时移动鼠标,然后如果离开pictureBox2区域,然后显示消息。

  1. private bool draw = false;
  2. public Form1()
  3. {
  4. InitializeComponent();
  5. InitializeForm();
  6. }
  7. private void InitializeForm()
  8. {
  9. textBox2.Text = Properties.Settings.Default.CroppedImagesFolder;
  10. selectedPath = textBox2.Text;
  11. if (!string.IsNullOrEmpty(textBox1.Text))
  12. {
  13. pictureBox2.Image = Image.FromFile(textBox1.Text);
  14. }
  15. checkBoxDrawBorder.Checked = true;
  16. checkBoxClearRectangles.Checked = true;
  17. checkBoxSaveRectangles.Checked = true;
  18. LoadFile(textBox2, "folder.txt");
  19. if (!string.IsNullOrEmpty(selectedPath) && Directory.Exists(selectedPath))
  20. {
  21. LoadFile(textBox1, "image.txt");
  22. LoadRectangles();
  23. }
  24. }
  25. public class DrawingRectangle
  26. {
  27. public Rectangle Rect => new Rectangle(Location, Size);
  28. public Size Size { get; set; }
  29. public Point Location { get; set; }
  30. public Control Owner { get; set; }
  31. public Point StartPosition { get; set; }
  32. public Color DrawingcColor { get; set; } = Color.LightGreen;
  33. public float PenSize { get; set; } = 3f;
  34. }
  35. private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
  36. {
  37. if (e.Button == MouseButtons.Left && pictureBox2.Image != null && selectedPath != null)
  38. {
  39. currentRect = new DrawingRectangle()
  40. {
  41. Location = e.Location,
  42. Size = Size.Empty,
  43. StartPosition = e.Location,
  44. Owner = (Control)sender,
  45. DrawingcColor = SelectedColor
  46. };
  47. }
  48. }
  49. private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
  50. {
  51. if (currentRect != null && e.Button == MouseButtons.Left)
  52. {
  53. draw = true;
  54. Point mouseLocation = pictureBox2.PointToClient(Cursor.Position);
  55. int width = mouseLocation.X - currentRect.Location.X;
  56. int height = mouseLocation.Y - currentRect.Location.Y;
  57. width = Math.Max(width, 0);
  58. height = Math.Max(height, 0);
  59. currentRect.Size = new Size(width, height);
  60. pictureBox2.Invalidate();
  61. }
  62. }
  63. private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
  64. {
  65. if (currentRect != null)
  66. {
  67. draw = false;
  68. DrawingRects.Add(currentRect);
  69. currentRect = null;
  70. ProcessRectangles();
  71. }
  72. }
  73. private void pictureBox2_MouseLeave(object sender, EventArgs e)
  74. {
  75. if (draw)
  76. {
  77. MessageBox.Show("You Leave At: " + MousePosition.X.ToString() + " --- " + MousePosition.Y.ToString());
  78. }
  79. }

字符串

ccgok5k5

ccgok5k51#

你看到这种行为(或没有这种行为)的原因是鼠标处于Capture状态。如果你不需要捕获它,你可以在鼠标按下时取消捕获它,让它按照你描述的方式工作:


的数据

  1. public class PictureBoxEx : PictureBox
  2. {
  3. protected override void OnMouseDown(MouseEventArgs e)
  4. {
  5. base.OnMouseDown(e);
  6. Capture = false;
  7. }
  8. protected override void OnMouseLeave(EventArgs e)
  9. {
  10. base.OnMouseLeave(e);
  11. if (MouseButtons == MouseButtons.Left)
  12. {
  13. var client = PointToClient(MousePosition);
  14. BeginInvoke(() =>
  15. MessageBox.Show($"You Leave At: {client.X} --- {client.Y}"));
  16. }
  17. }
  18. }

字符串
或者,你可以不去管Capture,自己实现鼠标离开行为:

  1. public class PictureBoxEx : PictureBox
  2. {
  3. protected override void OnMouseMove(MouseEventArgs e)
  4. {
  5. base.OnMouseMove(e);
  6. if ((MouseButtons == MouseButtons.Left) && !ClientRectangle.Contains(e.Location))
  7. {
  8. BeginInvoke(() =>
  9. MessageBox.Show($"You Leave At: {e.Location.X} --- {e.Location.Y}"));
  10. }
  11. }
  12. }

展开查看全部

相关问题