如何在 WPF 应用程序中使用 WndProc
How to use WndProc in a WPF application
我知道这里已经回答了很多关于这个的问题,但我不知道该怎么做,因为我还是编程的初学者。所以我有代码可以作为在 WinForms 中工作的全局键钩。即使应用程序最小化,它也会检测何时按下 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);
感谢您的回答。
我在 Arkane 的回答的帮助下解决了这个问题。但有效的示例代码位于:https://blog.magnusmontin.net/2015/03/31/implementing-global-hot-keys-in-wpf/
我知道这里已经回答了很多关于这个的问题,但我不知道该怎么做,因为我还是编程的初学者。所以我有代码可以作为在 WinForms 中工作的全局键钩。即使应用程序最小化,它也会检测何时按下 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);
感谢您的回答。
我在 Arkane 的回答的帮助下解决了这个问题。但有效的示例代码位于:https://blog.magnusmontin.net/2015/03/31/implementing-global-hot-keys-in-wpf/