写字板实例一
MainWindow.xaml
<Window x:Class="Wordpad01.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WordPad1.0" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Menu Grid.Row="0"> <MenuItem Header="File" /> <MenuItem Header="Copy" /> <MenuItem Header="Paste" /> <MenuItem Header="Cut" /> <MenuItem Header="Delete" /> </Menu> <ToolBar Grid.Row="1"> <Button> <Image Source="/Images/Copy.png" /> </Button> <Button> <Image Source="/Images/Paste.png" /> </Button> <Button> <Image Source="/Images/Cut.png" /> </Button> <Button> <Image Source="/Images/Delete.png" /> </Button> </ToolBar> <TextBox x:Name="text" Grid.Row="2" Text="WordPad" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Grid> </Window>
主要依靠Clipboard类实现
为Button添加Click时间
<MenuItem Header="Copy" Click="CopyClick" />
<Button Click="CopyClick">
private void CopyClick(object sender, RoutedEventArgs e) { if (text.Text != null && text.Text.Length > 0) { Clipboard.SetText(text.Text); } } private void PasteClick(object sender, RoutedEventArgs e) { if (Clipboard.ContainsText()) { text.Text = Clipboard.GetText(); } } private void CutClick(object sender, RoutedEventArgs e) { CopyClick(sender, e); DeleteClick(sender, e); } private void DeleteClick(object sender, RoutedEventArgs e) { text.Text = null; }
添加右键菜单
<TextBox x:Name="text" Grid.Row="2" Text="WordPad" FontSize="30" TextWrapping="Wrap" Height="Auto" Width="Auto" Margin="5"> <TextBox.ContextMenu> <ContextMenu> <MenuItem Header="Copy" Click="CopyClick" /> <MenuItem Header="Paste" Click="PasteClick" /> <MenuItem Header="Cut" Click="CutClick" /> <MenuItem Header="Delete" Click="DeleteClick" /> </ContextMenu> </TextBox.ContextMenu></TextBox>
添加快捷键 KeyGesture
private KeyGesture gestCopy = new KeyGesture(Key.C, ModifierKeys.Control); private KeyGesture gestPaste = new KeyGesture(Key.V, ModifierKeys.Control); private KeyGesture gestCut = new KeyGesture(Key.X, ModifierKeys.Control); private KeyGesture gestDelete= new KeyGesture(Key.Delete); protected override void OnPreviewKeyDown(KeyEventArgs e) { base.OnPreviewKeyDown(e); e.Handled = true; if (gestCopy.Matches(null, e)) { CopyClick(this, e); } else if (gestPaste.Matches(null, e)) { PasteClick(this, e); } else if (gestCut.Matches(null, e)) { CutClick(this, e); } else if (gestDelete.Matches(null, e)) { DeleteClick(this, e); } }
添加状态控制
菜单栏
<MenuItem Header="File" SubmenuOpened="MenuItem_SubmenuOpened"/> <MenuItem x:Name="copyItem" Header="Copy" Click="CopyClick" /> <MenuItem x:Name="pasteItem" Header="Paste" Click="PasteClick" /> <MenuItem x:Name="cutItem" Header="Cut" Click="CutClick" /> <MenuItem x:Name="deleteItem" Header="Delete" Click="DeleteClick" />
private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e) { cutItem.IsEnabled = copyItem.IsEnabled = deleteItem.IsEnabled = text.Text != null && text.Text.Length > 0; pasteItem.IsEnabled = Clipboard.ContainsText();}
TextBox同理
<TextBox x:Name="text" ContextMenuOpening="text_ContextMenuOpening">
快捷键与工具栏同上
一个简易的写字板程序完成了,但是有没有发现很麻烦呢,下一期更新WordPad2.0版
To be continue...
时间: 2024-11-05 00:42:16