C# – 窗体的鼠标按下事件无效

huangapple go评论121阅读模式
英文:

C# - Mouse Down event for form is not working

问题

  1. 我正在尝试制作可拖动的窗体,但当我尝试检测鼠标按下和抬起事件时,却没有触发事件。其他功能似乎是有效的,但这两个事件没有触发。
英文:

I'm trying to make draggable window form, but when I tried to detect mouse down and up event to form, but event does not fire.
Other things seems working but those 2 events are not.

  1. public form()
  2. {
  3. InitializeComponent();
  4. this.BackColor = Color.Fuchsia;
  5. this.TransparencyKey = this.BackColor;
  6. Debug.Write("initialized");
  7. }
  8. private void form_MouseDown(object sender, MouseEventArgs e)
  9. {
  10. Debug.Write("dragging..");
  11. }
  12. private void form_MouseUp(object sender, MouseEventArgs e)
  13. {
  14. Debug.Write("drag done");
  15. }

答案1

得分: 0

  1. 最好将一个 `panel` 添加到您的表单,并将 `Dock` 设置为 `top` 以覆盖标题栏区域。然后使用这个示例代码
  2. private bool isDragging = false;
  3. private Point lastLocation;
  4. private void panel_MouseDown(object sender, MouseEventArgs e)
  5. {
  6. isDragging = true;
  7. lastLocation = e.Location;
  8. }
  9. private void panel_MouseMove(object sender, MouseEventArgs e)
  10. {
  11. if (isDragging)
  12. {
  13. this.Location = new Point(
  14. (this.Location.X - lastLocation.X) + e.X,
  15. (this.Location.Y - lastLocation.Y) + e.Y);
  16. this.Update();
  17. }
  18. }
  19. private void panel_MouseUp(object sender, MouseEventArgs e)
  20. {
  21. isDragging = false;
  22. }
  23. 不要忘记在表单构造函数中添加处理程序
  24. panel.MouseDown += panel_MouseDown;
  25. panel.MouseMove += panel_MouseMove;
  26. panel.MouseUp += panel_MouseUp;
英文:

it is better to add a panel to your form and set Dock to top in order to cover the title bar area. then use this sample code

  1. private bool isDragging = false;
  2. private Point lastLocation;
  3. private void panel_MouseDown(object sender, MouseEventArgs e)
  4. {
  5. isDragging = true;
  6. lastLocation = e.Location;
  7. }
  8. private void panel_MouseMove(object sender, MouseEventArgs e)
  9. {
  10. if (isDragging)
  11. {
  12. this.Location = new Point(
  13. (this.Location.X - lastLocation.X) + e.X,
  14. (this.Location.Y - lastLocation.Y) + e.Y);
  15. this.Update();
  16. }
  17. }
  18. private void panel_MouseUp(object sender, MouseEventArgs e)
  19. {
  20. isDragging = false;
  21. }

don't forget to add handlers in the form constructor

  1. panel.MouseDown += panel_MouseDown;
  2. panel.MouseMove += panel_MouseMove;
  3. panel.MouseUp += panel_MouseUp;

答案2

得分: 0

在你的窗体构造函数中,注册那些用于监听鼠标按下事件的方法,就像这样。

  1. public form() {
  2. InitializeComponent();
  3. this.BackColor = Color.Fuchsia;
  4. this.TransparencyKey = this.BackColor;
  5. Debug.Write("initialized");
  6. //注册用于监听事件的方法
  7. this.MouseDown += form_MouseDown;
  8. this.MouseUp += form_MouseUp;
  9. }
  10. private void form_MouseDown(object sender,
  11. MouseEventArgs e) {
  12. Debug.Write("拖拽中..");
  13. }
  14. private void form_MouseUp(object sender,
  15. MouseEventArgs e) {
  16. Debug.Write("拖拽完成");
  17. }
英文:

In your constructor for form, register those methods your using to listen for the mouse down events like this.

  1. public form() {
  2. InitializeComponent();
  3. this.BackColor = Color.Fuchsia;
  4. this.TransparencyKey = this.BackColor;
  5. Debug.Write("initialized");
  6. //register the methods to listen for the events
  7. this.MouseDown += form_MouseDown;
  8. this.MouseUp += form_MouseUp;
  9. }
  10. private void form_MouseDown(object sender,
  11. MouseEventArgs e) {
  12. Debug.Write("dragging..");
  13. }
  14. private void form_MouseUp(object sender,
  15. MouseEventArgs e) {
  16. Debug.Write("drag done");
  17. }

huangapple
  • 本文由 发表于 2023年2月18日 19:50:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定