WPF 应用程序:系统托盘集成与快捷键唤醒及自定义

最小化应用程序时,将应用程序置于系统托盘,并能够自定义快捷键快速打开应用程序,是很多App必备的功能。

在此整理了一下WPF具体实现的过程。

效果
  • 最小化应用程序时,将应用程序置于系统托盘,并能够自定义快捷键快速打开应用程序
    在这里插入图片描述

  • 修改注册快捷键

    在这里插入图片描述

步骤拆分

1. 应用程序显示在系统托盘:

可以使用Hardcodet.NotifyIcon.Wpf 项目提供了WPF应用程序在系统托盘的功能

这是一个用于 WPF 平台的系统托盘图标(也称为通知区域图标)控件。利用 WPF 框架的多个功能来显示丰富的工具提示、弹出窗口、上下文菜单和气泡消息。

这里只用到它的显示托盘图标,托盘图标的单击命令。

实现步骤
  1. 添加引用:首先,需要将 Hardcodet.NotifyIcon.Wpf 库添加到项目中。可以通过 NuGet 包管理器搜索并安装。
  2. 配置 Application Resources:在 App.xaml 中配置系统托盘图标,指定图标资源和单击命令。
<Application.Resources>
    <tb:TaskbarIcon
        x:Key="Taskbar"
        IconSource="/Assets/Nita.ico"
        LeftClickCommand="{Binding ShowMainWindowCommand}"
        ToolTipText="你好!可点击系统托盘或按下Alt+O再次打开窗口" />
</Application.Resources>
  1. 初始化 TaskbarIcon:在 App 类的 OnStartup 方法中初始化 TaskbarIcon 并设置其数据上下文。
protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);
    ShowMainWindowCommand = new ShowMainWindowCommand();
    TaskbarIcon = (TaskbarIcon)FindResource("Taskbar");
    TaskbarIcon.DataContext = this;
}
  1. 定义 ICommand:创建一个 ICommand 实现,用于处理托盘图标的单击事件,将窗口状态设置为正常并显示。
public class ShowMainWindowCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter) => true;

    public void Execute(object parameter)
    {
         Application.Current.MainWindow.WindowState = WindowState.Normal;
    }
}
2. WPF 注册快捷键:FastHotKeyForWPF

FastHotKeyForWPF 是一个用于 WPF 应用程序的全局快捷键管理库。

这个库的主要功能是帮助开发者在 WPF 应用中优雅地管理全局快捷键,提供了丰富的 API 和组件来简化快捷键的注册、管理和使用。

因为我用的是.NET 6 所以,使用了FastHotKeyForWPF_NET6 这个版本。

GlobalHotKey.Awake(); // 激活全局热键功能
GlobalHotKey.Add(ModelKeys.CTRL, NormalKeys.F1, TestA); // 注册热键

if (GlobalHotKey.IsHotKeyProtected(modifier, key))
{
    MessageBox.Show("快捷键已被占用,请重新选择!");
    return;
}
GlobalHotKey.DeleteByKeys(currentModifier, currentKey); // 移除旧的快捷键
GlobalHotKey.Add(modifier, key, OpenWindow); // 注册新的快捷键




3.识别用户输入多按键组合

TextBox 监听KeyDown 和KeyUp事件,实现识别用户输入的快捷键

效果:用户按下按键时,清空原文本内容;识别用户按下的按键,用户抬起按键时,自动设置快捷键并提示设置成功。如果已经存在该快捷键则提示已被占用不能使用。

