C#注册全局热键的方法
来源:互联网 发布:单放砧板架淘宝网 编辑:程序博客网 时间:2024/04/29 17:10
在form里加入以下代码即可
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
switch (m.Msg)
{
case WM_HOTKEY:
this.Close();
break;
}
base.WndProc(ref m);
}
public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
{
NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
if (bCtrl)
{
modifiers = NativeWIN32.KeyModifiers.Control;
}
else if (bShift)
{
modifiers = NativeWIN32.KeyModifiers.Shift;
}
else if (bAlt)
{
modifiers = NativeWIN32.KeyModifiers.Alt;
}
else if (bWindows)
{
modifiers = NativeWIN32.KeyModifiers.Windows;
}
NativeWIN32.RegisterHotKey(Handle, 100, modifiers, c);
}
public class NativeWIN32
{
public NativeWIN32()
{ }
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr GetForegroundWindow();
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szText;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowText(IntPtr hWnd, out STRINGBUFFER ClassName, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public delegate bool EnumThreadProc(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool EnumThreadWindows(int threadId, EnumThreadProc pfnEnum, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr next, string sClassName, IntPtr sWindowTitle);
[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
}
}
- C#注册全局热键的方法
- C#注册全局热键
- C#注册全局热键
- c#注册全局热键
- C#注册全局热键
- [测试]注册全局热键
- Delphi全局热键注册
- Qt5注册全局热键
- Qt注册全局热键
- C#实现全局热键响应,失去焦点情况下响应全局快捷键的方法
- C#实现全局热键响应,失去焦点情况下响应全局快捷键的方法
- C#实现注册全局热键(register hot key)
- 第九节 组合热键与注册全局热键
- c# 全局热键
- VC添加全局热键的方法
- delphi程序全局热键的自定义方法
- 注册全局热键应用例子
- MFC中注册全局热键
- nutch下的CrawlDatum作用
- 十大科技公司创始人离职后发展回顾 境况多不佳
- FreeMarker demo
- QT中的 VS2008 的 .sln,.vcproj, *.proj.user
- android进不了系统解决方案
- C#注册全局热键的方法
- 【oracle资料】oracle sql语句 基础 学习
- struts2中的chain的用法
- 关于Spring的代理问题
- 我的第一个博客
- 中缀表达式转换成后缀表达式
- MySQL分区表实践
- 利用经纬度计算距离
- ISR不能做的(嵌入式面试常用)