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. 請注意, 此範例會將潛在的 capture 目標元素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是或ContentElement, 則轉換成是很有用的技術。Casting 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 執行, 而不需要將試用轉換UIElementContentElement或。The 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在呼叫CaptureMouse之前。Check whether IsEnabled is true before you call CaptureMouse.

如果呼叫CaptureMouse true傳回,IsMouseCaptured則也true會是。If calling CaptureMouse returns true, then IsMouseCaptured is also true.

如果呼叫CaptureMouse RoutedEventArgs.Source CaptureMouse IsMouseCapturedChanged傳回, 則會引發和事件,並在事件資料中回報為呼叫方法的元素。GotMouseCapture trueIf 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. 如果您強制 capture, 可能會干擾現有的捕捉, 特別是與滑鼠拖放相關的捕捉。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提供null的參數呼叫。To clear mouse capture from all elements, call Mouse.Capture with the element parameter provided as null.