这段代码的主要功能是实现一个文本框,用户可以在其中输入快捷键组合,然后将这个快捷键组合保存下来。具体实现逻辑如下:

  1. 定义一个keysPressedList列表,用于存储 KeyDown 和KeyUp事件之间,用户同属输入的按键组合。
  2. 定义两个枚举变量currentModifiercurrentKey,分别表示当前的修饰键(如Alt、Ctrl等)和普通键(如O、P等)。
  3. TextBox_KeyDown方法中,当用户按下键盘上的任意键时,首先清空文本框和keysPressedList列表,表示重新设置快捷键。然后获取当前按键的字符串表示,并将其添加到keysPressedList中。
    1. 通过e.KeyboardDevice.Modifiers来判断是否为修饰符
    2. 一般情况下按键对应的字符就是 e.Key.ToString();,但是还需要判断e.Key 是否为Key.System,如果是则当前按键的实际字符串为e.SystemKey.ToString();。否则按下ALT按键时识别不出NomalKey.
  4. TextBox_KeyUp方法中,当用户松开键盘上的任意键时,将keysPressedList中的按键组合连接成一个字符串,并显示在文本框中。
  5. 然后调用SaveHotKey方法保存快捷键组合。最后更新任务栏图标的工具提示文本和弹出消息框提示用户快捷键已保存。
  6. SaveHotKey方法接收一个按键组合字符串,将其拆分成修饰键和普通键,并将它们转换为对应的枚举值。然后调用RegisterHotKey方法注册新的快捷键。
  7. RegisterHotKey方法首先检查新的快捷键是否已被占用,如果是,则弹出提示框并返回。否则,删除旧的快捷键并添加新的快捷键。最后更新currentModifiercurrentKey的值。
private List<string> keysPressedList = new List<string>(); 
private ModelKeys currentModifier = ModelKeys.ALT;
private NormalKeys currentKey = NormalKeys.O;

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox.Text))
            { 
                textBox.Text = "";
                keysPressedList.Clear();
            }
            var keyStr = e.Key == Key.System ? e.SystemKey.ToString() : e.Key.ToString();
            if (e.KeyboardDevice.Modifiers != ModifierKeys.None)
            {
                if (!keysPressedList.Contains(e.KeyboardDevice.Modifiers.ToString()))
                {
                    keysPressedList.Add(e.KeyboardDevice.Modifiers.ToString());
                }
            }
            if (Enum.IsDefined(typeof(NormalKeys), keyStr))
            {
                if (!keysPressedList.Contains(keyStr))
                {
                    keysPressedList.Add(keyStr);
                }
            }
        }
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if(keysPressedList.Count == 0) return;
            textBox.Text = string.Join(" + ", keysPressedList);
            keysPressedList.Clear();
            SaveHotKey(textBox.Text);
            textBlock.Text = $"2. 可按下{textBox.Text}再次打开窗口";
            var taskbarIcon = ((App)Application.Current).TaskbarIcon;
            taskbarIcon.ToolTipText = "你好!可按下" + textBox.Text + "打开窗口";
            MessageBox.Show("快捷键已保存!");
        }
        private void SaveHotKey(string keyText)
        {

            string[] keys = keyText.Split('+');
            ModelKeys modifier = currentModifier;
            NormalKeys key = currentKey;

            for (int i = 0; i < keys.Length; i++)
            {
                string keyStr = keys[i].Trim();
                if (keyStr.Length == 0)
                {
                    continue;
                }

                if (Enum.IsDefined(typeof(NormalKeys), keyStr))
                {
                    key = (NormalKeys)Enum.Parse(typeof(NormalKeys), keyStr, true);
                }
                else if (Enum.IsDefined(typeof(ModelKeys), keyStr))
                {
                    modifier = (ModelKeys)Enum.Parse(typeof(ModelKeys), keyStr, true);
                }
            }

            RegisterHotKey(modifier, key);
        }
        private void RegisterHotKey(ModelKeys modifier, NormalKeys key)
        {
            if (GlobalHotKey.IsHotKeyProtected(modifier, key))
            {
                MessageBox.Show("快捷键已被占用,请重新选择!");
                return;
            }
            GlobalHotKey.DeleteByKeys(currentModifier, currentKey); // 移除旧的快捷键
            GlobalHotKey.Add(modifier, key, OpenWindow); // 注册新的快捷键
            currentModifier = modifier;
            currentKey = key;
        }

4. 窗口置顶展示

