//Form1不是激活时怎样使KeyDown也是有效的?
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace cjl
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(292, 273);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.TopMost = true;
this.KeyDown += new KeyEventHandler(this.Form1_KeyDown); }
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
private static void Main()
{
Application.Run(new Form1());
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// Form1不是激活时怎样使KeyDown也是有效的?
// 如果不Fom1不是激活的窗口(标题栏灰色时),KeyDown是无效的。怎样能够使窗口不在激活时
// (如最小化时)KeyDown也是有效的?
//
MessageBox.Show("keyDown"); }
}
}
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace cjl
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new Size(6, 14);
this.ClientSize = new Size(292, 273);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.TopMost = true;
this.KeyDown += new KeyEventHandler(this.Form1_KeyDown); }
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
private static void Main()
{
Application.Run(new Form1());
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// Form1不是激活时怎样使KeyDown也是有效的?
// 如果不Fom1不是激活的窗口(标题栏灰色时),KeyDown是无效的。怎样能够使窗口不在激活时
// (如最小化时)KeyDown也是有效的?
//
MessageBox.Show("keyDown"); }
}
}
public class WinHotKey
{
[DllImport("user32.dll",SetLastError=true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //窗口句柄
int id,
KeyModifiers fsModifiers,
Keys vk
); [DllImport("user32.dll",SetLastError=true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
); [Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control =2,
Shift = 4,
Windows = 8
} public WinHotKey()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}然后,在程序中这样调用
//快捷键定义
private bool key_Ctrl = false;
private bool key_Shift = false;
private bool key_Alt = false;
private bool key_Windows = false;
private Keys key_other; public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool bWindows,Keys nowKey)
{
try
{
this.key_Alt = bAlt;
this.key_Ctrl = bCtrl;
this.key_Shift = bShift;
this.key_Windows = bWindows;
this.key_other = nowKey;
WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None; if( this.key_Ctrl )
modifier |= WinHotKey.KeyModifiers.Control;
if(this.key_Alt )
modifier |= WinHotKey.KeyModifiers.Alt;
if(this.key_Shift)
modifier |= WinHotKey.KeyModifiers.Shift;
if(this.key_Windows)
modifier |= WinHotKey.KeyModifiers.Windows;
WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);
}
catch
{
//login.ShowMessage("快捷键定义错误!");
}
} //激活热键
protected override void WndProc(ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
{
//如果有新消息,弹出消息
if( ReceiveNewMessage == true)
{
for(int i=0;i<this.manInforList.Count;i++)
{
ManInfor searchMan = (ManInfor)this.manInforList[i];
if( searchMan.manInforID.Equals( getFriendID))
{
searchMan.Clicked = true;
searchMan.P2PShow();
break;
}
}
}
else
{
this.Show();
this.TopMost = true;
this.panel_Main.Refresh();
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
}
break;
}
base.WndProc(ref m );
}
Ctrl+A
Ctrl+B
怎样分别处理?
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if(m.Msg==WM_HOTKEY)
{
if(!settingHotKey)
{
System.EventArgs e;
e = new System.EventArgs();
if(HotKeyPress!=null)
HotKeyPress(this, e);
if((int)m.WParam==729)
OnHotKeyPressNotice(this, e);
else if((int)m.WParam==730)
OnHotKeyPressForm(this, e);
}
}
base.WndProc(ref m);
}
我这里面的729,730表示是注册热键时的值,相当于上面那位老兄代码里的:100
WinHotKey.RegisterHotKey(Handle,100,modifier,nowKey);如果你要注册多个热键,就传不同的值过去。