MouseButton 列舉

定義

定義值,以指定滑鼠裝置上的按鍵。Defines values that specify the buttons on a mouse device.

public enum class MouseButton
public enum MouseButton
type MouseButton = 
Public Enum MouseButton
繼承
MouseButton

欄位

Left 0

滑鼠左鍵。The left mouse button.

Middle 1

滑鼠中鍵。The middle mouse button.

Right 2

滑鼠右鍵。The right mouse button.

XButton1 3

第一個延伸的滑鼠鍵。The first extended mouse button.

XButton2 4

第二個延伸的滑鼠鍵。The second extended mouse button.

範例

下列範例會建立一個MouseDown事件處理常式, 以MouseButton使用列舉來判斷所按的按鈕。The following example creates a MouseDown event handler that uses the MouseButton enumeration to determine which button was pressed. 根據按下的按鈕, 引發事件之控制項的背景會變更。Depending on which button was pressed, the background of the control that fired the event is changed.

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

適用於

另請參閱