- cl
-
你可以使用热键来实现你要的功能 不需要窗口焦点 代码如下using System.Runtime.InteropServices; [DllImport("user32.dll", EntryPoint = "RegisterHotKey")]
public static extern bool RegisterHotKey
(
IntPtr hWnd, //要注册热键的窗口句柄
int id, //热键编号
int fsModifiers, //特殊键如:Ctrl,Alt,Shift,Window
int vk //一般键如:A B C F1,F2 等
); [DllImport("user32.dll", EntryPoint = "UnregisterHotKey")]
public static extern bool UnregisterHotKey
(
IntPtr hWnd, //注册热键的窗口句柄
int id //热键编号上面注册热键的编号
);
private const int WM_HOTKEY = 0x312;
private enum MyKeys
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
Win = 8
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(this.Handle, 1, (int)(MyKeys.Ctrl | MyKeys.Alt), (int)Keys.W);//Ctrl+Alt+W
RegisterHotKey(this.Handle, 2, (int)(MyKeys.None), (int)Keys.H);//直接按H
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 1);
UnregisterHotKey(this.Handle, 2);
} protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY)
{
switch (m.WParam.ToInt32())
{
case 1://按Ctrl+Alt+W时显示窗体
this.Show();
this.WindowState = FormWindowState.Normal;
break;
case 2://直接按H输出AAAA
MessageBox.Show("AAAA");
break;
default:
break;
}
}
base.WndProc(ref m);
}