public class NativeWIN32
{
public NativeWIN32()
{ }
/* ------- using WIN32 Windows API in a C# application ------- */ [DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr GetForegroundWindow(); // [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = )]
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); /* ------- using HOTKEYs in a C# application ------- in form load :
bool success = RegisterHotKey(Handle, 100, KeyModifiers.Control | KeyModifiers.Shift, Keys.J); in form closing :
UnregisterHotKey(Handle, 100); protected override void WndProc( ref Message m )
{
const int WM_HOTKEY = 0x0312; switch(m.Msg)
{
case WM_HOTKEY:
MessageBox.Show("Hotkey pressed");
break;
}
base.WndProc(ref m );
} ------- using HOTKEYs in a C# application ------- */ [DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, // handle to window
int id, // hot key identifier
KeyModifiers fsModifiers, // key-modifier options
Keys vk // virtual-key code
); [DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, // handle to window
int id // hot key identifier
); [Flags()]
public enum KeyModifiers
{
None = ,
Alt = ,
Control = ,
Shift = ,
Windows =
}
}

在Form中Load事件中首先注册热键

        /// <summary>
/// 注册热键
/// </summary>
/// <param name="c">按键</param>
/// <param name="bCtrl">是否需要ctrl</param>
/// <param name="bShift">是否需要shift</param>
/// <param name="bAlt">是否需要alt</param>
/// <param name="bWindows">是否需要win</param>
public void SetHotKey(Keys c, bool bCtrl, bool bShift, bool bAlt, bool bWindows)
{
//先赋到变量
Keys m_key = c;
bool m_ctrlhotkey = bCtrl;
bool m_shifthotkey = bShift;
bool m_althotkey = bAlt;
bool m_winhotkey = bWindows; //注册系统热键
NativeWIN32.KeyModifiers modifiers = NativeWIN32.KeyModifiers.None;
if (bCtrl)
modifiers |= NativeWIN32.KeyModifiers.Control;
if (bShift)
modifiers |= NativeWIN32.KeyModifiers.Shift;
if (bAlt)
modifiers |= NativeWIN32.KeyModifiers.Alt;
if (bWindows)
modifiers |= NativeWIN32.KeyModifiers.Windows;
NativeWIN32.RegisterHotKey(Handle, , modifiers, c);
}

然后监听消息,处理事件

        /// <summary>
/// 重写windows消息响应
/// </summary>
/// <param name="m"></param>
protected override void WndProc(ref Message m)
{
const int wmHotkey = 0x0312;
switch (m.Msg)
{
case wmHotkey:
WindowMax();
break;
}
base.WndProc(ref m);
}

最新文章

  1. JAVA单例的三种实现方式
  2. JsonCpp简单使用
  3. maven Spring 4.2+SpringMVC+dubbo解决TypeProxyInvocationHandler.invoke(SerializableTypeWrapper.java:239)
  4. 【转】iOS 开发之协议protocal-代理传值delegate
  5. gnome中文翻译之po
  6. Destoon标签使用技巧十则
  7. javascript正则简单入门
  8. Mysql 如何做双机热备和负载均衡 (方法一)
  9. Linux server关闭自己主动
  10. DDD分层架构之值对象(介绍篇)
  11. 使用Varnish+ESI实现静态页面的局部缓存(思路篇)
  12. SLAM+语音机器人DIY系列:(二)ROS入门——1.ROS是什么
  13. 网络爬虫 - 真&#183;AC自动机
  14. Java — CommonUtil
  15. 28. css样式中px转rem
  16. Thinkphp路由使用
  17. 转 java反射详解
  18. ActiveReports 报表应用教程 (11)---交互式报表之文档目录
  19. 7款让人惊叹的HTML5粒子动画特效
  20. luogu P2617 Dynamic Rankings(主席树)

热门文章

  1. Ubuntu16.04搭建Postfix作为SMTP服务器
  2. history 命令历史
  3. Ant 学习笔记
  4. SpringMVC注解@RequestParam与RequestMapping全面解析
  5. 数据结构与算法——基数排序简单Java实现
  6. 10.4ORM回顾!
  7. day_4.28 py
  8. 使用 tabindex 改变Tab 键顺序
  9. DataGridView实时提交
  10. J S 脚本语言 if() { if { } else { } } var a =100; switch { case ( ) break ; } 基础详解 , 最下面有例子