这里碰壁了,在StackOverFlow上找到了解决方案(可查看🔗),实测可用。
调用WindowsAPI并封装为了一个扩展方法


    public static class SystemWindows
    {
        #region 常量

        const UInt32 SWP_NOSIZE = 0x0001; // 不改变窗口大小
        const UInt32 SWP_NOMOVE = 0x0002; // 不改变窗口位置
        const UInt32 SWP_SHOWWINDOW = 0x0040; // 显示窗口

        #endregion

        /// <summary>
        /// 通过将当前窗口附加到前台窗口来激活任意窗口
        /// </summary>
        public static void GlobalActivate(this Window w)
        {
            // 获取此窗口线程的进程ID
            var interopHelper = new WindowInteropHelper(w);
            var thisWindowThreadId = GetWindowThreadProcessId(interopHelper.Handle, IntPtr.Zero);

            // 获取前台窗口线程的进程ID
            var currentForegroundWindow = GetForegroundWindow();
            var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);

            // 将此窗口线程附加到当前窗口线程
            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);

            // 设置窗口位置
            SetWindowPos(interopHelper.Handle, new IntPtr(0), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);

            // 将此窗口线程从当前窗口线程分离
            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);

            // 显示并激活窗口
            if (w.WindowState == WindowState.Minimized) w.WindowState = WindowState.Normal;
            w.Show();
            w.Activate();
        }

        #region DllImport

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow(); // 获取前台窗口句柄

        [DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); // 获取窗口所属线程的进程ID

        [DllImport("user32.dll")]
        private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); // 将一个线程附加到另一个线程

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); // 设置窗口位置和状态

        #endregion
    }

项目地址:

https://github.com/Nita121388/NitasDemo/tree/main/03HotKeyGlobalActiveWindows

完整代码

MainWindow.xaml

<Window
    x:Class="HotKeyGlobalActiveWindows.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:local="clr-namespace:HotKeyGlobalActiveWindows"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    <StackPanel>
        <TextBlock
            Margin="10,10,10,0"
            FontSize="24"
            Text="1. 最小化该app,可在系统托盘中应当看得到该app的图标" />
        <TextBlock
            x:Name="textBlock"
            Margin="10,10,10,0"
            FontSize="24"
            Text="2. 可点击系统托盘或按下Alt+O再次打开窗口" />

        <TextBox
            x:Name="textBox"
            Width="200"
            Height="50"
            Margin="50,10,10,0"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            HorizontalContentAlignment="Center"
            VerticalContentAlignment="Center"
            KeyDown="TextBox_KeyDown"
            KeyUp="TextBox_KeyUp"
            Text="修改快捷键..."
            TextInput="TextBox_TextInput" />
        <TextBox
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Foreground="Gray"
            IsReadOnly="True"
            Text="支持一个Shift、Ctrl、Alt与一个常规键的组合,例如:Alt+J" />
    </StackPanel>
</Window>

MainWindow.xaml.cs

using FastHotKeyForWPF;
using System.Windows;
using System.Windows.Input;

namespace HotKeyGlobalActiveWindows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<string> keysPressedList = new List<string>();
        private ModelKeys currentModifier = ModelKeys.ALT;
        private NormalKeys currentKey = NormalKeys.O;
        public MainWindow()
        {
            InitializeComponent();
            this.StateChanged += Window_StateChanged;
            this.textBox.Text = currentModifier.ToString() + " + " + currentKey.ToString();
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Minimized)
            {
                this.ShowInTaskbar = false;
            }
            else if (this.WindowState == WindowState.Normal)
            {
                OpenWindow();
            }
        }

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

            GlobalHotKey.Add(ModelKeys.ALT, NormalKeys.O, OpenWindow);//注册Alt+O 打开窗口

        }
        private void OpenWindow()
        {
            this.ShowInTaskbar = true;
            this.GlobalActivate();
        }
        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox.Text))
            { 
                textBox.Text = "";
                keysPressedList.Clear();
            }
            var keyStr = e.Key == Key.System ? e.SystemKey.ToString() : e.Key.ToString();
            if (e.KeyboardDevice.Modifiers != ModifierKeys.None)
            {
                if (!keysPressedList.Contains(e.KeyboardDevice.Modifiers.ToString()))
                {
                    keysPressedList.Add(e.KeyboardDevice.Modifiers.ToString());
                }
            }
            if (Enum.IsDefined(typeof(NormalKeys), keyStr))
            {
                if (!keysPressedList.Contains(keyStr))
                {
                    keysPressedList.Add(keyStr);
                }
            }
        }
        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if(keysPressedList.Count == 0) return;
            textBox.Text = string.Join(" + ", keysPressedList);
            keysPressedList.Clear();
            SaveHotKey(textBox.Text);
            textBlock.Text = $"2. 可按下{textBox.Text}再次打开窗口";
            var taskbarIcon = ((App)Application.Current).TaskbarIcon;
            taskbarIcon.ToolTipText = "你好!可按下" + textBox.Text + "打开窗口";
            MessageBox.Show("快捷键已保存!");
        }
        private void SaveHotKey(string keyText)
        {

            string[] keys = keyText.Split('+');
            ModelKeys modifier = currentModifier;
            NormalKeys key = currentKey;

            for (int i = 0; i < keys.Length; i++)
            {
                string keyStr = keys[i].Trim();
                if (keyStr.Length == 0)
                {
                    continue;
                }

                if (Enum.IsDefined(typeof(NormalKeys), keyStr))
                {
                    key = (NormalKeys)Enum.Parse(typeof(NormalKeys), keyStr, true);
                }
                else if (Enum.IsDefined(typeof(ModelKeys), keyStr))
                {
                    modifier = (ModelKeys)Enum.Parse(typeof(ModelKeys), keyStr, true);
                }
            }

            RegisterHotKey(modifier, key);
        }
        private void RegisterHotKey(ModelKeys modifier, NormalKeys key)
        {
            if (GlobalHotKey.IsHotKeyProtected(modifier, key))
            {
                MessageBox.Show("快捷键已被占用,请重新选择!");
                return;
            }
            GlobalHotKey.DeleteByKeys(currentModifier, currentKey); // 移除旧的快捷键
            GlobalHotKey.Add(modifier, key, OpenWindow); // 注册新的快捷键
            currentModifier = modifier;
            currentKey = key;
        }
    }
}

