|
Posted on 2010-11-29 16:55 Hero 阅读(666) 评论(0) 编辑 收藏 引用 所属分类: C#积累
1 using System; 2 using System.Windows; 3 using System.Windows.Controls; 4 using System.Windows.Input; 5 using System.Windows.Media; 6![](http://www.cppblog.com/Images/OutliningIndicators/None.gif) 7 namespace Petzold.CommandTheMenu 8![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) { 9 public class CommandTheMenu : Window 10![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 11 TextBlock text; 12![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 13 [STAThread] 14 public static void Main() 15![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 16 Application app = new Application(); 17 app.Run(new CommandTheMenu()); 18 } 19 public CommandTheMenu() 20![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 21 Title = "Command the Menu"; 22![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 23 // Create DockPanel. 24 DockPanel dock = new DockPanel(); 25 Content = dock; 26![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 27 // Create Menu docked at top. 28 Menu menu = new Menu(); 29 dock.Children.Add(menu); 30 DockPanel.SetDock(menu, Dock.Top); 31![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 32 // Create TextBlock filling the rest. 33 text = new TextBlock(); 34 text.Text = "Sample clipboard text"; 35 text.HorizontalAlignment = HorizontalAlignment.Center; 36 text.VerticalAlignment = VerticalAlignment.Center; 37 text.FontSize = 32; // ie, 24 points 38 text.TextWrapping = TextWrapping.Wrap; 39 dock.Children.Add(text); 40![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 41 // Create Edit menu. 42 MenuItem itemEdit = new MenuItem(); 43 itemEdit.Header = "_Edit"; 44 menu.Items.Add(itemEdit); 45![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 46 // Create items on Edit menu. 47 MenuItem itemCut = new MenuItem(); 48 itemCut.Header = "Cu_t"; 49 itemCut.Command = ApplicationCommands.Cut; 50 itemEdit.Items.Add(itemCut); 51![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 52 MenuItem itemCopy = new MenuItem(); 53 itemCopy.Header = "_Copy"; 54 itemCopy.Command = ApplicationCommands.Copy; 55 itemEdit.Items.Add(itemCopy); 56![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 57 MenuItem itemPaste = new MenuItem(); 58 itemPaste.Header = "_Paste"; 59 itemPaste.Command = ApplicationCommands.Paste; 60 itemEdit.Items.Add(itemPaste); 61![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 62 MenuItem itemDelete = new MenuItem(); 63 itemDelete.Header = "_Delete"; 64 itemDelete.Command = ApplicationCommands.Delete; 65 itemEdit.Items.Add(itemDelete); 66![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 67 // Add command bindings to window collection. 68 CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, 69 CutOnExecute, CutCanExecute)); 70 CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, 71 CopyOnExecute, CutCanExecute)); 72 CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, 73 PasteOnExecute, PasteCanExecute)); 74 CommandBindings.Add(new CommandBinding(ApplicationCommands.Delete, 75 DeleteOnExecute, CutCanExecute)); 76![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 77![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 78![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 79![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif) 添加自定义键盘快捷键#region 添加自定义键盘快捷键 80 InputGestureCollection restoreGestures = new InputGestureCollection(); 81 restoreGestures.Add( new KeyGesture(Key.R, ModifierKeys.Control));//CTRL + R 82 //创建Command,绑定快捷键 83 RoutedUICommand restoreUICommand = new RoutedUICommand("_Restore", "Restore", this.GetType(), restoreGestures); 84 //RoutedCommand restoreUICommand = new RoutedUICommand( "_Restore", "Restore", this.GetType(), restoreGestures ); 85 //添加到当前窗口的CommandBindings集合中 86 this.CommandBindings.Add( new CommandBinding(restoreUICommand, RestoreExecute) ); 87 //添加菜单,绑定到自定义的命令中 88 MenuItem itemRestore = new MenuItem(); 89 itemRestore.Command = restoreUICommand; 90 itemEdit.Items.Add(itemRestore); 91 #endregion 92 } 93![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 94 void RestoreExecute( object sender, ExecutedRoutedEventArgs e ) 95![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 96 this.text.Text = "John"; 97 } 98![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif) 99 void CutCanExecute(object sender, CanExecuteRoutedEventArgs args) 100![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 101 args.CanExecute = text.Text != null && text.Text.Length > 0; 102 } 103 void PasteCanExecute(object sender, CanExecuteRoutedEventArgs args) 104![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 105 args.CanExecute = Clipboard.ContainsText(); 106 } 107 void CutOnExecute(object sender, ExecutedRoutedEventArgs args) 108![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 109 ApplicationCommands.Copy.Execute( null, this ); 110 ApplicationCommands.Delete.Execute(null, this); 111 } 112 void CopyOnExecute(object sender, ExecutedRoutedEventArgs args) 113![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 114 Clipboard.SetText(text.Text); 115 } 116 void PasteOnExecute(object sender, ExecutedRoutedEventArgs args) 117![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 118 text.Text = Clipboard.GetText(); 119 } 120 void DeleteOnExecute(object sender, ExecutedRoutedEventArgs args) 121![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) { 122 text.Text = null; 123 } 124 } 125 } 126![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|