MouseButton 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義值,以指定滑鼠裝置上的按鍵。
public enum class MouseButton
public enum MouseButton
type MouseButton =
Public Enum MouseButton
- 繼承
欄位
Left | 0 | 滑鼠左鍵。 |
Middle | 1 | 滑鼠中鍵。 |
Right | 2 | 滑鼠右鍵。 |
XButton1 | 3 | 第一個延伸的滑鼠鍵。 |
XButton2 | 4 | 第二個延伸的滑鼠鍵。 |
範例
下列範例會建立事件處理程式,這個事件處理程式會使用 MouseButton 列舉來判斷按下哪一個MouseDown按鈕。 根據按下的按鈕,引發事件的控件背景會變更。
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