ContentElement.CaptureMouse ContentElement.CaptureMouse ContentElement.CaptureMouse ContentElement.CaptureMouse Method


嘗試強制將滑鼠的捕捉給這個項目。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是一種技術,如果您不確定您要擷取的項目滑鼠是否會很有用UIElementContentElementCasting 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. 檢查是否IsEnabledtrue之前先呼叫CaptureMouseCheck whether IsEnabled is true before you call CaptureMouse.

如果呼叫CaptureMouse會傳回true,然後IsMouseCaptured也是trueIf calling CaptureMouse returns true, then IsMouseCaptured is also true.

如果呼叫CaptureMouse傳回true,則GotMouseCaptureIsMouseCapturedChanged引發事件,與RoutedEventArgs.Source事件中的項目報告資料其中CaptureMouse呼叫方法。If 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.Capture具有element提供的參數nullTo clear mouse capture from all elements, call Mouse.Capture with the element parameter provided as null.