ContentElement.CaptureMouse 메서드

정의

마우스를 이 요소에 강제로 캡처하려고 시도합니다.

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

반품

마우스를 성공적으로 캡처하면

구현

예제

다음 예제에서는 마우스가 요소에 의해 이미 캡처되었는지 여부에 따라 마우스 또는 릴리스 캡처를 캡처합니다. 이 예제에서는 잠재적 캡처 대상 요소를 인터페이스로 IInputElement 캐스팅하므로 처음에 메서드를 호출합니다 IInputElement.CaptureMouse . IInputElement 캐스팅은 마우스를 캡처하려는 요소가 a 또는 UIElementa인지 확실하지 않은 경우에 유용한 기술입니다ContentElement. 그런 다음 인터페이스 캐스트 및 인터페이스 메서드 호출은 평가판 캐스팅을 요구하지 않고 내부적으로 적절한 형식별 CaptureMouse 구현을 UIElement 호출합니다 ContentElement. 이 동일한 캐스팅 기술은 예를 들어 많은 입력 관련 이벤트 및 기타 입력 관련 메서드를 정의하는 다른 멤버 IInputElement 에 대해 작동합니다.

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)
    {
      Mouse.Capture(target);
    } else {
      Mouse.Capture(null);
    }
  }
}
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
      Mouse.Capture(target)
    Else
      Mouse.Capture(Nothing)
    End If
  End If
End Sub

설명

캡처하려면 요소를 사용하도록 설정해야 합니다. 를 호출IsEnabled하기 전인지 확인 trueCaptureMouse 합니다.

호출 CaptureMouse 이 반환true되면 .IsMouseCapturedtrue

호출 CaptureMouse 이 반환true되면 메서드가 GotMouseCapture 호출되는 요소 IsMouseCapturedChanged 로 보고된 이벤트 데이터와 함께 RoutedEventArgs.Source 이벤트 및 CaptureMouse 이벤트가 발생합니다. 캡처를 강제로 적용하면 기존 캡처를 방해할 수 있습니다. 특히 마우스로 끌어서 놓기와 관련된 캡처를 방해할 수 있습니다.

모든 요소에서 마우스 캡처를 지우려면 다음과 같이 Mouse.Capture제공된 매개 변수를 element 사용하여 호출 null 합니다.

적용 대상

추가 정보