有两个窗体win1、win2,其中win2是win1通过show出来的窗体。
现在需要对win1、win2注册热键,例如二者都注册esc键
注册如下:
class HotKey //热键类
{
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
); [DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
); //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
} }win1中
注册热键:
HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.None, Keys.Escape);
热键方法:
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 100:
this.Close();
break;
}
break;
}
base.WndProc(ref m);
}
win2中注册:
HotKey.RegisterHotKey(Handle, 1000, HotKey.KeyModifiers.None, Keys.Escape);
热键方法:
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 1000:
this.Close();
break;
}
break;
}
base.WndProc(ref m);
}现在情况是,如果我按ESC键,这两个窗体会同时关闭,我的本意是按一下ESC键win2关闭,再按一下ESC键 win1关闭
请教各位高手,如何才能实现这种效果?
谢谢各位了
现在需要对win1、win2注册热键,例如二者都注册esc键
注册如下:
class HotKey //热键类
{
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
); [DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
); //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
} }win1中
注册热键:
HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.None, Keys.Escape);
热键方法:
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 100:
this.Close();
break;
}
break;
}
base.WndProc(ref m);
}
win2中注册:
HotKey.RegisterHotKey(Handle, 1000, HotKey.KeyModifiers.None, Keys.Escape);
热键方法:
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
switch (m.WParam.ToInt32())
{
case 1000:
this.Close();
break;
}
break;
}
base.WndProc(ref m);
}现在情况是,如果我按ESC键,这两个窗体会同时关闭,我的本意是按一下ESC键win2关闭,再按一下ESC键 win1关闭
请教各位高手,如何才能实现这种效果?
谢谢各位了
private void Form2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Escape)
{
this.Close();
}
}
回MASK_LL:
程序中要定义的热键很多,不同窗体之间的键可能就会冲突,除了这种注册方法,是否还有其他的方法来避免冲突呢?
回bdmh:窗口句柄已经传递了,但是我单步调试时发现,执行WndProc这个方法时,会先去只想win1的,此时句柄就是win1的,然后才走win2,句柄又变成win2的了,可能我没有理解您的意思,能方便具体说明吗?回yfqvip:我先试试您说的方法
您好,因为我先在win1中重写了WndProc,然后在win2中重写了WndProc,这样在执行热键时,它会先执行win1中的WndProc,怎么也进不去win2中,所以现在没实现效果。
请如何才能让它先执行win2中的WndProc呢?