ContentElement.CaptureMouse 方法


尝试将鼠标强制捕获到此元素。Attempts to force capture of the mouse to this element.

 virtual bool CaptureMouse();
public bool CaptureMouse ();
abstract member CaptureMouse : unit -> bool
override this.CaptureMouse : unit -> bool
Public Function CaptureMouse () As Boolean



如果成功捕获了鼠标,则为 true;否则为 falsetrue if the mouse is successfully captured; otherwise, false.



下面的示例根据鼠标是否已由元素捕获来捕获鼠标或释放捕获。The following example captures the mouse or releases capture, based on whether the mouse is already captured by the element. 请注意,此示例将预期的捕获目标元素强制转换为 IInputElement 接口,因而最初调用 IInputElement.CaptureMouse 方法。Note that this example casts the prospective capture target element to the IInputElement interface, and is thus initially calling the IInputElement.CaptureMouse method. IInputElement如果您不确定要捕获鼠标的元素是否为或,则强制转换为很有用 UIElement ContentElementCasting to IInputElement is a technique that is useful if you are unsure whether the element you want to have capture the mouse is a UIElement or a ContentElement. 然后,接口强制转换和接口方法调用在内部调用适当的特定于类型的 CaptureMouse 实现,而无需将试验强制转换为 UIElementContentElementThe interface cast and the interface method call then calls the appropriate type-specific CaptureMouse implementation internally without requiring a trial cast to either UIElement or ContentElement. 此相同的强制转换技术适用于 IInputElement 定义多个输入相关事件和其他输入相关方法的其他成员。This same casting technique works for other members that IInputElement defines, for instance many of the input-related events, and other input-related methods.

private void CaptureMouseCommandExecuted(object sender, ExecutedRoutedEventArgs e)
  MessageBox.Show("Mouse Command");
  IInputElement target = Mouse.DirectlyOver;

  target = target as Control;
  if (target != null)
    if (!target.IsMouseCaptured)
    } else {
Private Sub CaptureMouseCommandExecuted(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
  MessageBox.Show("Mouse Command")
  Dim target As IInputElement = Mouse.DirectlyOver

  target = TryCast(target, Control)
  If target IsNot Nothing Then
    If Not target.IsMouseCaptured Then
    End If
  End If
End Sub


若要捕获,必须启用一个元素。To be captured, an element must be enabled. IsEnabled true 调用之前检查是否为 CaptureMouseCheck whether IsEnabled is true before you call CaptureMouse.

如果调用 CaptureMouse 返回 true ,则 IsMouseCaptured 也是 trueIf calling CaptureMouse returns true, then IsMouseCaptured is also true.

如果调用 CaptureMouse 返回 true ,则 GotMouseCapture IsMouseCapturedChanged 会引发和事件,并将 RoutedEventArgs.Source 在事件数据中报告为调用方法的元素 CaptureMouseIf calling CaptureMouse returns true, then the GotMouseCapture and IsMouseCapturedChanged events are raised, with RoutedEventArgs.Source in the event data reported as the element where the CaptureMouse method is called. 如果强制捕获,则可能会干扰现有捕获,尤其是与用鼠标拖放相关的捕获。If you force capture, you might interfere with existing captures - especially with captures that relate to drag-and-drop with the mouse.

若要从所有元素中清除鼠标捕获,请 Mouse.Captureelement 提供的参数调用 nullTo clear mouse capture from all elements, call Mouse.Capture with the element parameter provided as null.