feat: 提取注册快捷键回调

dev_split_view_vm
DESKTOP-3BO4HSG\ksat 2 years ago
parent 9708ff1cdd
commit de3e6f0db5

@ -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<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
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;
}
/// <summary>
/// 快捷键消息处理
/// </summary>
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;
}
/// <summary>
/// 注册快捷键
/// </summary>

@ -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);
});
/// <summary>
/// 热键的功能
/// </summary>
/// <param name="m"></param>
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);
});
}
/// <summary>

Loading…
Cancel
Save