From de3e6f0db558b8d29ff9161fea0dc50b2b9edc77 Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Tue, 3 Jan 2023 19:43:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8F=90=E5=8F=96=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E5=BF=AB=E6=8D=B7=E9=94=AE=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- STranslate/Helper/HotkeysHelper.cs | 35 ++++++++++++++++++++ STranslate/MainWindow.xaml.cs | 52 ++++++++++-------------------- 2 files changed, 52 insertions(+), 35 deletions(-) diff --git a/STranslate/Helper/HotkeysHelper.cs b/STranslate/Helper/HotkeysHelper.cs index 2342246..ba95225 100644 --- a/STranslate/Helper/HotkeysHelper.cs +++ b/STranslate/Helper/HotkeysHelper.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using System.Windows; +using System.Windows.Interop; using static STranslate.Helper.NativeMethodHelper; namespace STranslate.Helper @@ -58,6 +61,38 @@ namespace STranslate.Helper public static byte OpenMainWindowModifiers; public static int OpenMainWindowKey; + public delegate void HotKeyCallBackHanlder(); + private static Dictionary keymap = new Dictionary(); + + public static void InitialHook(Window window) + { + var hwnd = new WindowInteropHelper(window).Handle; + RegisterHotKey(hwnd); + var _hwndSource = HwndSource.FromHwnd(hwnd); + _hwndSource.AddHook(WndProc); + } + public static void Register(int id, HotKeyCallBackHanlder callBack) + { + keymap[id] = callBack; + } + /// + /// 快捷键消息处理 + /// + private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) + { + switch (msg) + { + case 0x0312: //这个是window消息定义的 注册的热键消息 + int id = wParam.ToInt32(); + if (keymap.TryGetValue(id, out var callback)) + { + callback(); + } + break; + } + return IntPtr.Zero; + } + /// /// 注册快捷键 /// diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index b838067..10ec55d 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -138,44 +138,26 @@ namespace STranslate protected override void OnSourceInitialized(EventArgs e) { //base.OnSourceInitialized(e); - IntPtr handle = new WindowInteropHelper(this).Handle; - HotkeysHelper.RegisterHotKey(handle); + HotkeysHelper.InitialHook(this); + HotkeysHelper.Register(HotkeysHelper.InputTranslateId, () => + { + InputTranslateMenuItem_Click(null, null); + }); - HwndSource source = HwndSource.FromHwnd(handle); - source.AddHook(WndProc); - } + HotkeysHelper.Register(HotkeysHelper.CrosswordTranslateId, () => + { + CrossWordTranslateMenuItem_Click(null, null); + }); - /// - /// 热键的功能 - /// - /// - protected IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handle) - { - switch (msg) + //HotkeysHelper.Register(HotkeysHelper.ScreenShotTranslateId, () => + //{ + // ScreenshotTranslateMenuItem_Click(null, null); + //}); + + HotkeysHelper.Register(HotkeysHelper.OpenMainWindowId, () => { - case 0x0312: //这个是window消息定义的 注册的热键消息 - //Console.WriteLine(wParam.ToString()); - if (wParam.ToString().Equals(HotkeysHelper.InputTranslateId + "")) - { - this.InputTranslateMenuItem_Click(null, null); - } - else if (wParam.ToString().Equals(HotkeysHelper.CrosswordTranslateId + "")) - { - this.CrossWordTranslateMenuItem_Click(null, null); - } -#if false - else if (wParam.ToString().Equals(HotkeysHelper.ScreenShotTranslateId + "")) - { - this.ScreenshotTranslateMenuItem_Click(null, null); - } -#endif - else if (wParam.ToString().Equals(HotkeysHelper.OpenMainWindowId + "")) - { - this.OpenMainWin_Click(null, null); - } - break; - } - return IntPtr.Zero; + OpenMainWin_Click(null, null); + }); } ///