【发布时间】:2020-02-03 21:31:09
【问题描述】:
我有这段代码适用于Winform:
public class GlobalHotkey
{
private int modifier;
private int key;
private IntPtr hWnd;
private int id;
public GlobalHotkey(int modifier, Keys key, Form form)
{
this.modifier = modifier;
this.key = (int)key;
this.hWnd = form.Handle;
id = this.GetHashCode();
}
public bool Register()
{
return RegisterHotKey(hWnd, id, modifier, key);
}
public bool Unregiser()
{
return UnregisterHotKey(hWnd, id);
}
public override int GetHashCode()
{
return modifier ^ key ^ hWnd.ToInt32();
}
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
在InitializeComponent 之后,我只是这样启动它:
ghk = new GlobalHotkey(Constants.CTRL + Constants.SHIFT, Keys.A, this);
我有一个 WPF 项目,我想使用同一个类,所以我尝试这样更改构造函数:
public GlobalHotkey(int modifier, Keys key, System.Windows.Window form)
{
this.modifier = modifier;
this.key = (int)key;
this.hWnd = form.Handle;
id = this.GetHashCode();
}
但是我在这一行有一个编译时错误:
this.hWnd = form.Handle;
严重性代码描述项目文件行抑制状态 错误 CS1061 'Window' 不包含 'Handle' 的定义和 没有可访问的扩展方法“句柄”接受第一个参数 可以找到类型“窗口”(您是否缺少 using 指令或 汇编参考?)
【问题讨论】: