WPF类库【FastHotKeyForWPF】在【Version 1.1.5】中更新了哪些内容?这个类库如何让你在WPF快速构建全局热键相关功能?

目录

目录

概述

快速入手【Version 1.1.5】

代码示例

XAML部分

 C#部分

代码解释

界面

 功能演示

 【补档1】做了圆角效果的设置界面Demo

代码示例

XAML部分

C#部分

效果图

总结 

下版本预告


概述

Ⅰ 修复过往版本(已启用)中,无法使用Protect()来锁定指定组件的Bug

Ⅱ 修复过往版本中,鼠标离开组件后,组件仍然在接收用户输入的Bug

Ⅲ 新增组件与组件之间的自动通信机制,将自动识别重复的组合键(新的覆盖旧的),只要是类库中,继承KeyBox的组件之间,都能实现这种通信

快速入手【Version 1.1.5】=> 前往Bilibili看视频效果演示

代码示例

XAML部分

<Window x:Class="TestDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="470" Width="800">
    <Window.Resources>
        <!--这是你自定义的资源样式,后续可以为组件使用样式中定义的属性-->
        <Style x:Key="MyBox" TargetType="TextBox">
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="BorderBrush" Value="Red"/>
        </Style>
    </Window.Resources>
    <Viewbox>
        <Grid Height="450" Width="800">
            <!--这两个Border是用来给预制组件k1与k2定位的-->
            <Border x:Name="Box1" Margin="37,27,540,373" Height="50"/>
            <Border x:Name="Box2" Margin="297,27,280,373" Height="50"/>
            
            <!--这两个Border是用来给预制组件k3与k4定位的-->
            <Border x:Name="Box3" Margin="37,133,540,267" Height="50"/>
            <Border x:Name="Box4" Margin="297,133,280,267" Height="50"/>
            
            <!--这个Border是用来给预制组件k5定位的-->
            <Border x:Name="Box5" Margin="37,251,280,149" Height="50"/>

            <!--这个Border是用来给预制组件k6定位的-->
            <Border x:Name="Box6" Margin="37,347,280,53" Height="50"/>
        </Grid>
    </Viewbox>
</Window>

 C#部分

using FastHotKeyForWPF;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestDemo
{
    public partial class MainWindow : Window
    {
        private static ComponentInfo info = new ComponentInfo(20, Brushes.Black, Brushes.Wheat, new Thickness());
        //假设它们的字体大小颜色、背景色、Margin是相同的

        KeySelectBox k1 = PrefabComponent.GetComponent<KeySelectBox>(info);
        KeySelectBox k2 = PrefabComponent.GetComponent<KeySelectBox>(info);
        //这两个组件负责接管函数TestA

        KeySelectBox k3 = PrefabComponent.GetComponent<KeySelectBox>(info);
        KeySelectBox k4 = PrefabComponent.GetComponent<KeySelectBox>(info);
        //这两个组件负责接管函数TestB

        KeysSelectBox k5 = PrefabComponent.GetComponent<KeysSelectBox>(info);
        //这个组件负责接管函数TestC

        KeysSelectBox k6 = PrefabComponent.GetComponent<KeysSelectBox>(info);
        //这个组件负责接管函数TestD

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            GlobalHotKey.Awake();
            //激活功能

            GlobalHotKey.IsDeBug = true;
            //调试模式会打印部分过程值,默认关闭

            Box1.Child = k1;
            Box2.Child = k2;
            Box3.Child = k3;
            Box4.Child = k4;
            Box5.Child = k5;
            Box6.Child = k6;
            //将组件设置为容器的子元素

            k1.UseFatherSize<Border>();
            k2.UseFatherSize<Border>();
            k3.UseFatherSize<Border>();
            k4.UseFatherSize<Border>();
            k5.UseFatherSize<Border>();
            k6.UseFatherSize<Border>();
            //令组件的宽高、字体大小与父容器相适应

            string[] target = new string[] { "BorderThickness", "BorderBrush" };
            k1.UseStyleProperty("MyBox", target);
            k2.UseStyleProperty("MyBox", target);
            k3.UseStyleProperty("MyBox", target);
            k4.UseStyleProperty("MyBox", target);
            k5.UseStyleProperty("MyBox", target);
            k6.UseStyleProperty("MyBox", target);
            //为组件应用指定样式中的指定属性

            BindingRef.Connect(k1, k2, TestA);
            BindingRef.Connect(k3, k4, TestB);
            //令两个KeySelectBox接管指定函数

            BindingRef.Connect(k5, TestC);
            BindingRef.Connect(k6, TestD);
            //令KeysSelectBox接管指定函数

            GlobalHotKey.IsUpdate = true;
            //设为false会关闭监测返回值功能,默认打开

            BindingRef.BindingAutoEvent(WhileReceiveValue);
            //监测到返回值自动触发指定函数

            k1.Protect();
            k1.UnProtect();
            //锁定与解锁一个KeySelectBox组件的按键接收功能,KeysSelectBox组件也可以像这样锁定

            PrefabComponent.ProtectSelectBox<KeySelectBox>();
            PrefabComponent.UnProtectSelectBox<KeySelectBox>();
            //这是直接锁定与解锁指定类型的所有组件,优先级高于Protect()与UnProtect()

            base.OnSourceInitialized(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            GlobalHotKey.Destroy();
            //关闭功能

            base.OnClosed(e);
        }

        //k1与k2接管此函数
        private void TestA()
        {
            MessageBox.Show("测试A");
        }

        //k3与k4接管此函数
        private object TestB()
        {
            return "测试BB";
        }

        //k5接管此函数
        private void TestC()
        {
            MessageBox.Show("测试CCC");
        }

        //k6接管此函数
        private void TestD()
        {
            MessageBox.Show("测试DDDD");
        }

        //监测到返回值时发生的事件 
        private void WhileReceiveValue()
        {
            MessageBox.Show(BindingRef.Value.ToString());
        }
    }
}

