Oti*_*iel 36
你想要的是全球热门.
在您的班级顶部导入所需的库:
// DLL libraries used to manage hotkeys
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
Run Code Online (Sandbox Code Playgroud)在您的类中添加一个字段,该字段将成为代码中热键的引用:
const int MYACTION_HOTKEY_ID = 1;
Run Code Online (Sandbox Code Playgroud)注册热键(例如,在Windows窗体的构造函数中):
// Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8
// Compute the addition of each combination of the keys you want to be pressed
// ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6...
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int) Keys.F12);
Run Code Online (Sandbox Code Playgroud)通过在类中添加以下方法来处理键入的键:
protected override void WndProc(ref Message m) {
if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID) {
// My hotkey has been typed
// Do what you want here
// ...
}
base.WndProc(ref m);
}
Run Code Online (Sandbox Code Playgroud)如果您在运行Otiel的解决方案时遇到问题:
你需要包括:
using System.Runtime.InteropServices; //required for dll import
Run Code Online (Sandbox Code Playgroud)像我这样的新手的另一个疑问:"类的顶级"真的意味着你的类顶级(不是命名空间或构造函数):
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
Run Code Online (Sandbox Code Playgroud)您不需要添加user32.dll作为项目的引用.WinForms总是自动加载此DLL.
| 归档时间: |
|
| 查看次数: |
33630 次 |
| 最近记录: |