c#模仿酷狗怎么实现全局热键控制
展开全部
看代码:
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 100, 0, Keys.A); //注册快捷键,热键为A
RegisterHotKey(Handle, 100, 1, Keys.B); //1为Alt键,热键为Alt+B
RegisterHotKey(Handle, 100, 2, Keys.A); //定义热键为Alt+Tab,这里实现了屏幕系统Alt+Tab键
RegisterHotKey(Handle, 200, 2, Keys.B); //注册2个热键,根据id值100,200来判断需要执行哪个函数
}
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
break;
}
base.WndProc(ref m); //将系统消息传递自父类的WndProc
}
private void ProcessHotkey(Message m) //按下设定的键时调用该函数
{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "100":
MessageBox.Show("调用A函数");
break;
case "200":
MessageBox.Show("调用B函数");
break;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 100);//卸载第1个快捷键
UnregisterHotKey(Handle, 200); //缷载第2个快捷键
}
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 100, 0, Keys.A); //注册快捷键,热键为A
RegisterHotKey(Handle, 100, 1, Keys.B); //1为Alt键,热键为Alt+B
RegisterHotKey(Handle, 100, 2, Keys.A); //定义热键为Alt+Tab,这里实现了屏幕系统Alt+Tab键
RegisterHotKey(Handle, 200, 2, Keys.B); //注册2个热键,根据id值100,200来判断需要执行哪个函数
}
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
break;
}
base.WndProc(ref m); //将系统消息传递自父类的WndProc
}
private void ProcessHotkey(Message m) //按下设定的键时调用该函数
{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
//MessageBox.Show(id.ToString());
string sid = id.ToString();
switch (sid)
{
case "100":
MessageBox.Show("调用A函数");
break;
case "200":
MessageBox.Show("调用B函数");
break;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 100);//卸载第1个快捷键
UnregisterHotKey(Handle, 200); //缷载第2个快捷键
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询