Commit 6f7d995a by liulongfei

热键输入框

parent a51c3e62
......@@ -10,6 +10,7 @@
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/NumberBox/NumberBox.xaml"></ResourceDictionary>
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/ColorPickButton/ColorPickButton.xaml"></ResourceDictionary>
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/LabelValue/LabelValue.xaml"></ResourceDictionary>
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/HotkeyBox/HotkeyBox.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
......@@ -138,6 +138,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Widgets\HotkeyBox\HotkeyBox.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Widgets\LabelValue\LabelValue.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
......@@ -233,6 +237,9 @@
<Compile Include="Widgets\ColorPickButton\ColorPickButton.cs" />
<Compile Include="Widgets\DebugBorder\DebugBorder.cs" />
<Compile Include="Widgets\DragWindowBar\DragWindowBar.cs" />
<Compile Include="Widgets\HotkeyBox\HotkeyBox.cs" />
<Compile Include="Widgets\HotkeyBox\HotkeyHelper.cs" />
<Compile Include="Widgets\HotkeyBox\HotkeyPropertyAttribute.cs" />
<Compile Include="Widgets\LabelValue\LabelValue.cs" />
<Compile Include="Widgets\NavigationControl\NavigationConfig.cs" />
<Compile Include="Widgets\NavigationControl\NavigationControl.cs" />
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace VIZ.Framework.Common
{
/// <summary>
/// 热键输入框
/// </summary>
[TemplatePart(Name = nameof(PART_TextBox), Type = typeof(TextBox))]
public class HotkeyBox : Control
{
static HotkeyBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HotkeyBox), new FrameworkPropertyMetadata(typeof(HotkeyBox)));
}
/// <summary>
/// 输入框
/// </summary>
private TextBox PART_TextBox;
#region Hotkey -- 热键
/// <summary>
/// 热键
/// </summary>
public string Hotkey
{
get { return (string)GetValue(HotkeyProperty); }
set { SetValue(HotkeyProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for Hotkey. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty HotkeyProperty =
DependencyProperty.Register("Hotkey", typeof(string), typeof(HotkeyBox), new PropertyMetadata(null));
#endregion
/// <summary>
/// 应用模板
/// </summary>
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this.PART_TextBox = this.Template.FindName(nameof(this.PART_TextBox), this) as TextBox;
if (this.PART_TextBox == null)
return;
this.PART_TextBox.IsReadOnly = true;
this.PART_TextBox.PreviewKeyDown -= PART_TextBox_PreviewKeyDown;
this.PART_TextBox.PreviewKeyDown += PART_TextBox_PreviewKeyDown;
}
/// <summary>
/// 按钮按下
/// </summary>
private void PART_TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
this.Hotkey = HotkeyHelper.GetHotkey(e);
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:VIZ.Framework.Common">
<Style TargetType="{x:Type local:HotkeyBox}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:HotkeyBox}">
<TextBox x:Name="PART_TextBox" IsReadOnly="True" AcceptsReturn="False" TextWrapping="NoWrap" VerticalContentAlignment="Center"
Text="{TemplateBinding Hotkey}"></TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace VIZ.Framework.Common
{
/// <summary>
/// 热键辅助类
/// </summary>
public static class HotkeyHelper
{
/// <summary>
/// 获取热键值
/// </summary>
/// <param name="e">键盘事件</param>
/// <returns>热键值</returns>
public static string GetHotkey(KeyEventArgs e)
{
StringBuilder sb = new StringBuilder();
// Ctl Alt Shift
if (e.KeyboardDevice.IsKeyDown(Key.LeftCtrl) || e.KeyboardDevice.IsKeyDown(Key.RightCtrl))
{
sb.Append("Ctrl + ");
}
if (e.KeyboardDevice.IsKeyDown(Key.LeftAlt) || e.KeyboardDevice.IsKeyDown(Key.RightAlt))
{
sb.Append("Alt + ");
}
if (e.KeyboardDevice.IsKeyDown(Key.LeftShift) || e.KeyboardDevice.IsKeyDown(Key.RightShift))
{
sb.Append("Shift + ");
}
// D0 - D9 A - Z
for (int i = (int)Key.D0; i <= (int)Key.Z; i++)
{
Key key = (Key)i;
if (e.KeyboardDevice.IsKeyDown(key))
{
sb.Append($"{key}");
break;
}
}
// F1 - F12
for (int i = (int)Key.F1; i <= (int)Key.F12; i++)
{
Key key = (Key)i;
if (e.KeyboardDevice.IsKeyDown(key))
{
sb.Append($"{key}");
break;
}
}
return sb.ToString();
}
/// <summary>
/// 获取热键值
/// </summary>
/// <param name="e">键盘事件</param>
/// <returns>热键值</returns>
public static string GetHotkey(System.Windows.Forms.KeyEventArgs e)
{
StringBuilder sb = new StringBuilder();
if (e.Control)
{
sb.Append("Ctrl + ");
}
if (e.Alt)
{
sb.Append("Alt + ");
}
if (e.Shift)
{
sb.Append("Shift + ");
}
// D0 - D9
if (e.KeyCode >= System.Windows.Forms.Keys.D0 && e.KeyCode <= System.Windows.Forms.Keys.D9)
{
sb.Append($"{e.KeyCode}");
return sb.ToString();
}
// A - Z
if (e.KeyCode >= System.Windows.Forms.Keys.A && e.KeyCode <= System.Windows.Forms.Keys.Z)
{
sb.Append($"{e.KeyCode}");
return sb.ToString();
}
// F1 - F12
if (e.KeyCode >= System.Windows.Forms.Keys.F1 && e.KeyCode <= System.Windows.Forms.Keys.F12)
{
sb.Append($"{e.KeyCode}");
return sb.ToString();
}
return sb.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Common
{
/// <summary>
/// 热键属性
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class HotkeyPropertyAttribute : Attribute
{
}
}
<UserControl x:Class="VIZ.Framework.WpfTest.HotkeyBoxTest"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:VIZ.Framework.WpfTest"
xmlns:common="clr-namespace:VIZ.Framework.Common;assembly=VIZ.Framework.Common"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<common:HotkeyBox Width="400" Height="40"></common:HotkeyBox>
</Grid>
</UserControl>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace VIZ.Framework.WpfTest
{
/// <summary>
/// HotkeyBoxTest.xaml 的交互逻辑
/// </summary>
public partial class HotkeyBoxTest : UserControl
{
public HotkeyBoxTest()
{
InitializeComponent();
}
}
}
......@@ -8,6 +8,6 @@
mc:Ignorable="d" WindowStartupLocation="CenterScreen" WindowState="Maximized" WindowStyle="None"
Title="MainWindow" Height="800" Width="1200">
<Grid>
<local:NumberBoxTest></local:NumberBoxTest>
<local:HotkeyBoxTest></local:HotkeyBoxTest>
</Grid>
</Window>
......@@ -99,6 +99,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="HotkeyBoxTest\HotkeyBoxTest.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
......@@ -107,6 +111,9 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="HotkeyBoxTest\HotkeyBoxTest.xaml.cs">
<DependentUpon>HotkeyBoxTest.xaml</DependentUpon>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment