1、鍵盤類和鍵盤事件編程
WPF提供了基礎的鍵盤類(System.Input.Keyboard類),該類提供與鍵盤相關的事件、方法和屬性,這些事件、方法和屬性提供有關鍵盤狀態的信息。Keyboard的事件也經過UIElement等XAML基元素類的事件向外提供。學習
對於鍵盤操做,其經常使用的事件有兩組:this
KeyDown事件和PreviewKeyDown事件:處理鍵盤鍵按下
KeyUp事件和PreviewKeyUp事件:處理鍵盤鍵擡起
其中KeyDown和KeyUp事件屬於冒泡路由事件,而PreviewKeyDown和PreviewKeyup屬於隧道路由事件。spa
爲了使元素可以接收鍵盤輸入,該元素必須可得到焦點。默認狀況下,大多數 UIElement 派生對象均可得到焦點。若是不是這樣,則要使元素可得到焦點,請將基元素上的 Focusable 屬性設置爲 true。像 StackPanel 和 Canvas 這樣的 Panel 類將 Focusable 的默認值設置爲 false。所以,對要獲取鍵盤焦點的這些對象而言,必須將 Focusable 設置爲 true。code
例如:在筆者的Notebook中有「靜音」、「增大音量」、「減少音量」這三個快捷鍵,在一個應用程序的窗體上處理這三個鍵的點擊能夠:orm
1: <Window x:Class="InputCommandAndFocus.Window1" 2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4: Title="Window1" Height="300" Width="480" 5: Focusable="True" PreviewKeyDown="Window_PreviewKeyDown"> 6: <Canvas> 7: <!-- ... --> 8: </Canvas> 9: </Window> 1: private void Window_PreviewKeyDown(object sender, KeyEventArgs e) 2: { 3: if (e.Key == Key.VolumeMute) 4: { 5: // 按下「靜音」鍵 6: txtMessage.Text = "Mute"; 7: e.Handled = true; 8: } 9: else if (e.Key == Key.VolumeUp) 10: { 11: // 按下「增大音量」鍵 12: txtMessage.Text = "Up"; 13: e.Handled = true; 14: } 15: else if (e.Key == Key.VolumeDown) 16: { 17: // 按下「減少音量」鍵 18: txtMessage.Text = "Down"; 19: e.Handled = true; 20: } 21: }
2、鼠標類和鼠標事件xml
WPF提供的System.Input.Mouse類提供與鼠標相關的事件、方法和屬性,這些事件、方法和屬性提供有關鼠標狀態的信息。與Keyboard類相似,其事件也經過UIElement等基元素向外提供。對象
其事件主要有如下幾組(每一個事件均包含XXX冒泡路由事件和PreviewXXX隧道路由事件)blog
MouseDown、MouseUp事件:處理鼠標鍵的按下與擡起
MouseEnter、MouseLeave、MouseMove:處理鼠標進入、離開控件及在控件上移動
MouseWheel:處理鼠標滾輪滾動
另外,對於鼠標位置的捕獲,使用Mouse類的GetPosition方法,其參數是一個UIElement,表示其鼠標位置基於哪個控件的座標系。事件
例如,對於一個矩形圖形,設置其鼠標的各類事件:
1: <Rectangle Canvas.Left="246" Canvas.Top="46" Height="118" 2: Name="mainRectangle" Stroke="Black" Width="200" Fill="White" 3: MouseEnter="mainRectangle_MouseEnter" MouseLeave="mainRectangle_MouseLeave" 4: MouseMove="mainRectangle_MouseMove" MouseDown="mainRectangle_MouseDown" 5: MouseWheel="mainRectangle_MouseWheel"/> 1: private void mainRectangle_MouseEnter(object sender, MouseEventArgs e) 2: { 3: // 鼠標進入控件時,控件的顏色爲紅色 4: mainRectangle.Fill = new SolidColorBrush(Colors.Red); 5: } 6: 7: private void mainRectangle_MouseLeave(object sender, MouseEventArgs e) 8: { 9: // 鼠標離開控件時,控件的顏色爲紅色 10: mainRectangle.Fill = new SolidColorBrush(Colors.White); 11: } 12: 13: private void mainRectangle_MouseMove(object sender, MouseEventArgs e) 14: { 15: // 獲取基於Rectangle的鼠標的座標 16: Point pointBaseRectangle = Mouse.GetPosition(mainRectangle); 17: txtMessage.Text = string.Format( 18: "Mouse Position (Base the Rectangle) is ({0},{1})", 19: pointBaseRectangle.X, pointBaseRectangle.Y); 20: 21: txtMessage.Text += "\r\n"; 22: 23: // 獲取基於窗體的鼠標的座標 24: Point pointBaseWindow = Mouse.GetPosition(this); 25: txtMessage.Text += string.Format( 26: "Mouse Position (Base the Window) is ({0},{1})", 27: pointBaseWindow.X, pointBaseWindow.Y); 28: } 29: 30: private void mainRectangle_MouseDown(object sender, MouseButtonEventArgs e) 31: { 32: // 獲取點出的鼠標的按鈕 33: MouseButton button = e.ChangedButton; 34: 35: txtMessage.Text += "\r\n"; 36: txtMessage.Text += string.Format( 37: " Mouse Button is {0}", button.ToString()); 38: } 39: 40: private void mainRectangle_MouseWheel(object sender, MouseWheelEventArgs e) 41: { 42: if (e.Delta > 0) 43: { 44: // 若是向上推進滾輪,圖形的寬度增長 45: rectangle1.Width++; 46: } 47: 48: if (e.Delta < 0) 49: { 50: // 若是向下推進滾輪,圖形的寬度減少 51: rectangle1.Width--; 52: } 53: }
3、焦點處理
在 WPF 中,有兩個與焦點有關的主要概念:鍵盤焦點和邏輯焦點。 鍵盤焦點指接收鍵盤輸入的元素,而邏輯焦點指焦點範圍中具備焦點的元素。
一、鍵盤焦點:
鍵盤焦點指當前正在接收鍵盤輸入的元素。 在整個桌面上,只能有一個具備鍵盤焦點的元素。 在 WPF 中,具備鍵盤焦點的元素會將 IsKeyboardFocused 設置爲 true。 Keyboard 類的靜態屬性 FocusedElement 獲取當前具備鍵盤焦點的元素。
爲了使元素可以獲取鍵盤焦點,基元素的 Focusable 和 IsVisible 屬性必須設置爲 true。 有些類(如 Panel 基類)默認狀況下將 Focusable 設置爲 false;所以,若是您但願此類元素可以獲取鍵盤焦點,必須將 Focusable 設置爲 true。
能夠經過用戶與 UI 交互(例如,按 Tab 鍵定位到某個元素或者在某些元素上單擊鼠標)來獲取鍵盤焦點。 還能夠經過使用 Keyboard 類的 Focus 方法,以編程方式獲取鍵盤焦點。 Focus 方法嘗試將鍵盤焦點給予指定的元素。 返回的元素是具備鍵盤焦點的元素,若是有舊的或新的焦點對象阻止請求,則具備鍵盤焦點的元素可能不是所請求的元素。
二、邏輯焦點
邏輯焦點指焦點範圍中的 FocusManager..::.FocusedElement。 焦點範圍是一個跟蹤其範圍內的 FocusedElement 的元素。 當鍵盤焦點離開焦點範圍時,焦點元素會失去鍵盤焦點,但保留邏輯焦點。 當鍵盤焦點返回到焦點範圍時,焦點元素會再次得到鍵盤焦點。 這使得鍵盤焦點能夠在多個焦點範圍之間切換,但確保了在焦點返回到焦點範圍時,焦點範圍中的焦點元素再次得到鍵盤焦點。
一個應用程序中能夠有多個具備邏輯焦點的元素,但在一個特定的焦點範圍中只能有一個具備邏輯焦點的元素。
GetFocusScope 返回指定元素的焦點範圍。
WPF 中默認狀況下即爲焦點範圍的類有 Window、MenuItem、ToolBar 和 ContextMenu。
GetFocusedElement 獲取指定焦點範圍的焦點元素。SetFocusedElement 設置指定焦點範圍中的焦點元素。SetFocusedElement 一般用於設置初始焦點元素。
三、鍵盤導航
當按下導航鍵之一時,KeyboardNavigation 類將負責實現默認鍵盤焦點導航。 導航鍵有:Tab、Shift+Tab、Ctrl+Tab、Ctrl+Shift+Tab、向上鍵、向下鍵、向左鍵和向右鍵。
能夠經過設置附加的 KeyboardNavigation 屬性 TabNavigation、ControlTabNavigation 和 DirectionalNavigation 來更改導航容器的導航行爲。 這些屬性是 KeyboardNavigationMode 類型,可能值有 Continue、Local、Contained、Cycle、Once 以及 None。 默認值是 Continue,這意味着元素不是導航容器。
四、焦點事件
與鍵盤焦點相關的事件有 PreviewGotKeyboardFocus、GotKeyboardFocus、PreviewLostKeyboardFocus 以及 LostKeyboardFocus。 這些事件定義爲 Keyboard 類的附加事件,但更便於做爲基元素類上的等效路由事件來訪問。
當元素獲取鍵盤焦點時,會引起 GotKeyboardFocus。當元素失去鍵盤焦點時,會引起 LostKeyboardFocus。 若是處理了 PreviewGotKeyboardFocus 事件或 PreviewLostKeyboardFocusEvent 事件,而且 Handled 設置爲 true,則焦點將不會改變。
PS:本文非原創,從其它處搬運過來供本身學習,在此感謝原做者