C#(Wpf)实现小键盘

简介: 原文:C#(Wpf)实现小键盘花了一天时间小键盘基本功能已完成,先看看效果图吧!              默认:             Shift:     Caps Lock:   Button style ...
原文: C#(Wpf)实现小键盘

花了一天时间小键盘基本功能已完成,先看看效果图吧!

             默认:

            Shift:

    Caps Lock:

 

Button style

<Style x:Key="KeyButton" TargetType="Button">
        <Setter Property="Background" Value="White"/>
        <Setter Property="Margin" Value="1,2"/>
        <Setter Property="Width" Value="24"/>
        <Setter Property="Height" Value="24"/>
        <Setter Property="BorderThickness" Value=".5"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" To="#47C9FB"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="#2BA8E8"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="#1097D7"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" To="#1097D7"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" To="#2BA8E8"/>
                                        <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundGradient" Storyboard.TargetProperty="(Rectangle.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" To="#47C9FB"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled" />
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused" />
                                <VisualState x:Name="Unfocused" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="Background"  Background="White" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}">
                            <Grid Background="{TemplateBinding Background}">
                                <Rectangle x:Name="BackgroundGradient" >
                                    <Rectangle.Fill>
                                        <LinearGradientBrush StartPoint=".5,0" EndPoint=".5,1">
                                            <GradientStop Color="White" Offset="0" />
                                            <GradientStop Color="White"  Offset="0.4" />
                                            <GradientStop Color="White" Offset="1" />
                                        </LinearGradientBrush>
                                    </Rectangle.Fill>
                                </Rectangle>
                            </Grid>
                        </Border>
                        <ContentPresenter
                              x:Name="contentPresenter"
                              Content="{TemplateBinding Content}"
                              ContentTemplate="{TemplateBinding ContentTemplate}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              Margin="{TemplateBinding Padding}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
View Code

Window style

