MouseButtonEventHandler 委托

定义

表示将处理与鼠标按钮相关的路由事件的方法,例如 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

事件处理程序所附加到的对象。The object where the event handler is attached.

e
MouseButtonEventArgs

事件数据。The event data.

继承
MouseButtonEventHandler

示例

下面的示例创建一个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)

获取指示指定委托表示的方法的对象。Gets an object that represents the method represented by the specified delegate.

适用于

另请参阅