Control.MouseCaptureChanged Control.MouseCaptureChanged Control.MouseCaptureChanged Control.MouseCaptureChanged Event

定义

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

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

示例

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

private void button1_MouseDown(object sender, MouseEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("button1_MouseDown");
}

private void button1_MouseUp(object sender, MouseEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("button1_MouseUp");
}

private void button1_MouseCaptureChanged(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("button1_MouseCaptureChanged");
}
Private Sub Button1_MouseDown(ByVal sender As System.Object, _
ByVal e As MouseEventArgs) Handles Button1.MouseDown
    Debug.WriteLine("Button1_MouseDown")
End Sub

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

Private Sub Button1_MouseCaptureChanged(ByVal sender As System.Object, _
ByVal e As EventArgs) Handles Button1.MouseCaptureChanged
    Debug.WriteLine("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.

button1_MouseDown

button1_MouseUp

button1_MouseCaptureChanged

现在,单击并按住鼠标左键Button控件。Now, 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.

button1_MouseDown

button1_MouseCaptureChanged

注解

在极少数情况下,可能需要检测意外的输入。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.

适用于