日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

WPF学习之路(五) 实例:写字板

發布時間:2023/12/13 综合教程 25 生活家
生活随笔 收集整理的這篇文章主要介紹了 WPF学习之路(五) 实例:写字板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫字板實例一

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...

總結

以上是生活随笔為你收集整理的WPF学习之路(五) 实例:写字板的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。