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);
}

winform接收全局的快捷键的更多相关文章

  1. swift xcode设置 ,代码折叠,全局折叠 快捷键

    在preference text editing 里面打开 function 折叠的项, 折叠方法快捷键: option+command +left/right 全局折叠快捷键: shift+opti ...

  2. C# Winform添加全局快捷键(老板键)

    using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.W ...

  3. WinForm程序全局捕捉异常处理办法

    如何全局捕捉Winform程序异常呢,当然是从程序启动入口的Program类下的Main()方法定义了,下面看下这个类怎么写的吧 static class Program { static strin ...

  4. C# WinForm捕获全局异常

    网上找的C# WinForm全局异常捕获方法,代码如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summ ...

  5. winform 记录全局异常捕获

    这篇文章主要是备用 记录winform程序捕获全局异常. /// <summary> /// 应用程序的主入口点. /// </summary> public static A ...

  6. 如何捕获winform程序全局异常?(续)

    前言 上篇文章我提供了一种方案可以供我们捕获单线程程序中的所有未处理异常.但是如果程序是多线程,那么新增线程出现了异常上个方案就无能为力了.本着方案总比问题多的态度,我再给大家提供一种新的方案,供大家 ...

  7. 如何捕获winform程序全局异常?

    1.在C#中我们如何处理异常? 上面的问题学过C#的问题大家可能都能回答处理,用try-catch-finally具体如下: try { //可能出错的语句 } catch (Exception) { ...

  8. 为Sublime Text 设置全局启动快捷键

    为Sublime Text创建快捷方式.找到Sublime Text安装目录中的“sublime_text.exe”文件,然后右击创建快捷方式,如下图:  为Sublime Tex设置全局快捷键.将上 ...

  9. Winform为窗体增加快捷键

    1. 定义窗体的 xxx_KeyDown(object sender, EventArgs e) 2. 书写快捷键的代码: //这里的xxx代表你的窗体名 private void xxxx_KeyD ...

随机推荐

  1. EasyRadius 动态域名DDNS设置工具,支持WayOS三代,完美解决近段时间3322和每步不稳定问题

    以下软件只适合拥有公网IP的用户哦,要是您没有公网IP,只能和我们联系,获取VPN了 EasyRadius从1.65开始就提供DDNS,中途由于我们升级了安全性,导致DDNS更新失败 这段时间由于33 ...

  2. Redis数据库高级实用特性:持久化机制

    Redis数据库高级实用特性:持久化机制 大型web系统数据缓存设计 Redis高级特性:虚拟内存的使用技巧 Redis高级实用特性:安全性与主从复制 Memcached.Redis OR Tair

  3. xml 转map dom4j

    http://ziyu-1.iteye.com/blog/469003 传过来一个xml文件,需要转换成Map,能够应对不用结构的xml,而不是只针对固定格式的xml. 转换规则: 1.主要是Map与 ...

  4. 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V2 -- 本地方法调用 整合 Spring

    网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——RPC -- 本地方法调用 + Spring 1. 配置applicationContext.xml文件 注入 bean 及 管理 bean ...

  5. Android ListView只加载当前屏幕内的图片(解决list滑动时加载卡顿)

    最近在做ListView分页显示,其中包括图片 和文字(先下载解析文字内容,再异步加载图片)发现每次点击下一页后,文字内容加载完毕,马上向下滑动,由于这时后台在用线程池异步下载图片,我每页有20条,也 ...

  6. Office Web Apps Server

    Office Web Apps Server Office Web Apps Server 是一款 Office 服务器产品,可提供针对 Office 文件的基于浏览器的文件查看和编辑服务.Offic ...

  7. AngularJS基础02 神奇的数据绑定(Binding)

    作者:arccosxy  转载请注明出处:http://www.cnblogs.com/arccosxy/ 上一节,我们在JS中声明一个scope变量然后在HTML直接访问它,这非常的酷.但是Angu ...

  8. lombok安装方法

    一.介绍 lombok网址:https://projectlombok.org/download.html lombok能够在编译源码的时候自动生成getter和setter方法.即它最终能够达到的效 ...

  9. 网卡配置文件详解 用户管理与文件权限篇 文件与目录权限 软连接 tar解压命令 killall命令 linux防火墙 dns解析设置 计划任务crond服务 软件包安装 阿里云 yum源 安装

    Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. ...

  10. xdebug的配置

    第一步,让xdebug在php环境中生效 下载xdebug http://www.xdebug.org/download.php 这里会出现针对PHP各种版本的下载.找到适合你自己的版本,此处值得注意 ...