代码解释

界面

它们构筑了如下所示的快捷键设置页面,每一行都接管了一个函数,由上而下,分别是TestA\B\C\D

 功能演示

鼠标位于Box内,就可以通过按键来设置这个Box的Key,当满足一个组合键的格式时,自动注册热键,例如第一行的两个Box是k1和k2,它们接管函数TestA(以此类推B/C/D……)

 如果组合键重复了会怎样?答案是:自动清除重复的,以最新接收到的为准,注册新的热键,例如这里就把第一行的热键删除了,CTRL+A现在接管的是TestC函数

 【补档1】做了圆角效果的设置界面Demo

代码示例

XAML部分

<Window x:Class="TestDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="470" Width="800">
    <Window.Resources>
        <!--这是你自定义的资源样式,后续可以为组件使用样式中定义的属性-->
        <Style x:Key="MyBox" TargetType="TextBox">
            <Setter Property="FontSize" Value="36"/>
            <Setter Property="Foreground" Value="Cyan"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="BorderBrush" Value="Red"/>
        </Style>
        <Style x:Key="MyBorderA" TargetType="Border">
            <Setter Property="Background" Value="#29353d"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="CornerRadius" Value="10"/>
        </Style>
        <Style x:Key="MyBorderB" TargetType="Border">
            <Setter Property="Background" Value="#29353d"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="CornerRadius" Value="10"/>
        </Style>
    </Window.Resources>
    <Viewbox>
        <Grid Height="450" Width="800">
            <!--这两个Border是用来给预制组件KeySelectBox定位的-->
            <Border x:Name="Box1" Margin="174,131,404,269" Style="{StaticResource MyBorderA}"/>
            <Border x:Name="Box2" Margin="434,131,144,269" Style="{StaticResource MyBorderA}"/>

            <!--这两个Border是用来给预制组件KeySelectBox定位的-->
            <Border x:Name="Box3" Margin="174,202,404,198" Style="{StaticResource MyBorderA}"/>
            <Border x:Name="Box4" Margin="434,202,144,198" Style="{StaticResource MyBorderA}"/>

            <!--这个Border是用来给预制组件KeysSelectBox定位的-->
            <Border x:Name="Box5" Margin="174,277,144,123" Style="{StaticResource MyBorderB}"/>
            <!--这个Border是用来给预制组件KeysSelectBox定位的-->
            <Border x:Name="Box6" Margin="174,349,144,51" Style="{StaticResource MyBorderB}"/>

            <TextBlock Margin="28,130,569,270" Text="测试A" FontSize="40"/>
            <TextBlock Margin="28,200,569,200" Text="测试B" FontSize="40"/>
            <TextBlock Margin="28,276,569,124" Text="测试C" FontSize="40"/>
            <TextBlock Margin="28,348,569,52" Text="测试D" FontSize="40"/>
            <TextBlock Margin="28,30,404,370" Text="设置 => 快捷方式" FontSize="40"/>
        </Grid>
    </Viewbox>
</Window>

