winform接收全局的快捷键
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接收全局的快捷键的更多相关文章
- swift xcode设置 ,代码折叠,全局折叠 快捷键
在preference text editing 里面打开 function 折叠的项, 折叠方法快捷键: option+command +left/right 全局折叠快捷键: shift+opti ...
- C# Winform添加全局快捷键(老板键)
using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.W ...
- WinForm程序全局捕捉异常处理办法
如何全局捕捉Winform程序异常呢,当然是从程序启动入口的Program类下的Main()方法定义了,下面看下这个类怎么写的吧 static class Program { static strin ...
- C# WinForm捕获全局异常
网上找的C# WinForm全局异常捕获方法,代码如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summ ...
- winform 记录全局异常捕获
这篇文章主要是备用 记录winform程序捕获全局异常. /// <summary> /// 应用程序的主入口点. /// </summary> public static A ...
- 如何捕获winform程序全局异常?(续)
前言 上篇文章我提供了一种方案可以供我们捕获单线程程序中的所有未处理异常.但是如果程序是多线程,那么新增线程出现了异常上个方案就无能为力了.本着方案总比问题多的态度,我再给大家提供一种新的方案,供大家 ...
- 如何捕获winform程序全局异常?
1.在C#中我们如何处理异常? 上面的问题学过C#的问题大家可能都能回答处理,用try-catch-finally具体如下: try { //可能出错的语句 } catch (Exception) { ...
- 为Sublime Text 设置全局启动快捷键
为Sublime Text创建快捷方式.找到Sublime Text安装目录中的“sublime_text.exe”文件,然后右击创建快捷方式,如下图: 为Sublime Tex设置全局快捷键.将上 ...
- Winform为窗体增加快捷键
1. 定义窗体的 xxx_KeyDown(object sender, EventArgs e) 2. 书写快捷键的代码: //这里的xxx代表你的窗体名 private void xxxx_KeyD ...
随机推荐
- jquery.pagination.js 新增 首页 尾页 功能
jquery.pagination.js 新增 首页 尾页 功能 废话不多说,直接上修改后的代码,修改部分已经用 update 注释包含 17-20行 99-103行 141-145行 /** * T ...
- Oracle Grid 11.2.0.4 安装是出现"INS-30510: Insufficient number of ASM disks selected."
最新文章:Virson's Blog 错误的原因是由于磁盘数和冗余层级不匹配: 如果创建用来存放OCR和VOTEDISK的ASM磁盘组,那么External.Normal.High三种冗余级别对应的F ...
- java-信息安全(十二)-数字签名【Java证书体系实现】
概述 信息安全基本概念 数字证书 数字证书就是互联网通讯中标志通讯各方身份信息的一串数字,提供了一种在Internet上验证通信实体身份的方式,数字证书不是数字身份证,而是身份认证机构盖在数字身份证上 ...
- wvblk 把 xp、2003、win7(32位) 装入 VHD
关键1:是[预安装]阶段F6加载wvblk驱动: or 在还原ghost镜像后,导入wvblk驱动. 关键1.5:对于 win7(32位)来说,还可以在设备管理器内,通过添加“过时”硬件的方式导入wv ...
- [Bayes] Understanding Bayes: A Look at the Likelihood
From: https://alexanderetz.com/2015/04/15/understanding-bayes-a-look-at-the-likelihood/ Reading note ...
- [Algorithm] Maximum Flow
Ref MIT: lecture-13-incremental-improvement-max-flow-min-cut/ Ford Fulkerson algorithm for finding m ...
- SpringMVC中 -- @RequestMapping的作用及用法
一.@RequestMapping 简介 在Spring MVC 中使用 @RequestMapping 来映射请求,也就是通过它来指定控制器可以处理哪些URL请求,相当于Servlet中在web.x ...
- 网易大数据之数据存储:HDFS
一.HDFS基础架构 1.HDFS特点:水平扩展.高容错性.廉价硬件.开源生态系统 2.Hadoop生态圈 1).分布式存储系统(HDFS),2).资源管理框架(YARN),3).批处理框架(MapR ...
- Python version 2.7 required, which was not found in the registry解决方法
转自:https://blog.csdn.net/zklth/article/details/8117207 新建一个register.py文件,执行该文件,完成python的注册. import s ...
- NUC972----最简单的驱动(转)
1.新建文本文档,重命名为 hello_dev.c (驱动的开发同应用的开发一样,也是在文本文档下开发的). 2.包含头文件 内核模块需要包含内核相关头文件,不同模块根据功能的差异,所需要的头文件也不 ...