按下按钮时移动鼠标时会引发什么事件?

Soh*_*eil 2 .net c# events mouseevent winforms

我需要一个.NET Framework标准事件中不存在的事件.例如,鼠标左键移动时鼠标移动.

我还需要改变一些行为.例如,我有一些按钮,我想在鼠标左键按下时更改光标所在的每个按钮的背景图像,但是当我单击一个按钮并按住鼠标左键时,我移动鼠标其他按钮不会引发任何事件.

我该怎么办?如何创建新活动?我怎样才能改变行为?

任何帮助表示赞赏.

stu*_*rtd 6

您的问题是当按钮上发生MouseDown事件时,该按钮"捕获"鼠标并且在释放按钮之前不会释放它,这意味着其他按钮不会接收MouseMove事件.

有一些代码从这里可以帮助:

 // Assuming all buttons subscribe to this event:
 private void buttons_MouseMove (object sender, MouseEventArgs e)
 {
   if (e.Button == System.Windows.Forms.MouseButtons.Left)
      {
       Control control = (Control) sender;
       if (control.Capture)
       {
          control.Capture = false;
       }
       if (control.ClientRectangle.Contains (e.Location)) 
       {
           Control.BackgroundImage = ...;
       }
    }
  }
Run Code Online (Sandbox Code Playgroud)