C#部分

using FastHotKeyForWPF;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestDemo
{
    public partial class MainWindow : Window
    {
        private static ComponentInfo info = new ComponentInfo(20, Brushes.Black, Brushes.Wheat, new Thickness());
        //假设它们的字体大小颜色、背景色、Margin是相同的

        KeySelectBox k1 = PrefabComponent.GetComponent<KeySelectBox>(info);
        KeySelectBox k2 = PrefabComponent.GetComponent<KeySelectBox>(info);
        //这两个组件负责接管函数TestA

        KeySelectBox k3 = PrefabComponent.GetComponent<KeySelectBox>(info);
        KeySelectBox k4 = PrefabComponent.GetComponent<KeySelectBox>(info);
        //这两个组件负责接管函数TestB

        KeysSelectBox k5 = PrefabComponent.GetComponent<KeysSelectBox>(info);
        //这个组件负责接管函数TestC

        KeysSelectBox k6 = PrefabComponent.GetComponent<KeysSelectBox>(info);
        //这个组件负责接管函数TestD

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            GlobalHotKey.Awake();
            //激活功能

            GlobalHotKey.IsDeBug = true;
            //调试模式会打印部分过程值,默认关闭

            Box1.Child = k1;
            Box2.Child = k2;
            Box3.Child = k3;
            Box4.Child = k4;
            Box5.Child = k5;
            Box6.Child = k6;
            //将组件设置为容器的子元素

            k1.UseFatherSize<Border>();
            k2.UseFatherSize<Border>();
            k3.UseFatherSize<Border>();
            k4.UseFatherSize<Border>();
            k5.UseFatherSize<Border>();
            k6.UseFatherSize<Border>();
            //令组件的宽高、字体大小与父容器相适应

            k1.UseStyleProperty("MyBox");
            k2.UseStyleProperty("MyBox");
            k3.UseStyleProperty("MyBox");
            k4.UseStyleProperty("MyBox");
            k5.UseStyleProperty("MyBox");
            k6.UseStyleProperty("MyBox");
            k1.IsDefaultColorChange = false;
            k2.IsDefaultColorChange = false;
            k3.IsDefaultColorChange = false;
            k4.IsDefaultColorChange = false;
            k5.IsDefaultColorChange = false;
            k6.IsDefaultColorChange = false;
            //为组件应用指定样式中的指定属性

            BindingRef.Connect(k1, k2, TestA);
            BindingRef.Connect(k3, k4, TestB);
            //令两个KeySelectBox接管指定函数

            BindingRef.Connect(k5, TestC);
            BindingRef.Connect(k6, TestD);
            //令KeysSelectBox接管指定函数

            GlobalHotKey.IsUpdate = true;
            //设为false会关闭监测返回值功能,默认打开

            BindingRef.BindingAutoEvent(WhileReceiveValue);
            //监测到返回值自动触发指定函数

            k1.Protect();
            k1.UnProtect();
            //锁定与解锁一个KeySelectBox组件的按键接收功能,KeysSelectBox组件也可以像这样锁定

            PrefabComponent.ProtectSelectBox<KeySelectBox>();
            PrefabComponent.UnProtectSelectBox<KeySelectBox>();
            //这是直接锁定与解锁指定类型的所有组件,优先级高于Protect()与UnProtect()

            base.OnSourceInitialized(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            GlobalHotKey.Destroy();
            //关闭功能

            base.OnClosed(e);
        }

        //k1与k2接管此函数
        private void TestA()
        {
            MessageBox.Show("测试A");
        }

        //k3与k4接管此函数
        private object TestB()
        {
            return "测试BB";
        }

        //k5接管此函数
        private void TestC()
        {
            MessageBox.Show("测试CCC");
        }

        //k6接管此函数
        private void TestD()
        {
            MessageBox.Show("测试DDDD");
        }

        //监测到返回值时发生的事件 
        private void WhileReceiveValue()
        {
            MessageBox.Show(BindingRef.Value.ToString());
        }
    }
}

效果图

 搞定!组件功能就是这样提高开发效率的!

总结 

【Version 1.1.5】大幅度优化了组件的运作逻辑,现在它完全可用于构筑一个设置页面,这也是我最初开发它的时候所希望看到的!同时,由于之前的旧版差距太大,就只能直接下架NuGet了,直接用这一版本就好。

下版本预告

【Version 1.1.6】将直接支持圆角Box(新增组件),并支持修改Box的圆角值、背景色、字体颜色等属性

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值