C# 系统热键注册实现代码

2016-02-19 09:07 6 1 收藏

今天图老师小编要跟大家分享C# 系统热键注册实现代码,精心挑选的过程简单易学,喜欢的朋友一起来学习吧!

【 tulaoshi.com - 编程语言 】

先引用using System.Runtime.InteropServices; 的命名空间,
然后在合适的位置加上如下代码就OK。。注意:Form1_Load和Form1_FormClosed不能直接copy哦~ 代码如下:

[DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd,int id,uint control,Keys vk );
//注册热键的api
[DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

private void Form1_Load(object sender, EventArgs e)
{
//注册热键(窗体句柄,热键ID,辅助键,实键)
RegisterHotKey(this.Handle, 888, 2, Keys.A);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
//注消热键(句柄,热键ID)
UnregisterHotKey(this.Handle, 888);
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x0312: //这个是window消息定义的 注册的热键消息
if (m.WParam.ToString().Equals("888")) //如果是我们注册的那个热键
MessageBox.Show("你按了ctrl+a");
break;
}
base.WndProc(ref m);
}

辅助键说明:
None = 0,
Alt = 1,
crtl= 2,
Shift = 4,
Windows = 8
如果有多个辅助键则,例如 alt+crtl是3 直接相加就可以了

来源:https://www.tulaoshi.com/n/20160219/1589647.html

延伸阅读
1. 概述 有时候我们需要在web页面上显示一张图,比如说一张地图,而这张地图会比较大。这时候如果我们把一张大图分隔成一组小图,那么客户端的显示速度会明显地感觉块。希望阅读本文对你有所帮助。 2. 实现思路 .NET Framework GDI+ 为我们提供了一组丰富地类来编辑图形图像。有关.NET Framework GDI+的...
今天在写程序时想用一个系统热键,就像QQ一样最小化以后也能照样响应指定的快捷键。尽管我知道VC中肯定有现成的方法,但我毕竟没有实现过,于是研究了一番,很快就找到了解决方法。 使用系统热键可以这么进行,假设我们在 CTestHotkeyDlg 这个类进行热键测试。 A. 首先是注册系统热键 RegisterHotKey BOOL RegisterHotKey(HWND hWnd, int i...
此时希望用户不能通过键盘alt+F4来结束程序及通过Win的组合键对窗口进行操作。我在网上搜索了一下,采用全局键盘钩子的方法可以做到屏蔽用户对键盘的操作。。以下为相关代码,用到了Form1_load事件和Form1_FormClosing事件: 代码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;...
标签: Web开发
解析得到的代码能通过XHTML 1.0 STRICT验证; 包含了标题,链接,字体,对齐,图片,引用,列表等方面的功能.  Ubb.ReadMe.htm UBB代码说明标题[h1]标题一[/h1] 标题一 [h2]标题二[/h2] 标题二 [h1]标题三[/h1] 标题三 [h4]标题四[/h4] 标题四 [h5]标题五[/h5] 标题五 [h6]标题六[/h6] 标题六 链接[url]www.unibetter.co...
/*  FTPFactory.cs  Better view with tab space=4  Written by Jaimon Mathew (jaimonmathew@rediffmail.com)  Rolander,Dan (Dan.Rolander@marriott.com) has modified the  download  method to cope with file ...

经验教程

679

收藏

9
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部