Control.MouseCaptureChanged 事件


当控件失去鼠标捕获时发生。Occurs when the control loses mouse capture.

 event EventHandler ^ MouseCaptureChanged;
public event EventHandler MouseCaptureChanged;
member this.MouseCaptureChanged : EventHandler 
Public Custom Event MouseCaptureChanged As EventHandler 




下面的代码示例演示 MouseCaptureChanged 控件的事件 ButtonThe following code example demonstrates the MouseCaptureChanged event for a Button control.

private void button1_MouseDown(object sender, MouseEventArgs e)

private void button1_MouseUp(object sender, MouseEventArgs e)

private void button1_MouseCaptureChanged(object sender, EventArgs e)
Private Sub Button1_MouseDown(ByVal sender As System.Object, _
ByVal e As MouseEventArgs) Handles Button1.MouseDown
End Sub

Private Sub Button1_MouseUp(ByVal sender As System.Object, _
ByVal e As MouseEventArgs) Handles Button1.MouseUp
End Sub

Private Sub Button1_MouseCaptureChanged(ByVal sender As System.Object, _
ByVal e As EventArgs) Handles Button1.MouseCaptureChanged
End Sub

若要测试此示例,请按 F5 在调试器中运行该示例。To test this example, run it in the debugger by pressing F5. 在 Visual Studio 中打开 " 输出 " 窗口,以便可以查看事件的引发时间。Open the Output window in Visual Studio so that you can see when events are raised. 单击 Button ,并注意以下输出。Click the Button and notice the following output.




现在,单击并按住控件上的鼠标左键 ButtonNow, click and hold the left mouse button on the Button control. 如果仍单击鼠标,请按 ALT + TAB 切换到另一个程序。While still clicking the mouse, press ALT+TAB to switch to another program. 请注意, MouseCaptureChanged 会引发事件,使您能够处理此方案。Notice that the MouseCaptureChanged event is raised enabling you to potentially handle this scenario. 根据您的操作, MouseUp 可能不会引发事件。Depending on your actions, the MouseUp event might not be raised. 还可以通过 Windows 键或 CTRL + ESC 尝试此测试。You can also try this test with the Windows key or CTRL+ESC.




在极少数情况下,可能需要检测意外输入。In rare scenarios, you might need to detect unexpected input. 例如,请考虑以下方案。For example, consider the following scenarios.

  • 在鼠标操作期间,用户通过按 Windows 键或 CTRL + ESC 打开 "开始" 菜单。During a mouse operation, the user opens the Start menu by pressing the Windows key or CTRL+ESC.

  • 在鼠标操作期间,用户可以通过按 ALT + TAB 切换到另一个程序。During a mouse operation, the user switches to another program by pressing ALT+TAB.

  • 在鼠标操作过程中,其他程序将显示一个窗口,或者从当前应用程序中获得焦点的消息框。During a mouse operation, another program displays a window or a message box that takes focus away from the current application.

鼠标操作可以包括在窗体或控件上单击和按住鼠标,或执行鼠标拖动操作。Mouse operations can include clicking and holding the mouse on a form or a control, or performing a mouse drag operation. 如果您必须检测窗体或控件何时失去这些和相关的意外方案的鼠标捕获,则可以使用 MouseCaptureChanged 事件。If you have to detect when a form or a control loses mouse capture for these and related unexpected scenarios, you can use the MouseCaptureChanged event.