我知道這里已經回答了很多關于這個問題的問題,但我不明白該怎么做,因為我仍然是編程的初學者。所以我有代碼可以作為在 WinForms 中作業的全域 keyhook。即使應用程式最小化,它也會檢測何時按下 F9。但是在將代碼重寫為 wpf 時,我遇到了一些麻煩protected override void WndProc(ref Message m)。我解決了該錯誤所需的所有其他代碼。以下是我的代碼片段。
using System.Windows.Interop;
// 2. Import the RegisterHotKey Method
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
在建構式中:
// 3. Register HotKey
int UniqueHotkeyId = 1;
int HotKeyCode = (int)0x78;
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
Boolean F9Registered = RegisterHotKey(windowHandle, UniqueHotkeyId, 0x0000, HotKeyCode);
以及錯誤所在的部分:
protected override void WndProc(ref Message m)
{
// 5. Catch when a HotKey is pressed!
if (m.Msg == 0x0312)
{
int id = m.WParam.ToInt32();
if (id == 1)
{
//Do something
}
}
base.WndProc(ref m);
}
錯誤在:
protected override void WndProc(ref Message m)
和
base.WndProc(ref m);
感謝您的任何回答。
uj5u.com熱心網友回復:
我在 Arkane 的回答的幫助下解決了這個問題。但有效的示例代碼位于此處:https ://blog.magnusmontin.net/2015/03/31/implementing-global-hot-keys-in-wpf/
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/438281.html
上一篇:以不同的表單標簽C#顯示計算結果
