MouseButtonEventHandler MouseButtonEventHandler MouseButtonEventHandler MouseButtonEventHandler Delegate

定義

代表將處理滑鼠按鈕相關路由事件的方法,例如 MouseLeftButtonDownRepresents the method that will handle mouse button related routed events, for example MouseLeftButtonDown.

public delegate void MouseButtonEventHandler(System::Object ^ sender, MouseButtonEventArgs ^ e);
public delegate void MouseButtonEventHandler(object sender, MouseButtonEventArgs e);
type MouseButtonEventHandler = delegate of obj * MouseButtonEventArgs -> unit
Public Delegate Sub MouseButtonEventHandler(sender As Object, e As MouseButtonEventArgs)

參數

sender
Object Object Object Object

附加事件處理常式的物件。The object where the event handler is attached.

繼承
MouseButtonEventHandlerMouseButtonEventHandlerMouseButtonEventHandlerMouseButtonEventHandler

範例

下列範例會建立MouseDown事件處理常式, 以變更事件來源的背景色彩。The following example creates a MouseDown event handler that changes the background color of the source of the event. 背景色彩取決於按下哪一個按鈕。The background color is determined by which button is pressed.

private void MouseButtonDownHandler(object sender, MouseButtonEventArgs e)
{
    Control src = e.Source as Control;

    if (src != null)
    {
        switch (e.ChangedButton)
        {
            case MouseButton.Left:
                src.Background = Brushes.Green;
                break;
            case MouseButton.Middle:
                src.Background = Brushes.Red;
                break;
            case MouseButton.Right:
                src.Background = Brushes.Yellow;
                break;
            case MouseButton.XButton1:
                src.Background = Brushes.Brown;
                break;
            case MouseButton.XButton2:
                src.Background = Brushes.Purple;
                break;
            default:
                break;
        }
    }
}
Private Sub MouseButtonDownHandler(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
    Dim src As Control = TryCast(e.Source, Control)

    If src IsNot Nothing Then
        Select Case e.ChangedButton
            Case MouseButton.Left
                src.Background = Brushes.Green
            Case MouseButton.Middle
                src.Background = Brushes.Red
            Case MouseButton.Right
                src.Background = Brushes.Yellow
            Case MouseButton.XButton1
                src.Background = Brushes.Brown
            Case MouseButton.XButton2
                src.Background = Brushes.Purple
            Case Else
        End Select
    End If
End Sub

備註

這個委派會與下列附加事件搭配使用。This delegate is used with the following attached events.

這個委派會與下列路由事件搭配使用。This delegate is used with the following routed events. 這些路由事件會轉送先前列出的附加事件, 讓中WPFWPF的一般元素模型更容易存取。These routed events forward the previously listed attached events to make them more accessible to the general element model in WPFWPF.

附加事件和基底元素路由事件會共用其事件資料, 而路由事件的反升和通道版本也會共用事件資料。The attached events and the base element routed events share their event data, and the bubbling and tunneling versions of the routed events also share event data. 這可能會影響事件在移動事件路由時的已處理特性。This can affect the handled characteristics of the event as it travels the event route. 如需詳細資訊, 請參閱輸入總覽For details, see Input Overview.

擴充方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。Gets an object that represents the method represented by the specified delegate.

適用於

另請參閱