App.xaml文件

<Application
    x:Class="HotKeyGlobalActiveWindows.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:HotKeyGlobalActiveWindows"
    xmlns:tb="http://www.hardcodet.net/taskbar"
    StartupUri="MainWindow.xaml">
    <Application.Resources>
        <tb:TaskbarIcon
            x:Key="Taskbar"
            IconSource="/Assets/Nita.ico"
            LeftClickCommand="{Binding ShowMainWindowCommand}"
            ToolTipText="你好!可点击系统托盘或按下Alt+O再次打开窗口" />
    </Application.Resources>
</Application>

App.xaml.cs文件

using Hardcodet.Wpf.TaskbarNotification;
using System.Configuration;
using System.Data;
using System.Windows;
using System.Windows.Input;

namespace HotKeyGlobalActiveWindows
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public TaskbarIcon TaskbarIcon { get; set; }
        public ICommand ShowMainWindowCommand { get; private set; }
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            ShowMainWindowCommand = new ShowMainWindowCommand();
            TaskbarIcon = (TaskbarIcon)FindResource("Taskbar");
            TaskbarIcon.DataContext = this; // 设置 DataContext 以便绑定命令
        }
    }
    public class ShowMainWindowCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            Application.Current.MainWindow.WindowState = WindowState.Normal;
        }
    }
}

SystemWindows.cs文件


    public static class SystemWindows
    {
        #region 常量

        const UInt32 SWP_NOSIZE = 0x0001; // 不改变窗口大小
        const UInt32 SWP_NOMOVE = 0x0002; // 不改变窗口位置
        const UInt32 SWP_SHOWWINDOW = 0x0040; // 显示窗口

        #endregion

        /// <summary>
        /// 通过将当前窗口附加到前台窗口来激活任意窗口
        /// </summary>
        public static void GlobalActivate(this Window w)
        {
            // 获取此窗口线程的进程ID
            var interopHelper = new WindowInteropHelper(w);
            var thisWindowThreadId = GetWindowThreadProcessId(interopHelper.Handle, IntPtr.Zero);

            // 获取前台窗口线程的进程ID
            var currentForegroundWindow = GetForegroundWindow();
            var currentForegroundWindowThreadId = GetWindowThreadProcessId(currentForegroundWindow, IntPtr.Zero);

            // 将此窗口线程附加到当前窗口线程
            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, true);

            // 设置窗口位置
            SetWindowPos(interopHelper.Handle, new IntPtr(0), 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW);

            // 将此窗口线程从当前窗口线程分离
            AttachThreadInput(currentForegroundWindowThreadId, thisWindowThreadId, false);

            // 显示并激活窗口
            if (w.WindowState == WindowState.Minimized) w.WindowState = WindowState.Normal;
            w.Show();
            w.Activate();
        }

        #region DllImport

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow(); // 获取前台窗口句柄

        [DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); // 获取窗口所属线程的进程ID

        [DllImport("user32.dll")]
        private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); // 将一个线程附加到另一个线程

        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); // 设置窗口位置和状态

        #endregion
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值