<Style TargetType="{x:Type local:KeyBoard}">
        <Setter Property="Width" Value="360px"></Setter>
        <Setter Property="Height" Value="108px"></Setter>
        <Setter Property="AllowsTransparency" Value="true"/>
        <Setter Property="ResizeMode" Value="NoResize"/>
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:KeyBoard}">
                    <Border Background="#2092D8"
                            BorderBrush="#043150"
                            BorderThickness="1" CornerRadius="2">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                                <RowDefinition />
                                <RowDefinition /> 
                            </Grid.RowDefinitions>
                            <StackPanel Orientation="Horizontal">
                                <Button x:Name="btnNum2" Margin="10,2,1,2" Style="{StaticResource KeyButton}">2</Button>
                                <Button x:Name="btnNum3" Style="{StaticResource KeyButton}">3</Button>
                                <Button x:Name="btnNum4" Style="{StaticResource KeyButton}">4</Button>
                                <Button x:Name="btnNum5" Style="{StaticResource KeyButton}">5</Button>
                                <Button x:Name="btnNum6" Style="{StaticResource KeyButton}">6</Button>
                                <Button x:Name="btnNum7" Style="{StaticResource KeyButton}">7</Button>
                                <Button x:Name="btnNum8" Style="{StaticResource KeyButton}">8</Button>
                                <Button x:Name="btnNum9" Style="{StaticResource KeyButton}">9</Button>
                                <Button x:Name="btnNum0" Style="{StaticResource KeyButton}">0</Button>
                                <Button x:Name="btnDot" Style="{StaticResource KeyButton}">`</Button>
                                <Button x:Name="btnNum1" Style="{StaticResource KeyButton}">1</Button>
                                <Button x:Name="btnDelete" Style="{StaticResource KeyButton}" Width="45">&lt;-</Button>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Grid.Row="1">
                                <Button x:Name="btnShift" Margin="2,2,1,2" Style="{StaticResource KeyButton}" Width="30">Shift</Button>
                                <Button x:Name="btnBackslant" Style="{StaticResource KeyButton}">\</Button>
                                <Button x:Name="btnLParenthesis" Style="{StaticResource KeyButton}">[</Button>
                                <Button x:Name="btnRParenthesis" Style="{StaticResource KeyButton}">]</Button>
                                <Button x:Name="btnSemicolon" Style="{StaticResource KeyButton}">;</Button>
                                <Button x:Name="btnSQuotes" Style="{StaticResource KeyButton}">'</Button>
                                <Button x:Name="btnComma" Style="{StaticResource KeyButton}">,</Button>
                                <Button x:Name="btnPeriod" Style="{StaticResource KeyButton}">.</Button>
                                <Button x:Name="btnSlant" Style="{StaticResource KeyButton}">/</Button>
                                <Button x:Name="btnSub" Style="{StaticResource KeyButton}">-</Button>
                                <Button x:Name="btnEquip" Style="{StaticResource KeyButton}">=</Button>
                                <Button x:Name="btnCapsLock" Style="{StaticResource KeyButton}" Width="62">Caps Lock</Button>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Grid.Row="2">
                                <Button x:Name="btnj" Margin="10,2,1,2" Style="{StaticResource KeyButton}">j</Button>
                                <Button x:Name="btnk" Style="{StaticResource KeyButton}">k</Button>
                                <Button x:Name="btnl" Style="{StaticResource KeyButton}">l</Button>
                                <Button x:Name="btnm" Style="{StaticResource KeyButton}">m</Button>
                                <Button x:Name="btna" Style="{StaticResource KeyButton}">a</Button>
                                <Button x:Name="btnb" Style="{StaticResource KeyButton}">b</Button>
                                <Button x:Name="btnc" Style="{StaticResource KeyButton}">c</Button>
                                <Button x:Name="btnd" Style="{StaticResource KeyButton}">d</Button>
                                <Button x:Name="btne" Style="{StaticResource KeyButton}">e</Button>
                                <Button x:Name="btnf" Style="{StaticResource KeyButton}">f</Button>
                                <Button x:Name="btng" Style="{StaticResource KeyButton}">g</Button>
                                <Button x:Name="btnh" Style="{StaticResource KeyButton}">h</Button>
                                <Button x:Name="btni" Style="{StaticResource KeyButton}">i</Button>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Grid.Row="3">
                                <Button x:Name="btnp" Margin="10,2,1,2" Style="{StaticResource KeyButton}">p</Button>
                                <Button x:Name="btnq" Style="{StaticResource KeyButton}">q</Button>
                                <Button x:Name="btnr" Style="{StaticResource KeyButton}">r</Button>
                                <Button x:Name="btns" Style="{StaticResource KeyButton}">s</Button>
                                <Button x:Name="btnt" Style="{StaticResource KeyButton}">t</Button>
                                <Button x:Name="btnu" Style="{StaticResource KeyButton}">u</Button>
                                <Button x:Name="btnv" Style="{StaticResource KeyButton}">v</Button>
                                <Button x:Name="btnw" Style="{StaticResource KeyButton}">w</Button>
                                <Button x:Name="btnx" Style="{StaticResource KeyButton}">x</Button>
                                <Button x:Name="btny" Style="{StaticResource KeyButton}">y</Button>
                                <Button x:Name="btnz" Style="{StaticResource KeyButton}">z</Button>
                                <Button x:Name="btnn" Style="{StaticResource KeyButton}">n</Button>
                                <Button x:Name="btno" Style="{StaticResource KeyButton}">o</Button>
                            </StackPanel>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
View Code

存在的问题:还不能自动定位,弹出键盘需要手动控制,这些以后会解决的

周末愉快!

本文地址:http://www.cnblogs.com/liuxiaobo93/p/3915468.html 暮雨冰蓝

目录
相关文章
|
5月前
|
编译器 API C#
技术心得记录:深入分析C#键盘勾子(Hook)拦截器,屏蔽键盘活动的详解
技术心得记录:深入分析C#键盘勾子(Hook)拦截器,屏蔽键盘活动的详解
C# WPF 中 外部图标引入iconfont,无法正常显示问题 【小白记录】
本文介绍了在C# WPF应用程序中引入外部iconfont图标时可能遇到的显示问题及其解决方法:1) 检查资源路径和引入格式是否正确,确保字体文件引用格式为“#xxxx”,并正确指向字体文件位置;2) 确保图标资源被包含在程序集中,通过设置字体文件的生成操作为Resource(资源)来实现。
C# WPF 中 外部图标引入iconfont,无法正常显示问题 【小白记录】
|
2月前
|
编解码 C# 数据库
C# + WPF 音频播放器 界面优雅,体验良好
【9月更文挑战第18天】这是一个用 C# 和 WPF 实现的音频播放器示例,界面简洁美观,功能丰富。设计包括播放/暂停按钮、进度条、音量控制滑块、歌曲列表和专辑封面显示。功能实现涵盖音频播放、进度条控制、音量调节及歌曲列表管理。通过响应式设计、动画效果、快捷键支持和错误处理,提升用户体验。可根据需求扩展更多功能。
125 3
|
3月前
|
C#
C# WPF 将第三方DLL嵌入 exe
C# WPF 将第三方DLL嵌入 exe
82 0
|
3月前
|
前端开发 C# 容器
WPF/C#:实现导航功能
WPF/C#:实现导航功能
73 0
|
3月前
|
设计模式 测试技术 C#
WPF/C#:在WPF中如何实现依赖注入
WPF/C#:在WPF中如何实现依赖注入
75 0
|
3月前
|
前端开发 C# Windows
WPF/C#:如何实现拖拉元素
WPF/C#:如何实现拖拉元素
51 0
|
3月前
|
存储 C# 索引
WPF/C#:BusinessLayerValidation
WPF/C#:BusinessLayerValidation
34 0
|
3月前
|
C#
WPF/C#:数据绑定到方法
WPF/C#:数据绑定到方法
43 0
|
3月前
|
前端开发 测试技术 C#
WPF/C#:在DataGrid中显示选择框
WPF/C#:在DataGrid中显示选择框
62 0