一、鍵盤類和鍵盤事件
WPF提供了基礎的鍵盤類(System.Input.Keyboard類),該類提供與鍵盤相關的事件、方法和屬性,這些事件、方法和屬性提供有關鍵盤狀態的資訊,Keyboard的事件也通過UIElement等XAML基元素類的事件向外提供,
對于鍵盤操作,其常用的事件有兩組:
KeyDown事件和PreviewKeyDown事件:處理鍵盤鍵按下
KeyUp事件和PreviewKeyUp事件:處理鍵盤鍵抬起
其中KeyDown和KeyUp事件屬于冒泡路由事件,而PreviewKeyDown和PreviewKeyup屬于隧道路由事件,
為了使元素能夠接收鍵盤輸入,該元素必須可獲得焦點,默認情況下,大多數 UIElement 派生物件都可獲得焦點,如果不是這樣,則要使元素可獲得焦點,請將基元素上的 Focusable 屬性設定為 true,像 StackPanel 和 Canvas 這樣的 Panel 類將 Focusable 的默認值設定為 false,因此,對要獲取鍵盤焦點的這些物件而言,必須將 Focusable 設定為 true,
例如:在筆者的Notebook中有“靜音”、“增大音量”、“減小音量”這三個快捷鍵,在一個應用程式的表單上處理這三個鍵的點擊可以:
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: }
二、滑鼠類和滑鼠事件
WPF提供的System.Input.Mouse類提供與滑鼠相關的事件、方法和屬性,這些事件、方法和屬性提供有關滑鼠狀態的資訊,與Keyboard類類似,其事件也通過UIElement等基元素向外提供,
其事件主要有以下幾組(每個事件均包含XXX冒泡路由事件和PreviewXXX隧道路由事件)
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: }
三、焦點處理
在 WPF 中,有兩個與焦點有關的主要概念:鍵盤焦點和邏輯焦點, 鍵盤焦點指接收鍵盤輸入的元素,而邏輯焦點指焦點范圍中具有焦點的元素,
1、鍵盤焦點:
鍵盤焦點指當前正在接收鍵盤輸入的元素, 在整個桌面上,只能有一個具有鍵盤焦點的元素, 在 WPF 中,具有鍵盤焦點的元素會將 IsKeyboardFocused 設定為 true, Keyboard 類的靜態屬性 FocusedElement 獲取當前具有鍵盤焦點的元素,
為了使元素能夠獲取鍵盤焦點,基元素的 Focusable 和 IsVisible 屬性必須設定為 true, 有些類(如 Panel 基類)默認情況下將 Focusable 設定為 false;因此,如果您希望此類元素能夠獲取鍵盤焦點,必須將 Focusable 設定為 true,
可以通過用戶與 UI 互動(例如,按 Tab 鍵定位到某個元素或者在某些元素上單擊滑鼠)來獲取鍵盤焦點, 還可以通過使用 Keyboard 類的 Focus 方法,以編程方式獲取鍵盤焦點, Focus 方法嘗試將鍵盤焦點給予指定的元素, 回傳的元素是具有鍵盤焦點的元素,如果有舊的或新的焦點物件阻止請求,則具有鍵盤焦點的元素可能不是所請求的元素,
2、邏輯焦點
邏輯焦點指焦點范圍中的 FocusManager..::.FocusedElement, 焦點范圍是一個跟蹤其范圍內的 FocusedElement 的元素, 當鍵盤焦點離開焦點范圍時,焦點元素會失去鍵盤焦點,但保留邏輯焦點, 當鍵盤焦點回傳到焦點范圍時,焦點元素會再次獲得鍵盤焦點, 這使得鍵盤焦點可以在多個焦點范圍之間切換,但確保了在焦點回傳到焦點范圍時,焦點范圍中的焦點元素再次獲得鍵盤焦點,
一個應用程式中可以有多個具有邏輯焦點的元素,但在一個特定的焦點范圍中只能有一個具有邏輯焦點的元素,
GetFocusScope 回傳指定元素的焦點范圍,
WPF 中默認情況下即為焦點范圍的類有 Window、MenuItem、ToolBar 和 ContextMenu,
GetFocusedElement 獲取指定焦點范圍的焦點元素,SetFocusedElement 設定指定焦點范圍中的焦點元素,SetFocusedElement 通常用于設定初始焦點元素,
3、鍵盤導航
當按下導航鍵之一時,KeyboardNavigation 類將負責實作默認鍵盤焦點導航, 導航鍵有:Tab、Shift+Tab、Ctrl+Tab、Ctrl+Shift+Tab、向上鍵、向下鍵、向左鍵和向右鍵,
可以通過設定附加的 KeyboardNavigation 屬性 TabNavigation、ControlTabNavigation 和 DirectionalNavigation 來更改導航容器的導航行為, 這些屬性是 KeyboardNavigationMode 型別,可能值有 Continue、Local、Contained、Cycle、Once 以及 None, 默認值是 Continue,這意味著元素不是導航容器,
4、焦點事件
與鍵盤焦點相關的事件有 PreviewGotKeyboardFocus、GotKeyboardFocus、PreviewLostKeyboardFocus 以及 LostKeyboardFocus, 這些事件定義為 Keyboard 類的附加事件,但更便于作為基元素類上的等效路由事件來訪問,
當元素獲取鍵盤焦點時,會引發 GotKeyboardFocus,當元素失去鍵盤焦點時,會引發 LostKeyboardFocus, 如果處理了 PreviewGotKeyboardFocus 事件或 PreviewLostKeyboardFocusEvent 事件,并且 Handled 設定為 true,則焦點將不會改變,
PS:本文非原創,從其它處搬運過來供自己學習,在此感謝原作者
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/19351.html
標籤:WPF
