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 ...
随机推荐
- SQL 逗号分隔将一行拆成多行
and number<=len(a.KOrderID) and type=)=',')
- 史上最详细的JavaScript事件使用指南
事件流 事件流描述的是从页面中接收事件的顺序,IE和Netscape提出来差不多完全相反的事件流的概念,IE事件流是事件冒泡流,Netscape事件流是事件捕获流. 事件冒泡 IE的事件流叫做事件冒泡 ...
- Cocos2dx网络读取图片
// // Connection.h // XXDemo // // Created by LeeHonGee on 14-9-4. // // #ifndef __XXDemo__Connectio ...
- react如何监听路由url变化
"componentWillReceiveProps" "shouldComponentUpdate" "componentWillUpdate&qu ...
- MTK 修改默认时区
首先介绍应用程序修改 : AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); mA ...
- [Tensorflow] Cookbook - Retraining Existing CNNs models - Inception Model
From: https://github.com/jcjohnson/cnn-benchmarks#alexnet 先大概了解模型,再看如果加载pre-training weight. 关于retai ...
- web.xml配置DispatcherServlet (***-servlert.xml)
1. org.springframework.web.servlet.DispatcherServlet 所在jar包: <dependency> <groupId>org.s ...
- PHP实现敏感词过滤系统
PHP实现敏感词过滤系统 安装说明 安装PHP扩展 trie_filter,安装教程 http://blog.41ms.com/post/39.html 安装PHP扩展 swoole,安装教程 htt ...
- IDEA自动编译设置
ctrl+alt+s: ctrl+shift+alt+/:
- js 使用a标签 下载资源
文档 let data = new Blob(['hello ajanuw'], { type: 'application/text' }) let src = window.URL.createOb ...