还可以通过调用user32.dll实现.
using System.Runtime.InteropServices;//其他using就省略了
namespace WindowsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
int fsModifiers,//alt = 1, none = 0, win = 8;
Keys virtualKey
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey();//调用主处理程序
break;
}
base.WndProc(ref m);
}
void ProcessHotkey()
{
//这里是处理快捷键的地方,为空就好了
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 100, 1, Keys.F4);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 100);
}
}
}
using System.Runtime.InteropServices;//其他using就省略了
namespace WindowsApplication1
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
int fsModifiers,//alt = 1, none = 0, win = 8;
Keys virtualKey
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey();//调用主处理程序
break;
}
base.WndProc(ref m);
}
void ProcessHotkey()
{
//这里是处理快捷键的地方,为空就好了
}
private void Form1_Load(object sender, EventArgs e)
{
RegisterHotKey(Handle, 100, 1, Keys.F4);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(Handle, 100);
}
}
}
浙公网安备 33010602011771号