惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 流泉飞石

转:ADO.Net Entity Framework : (七) 多條件查詢 转载:GridView 空记录时显示 Header 代码重构相关书籍 转:恢复Reflector反编译后资源文件的办法 WPF 回车转Tab实现跳转 C#程序多用户只启动一个进程的方法[转载] C#中自定义快捷键【转载】 转:[WPF] WPF资源收集 分享 转:.NET开发人员必知的八个网站 WatermarkComboBox 和 WatermarkTextBox 转:c#中线程访问winform控件的若干问题 [转] DotNet资源站点汇总 DataKeyNames工作 - 流泉飞石 - 博客园 转:功能很强大的UIHelper类 转:动态修改webservice地址 转:CS结构软件自动升级实现 几个很好的url重写工具 asp.net获取应用程序路径 - 流泉飞石 - 博客园 SqlServer 查询sql执行时间
C#自定义快捷键实现介绍
流泉飞石 · 2009-12-30 · via 博客园 - 流泉飞石

这篇文章以按下Ctrl+Shift+0实现显示桌面为例,采用C#编写的程序代码说明C#自定义快捷键的实现。

    读者可以依此类推,通过按下某些键可以实现一些自定义的功能,只要修改下面代码中RegisterHotKey 的参数和case语句中的执行内容即可。

    下面给的示例程序中关键处都具有注释。

    下面给出一个完整的可运行的C#编写的示例程序

    打开VS2005集成开发环境,新建一个windows应用程序,下面的是Form1.cs的全部代码。

    (说明:要使该程序正确运行,必须把下面代码中的C:\ShowDesktop.scf替换成你本机的“显示桌面.scf”文件所在的路径)

C#自定义快捷键实现代码

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel; 
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8.  
  9. //要使用DllImport语句必须引用该命名空间  
  10. using System.Runtime.InteropServices; 
  11.  
  12. //要使用Process语句必须引用该命名空间  
  13. using System.Diagnostics; 
  14.  
  15. namespace WindowsApplication4  
  16. {  
  17. public partial class Form1 : Form  
  18.  
  19. //user32.dll是非托管代码,不能用命名空间的方式直接引用,
  20. //所以需要用“DllImport”进行引入后才能使用  
  21. [DllImport("user32.dll", SetLastError = true)]  
  22. public static extern bool RegisterHotKey(  
  23. IntPtr hWnd, //要定义热键的窗口的句柄  
  24. int id, //定义热键ID(不能与其它ID重复) 
  25. //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
  26. KeyModifiers fsModifiers,  
  27. Keys vk //定义热键的内容  
  28. ); 
  29.  
  30. [DllImport("user32.dll", SetLastError = true)]  
  31. public static extern bool UnregisterHotKey(  
  32. IntPtr hWnd, //要取消热键的窗口的句柄  
  33. int id //要取消热键的ID 
  34.  
  35. ); 
  36.  
  37. //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)  
  38. [Flags()]  
  39. public enum KeyModifiers  
  40. {  
  41. None = 0,  
  42. Alt = 1,  
  43. Ctrl = 2, 
  44. Shift = 4,  
  45. WindowsKey = 8,  
  46. CtrlAndShift = 6  
  47.  
  48.  
  49. private void Form1_Load(object sender, EventArgs e)  
  50. {  
  51. //注册热键Shift+S,Id号为100。KeyModifiers.Shift也可以直接使用数字4来表示。 
  52. RegisterHotKey(Handle, 100, KeyModifiers.Shift, Keys.S);  
  53. //注册热键Ctrl+B,Id号为101。KeyModifiers.Ctrl也可以直接使用数字2来表示。 
  54. RegisterHotKey(Handle, 101, KeyModifiers.Ctrl, Keys.B);  
  55. //注册热键Alt+D,Id号为102。KeyModifiers.Alt也可以直接使用数字1来表示。  
  56. RegisterHotKey(Handle, 102, KeyModifiers.Alt, Keys.D); 
  57. //注册热键Ctrl+Alt+0,Id号为103。KeyModifiers.CtrlAndAlt也可以直接使用数字3来表示。  
  58. RegisterHotKey(Handle, 103, KeyModifiers.CtrlAndShift, Keys.D0); 
  59.  
  60.  
  61. private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
  62. {  
  63. //注销Id号为100的热键设定  
  64. UnregisterHotKey(Handle, 100);  
  65. //注销Id号为101的热键设定  
  66. UnregisterHotKey(Handle, 101);  
  67. //注销Id号为102的热键设定  
  68. UnregisterHotKey(Handle, 102);  
  69. //注销Id号为103的热键设定  
  70. UnregisterHotKey(Handle, 103);  
  71.  
  72. protected override void WndProc(ref Message m)  
  73. {  
  74. const int WM_HOTKEY = 0x0312;  
  75. //按快捷键  
  76. switch (m.Msg)  
  77. {  
  78. case WM_HOTKEY:  
  79. switch (m.WParam.ToInt32())  
  80. {  
  81. case 100: //按下的是Shift+S  
  82. //此处填写快捷键响应代码  
  83. break;  
  84. case 101: //按下的是Ctrl+B  
  85. //此处填写快捷键响应代码  
  86. break;  
  87. case 102: //按下的是Alt+D  
  88. //此处填写快捷键响应代码  
  89. break;  
  90. case 103: //按下的是Ctrl+Shift+0  
  91. {  
  92. Process Myprocess;  
  93. try 
  94. {  
  95. //这段程序功能为:按下Ctrl+Shift+0后显示桌面  
  96. Myprocess = new System.Diagnostics.Process();  
  97. Myprocess.StartInfo.FileName = @"C:\ShowDesktop.scf";  
  98. Myprocess.StartInfo.Verb = "Open";  
  99. Myprocess.Start();  
  100. }  
  101. catch (Exception ex)  
  102. {  
  103. //程序出错时提示信息  
  104. MessageBox.Show(
  105. ex.Message, "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  106. }  
  107. break;  
  108. }  
  109. }  
  110. break;  
  111. }  
  112. base.WndProc(ref m);  
  113.  
  114. public Form1()  
  115. {  
  116. InitializeComponent();  
  117. }  
  118. }  

    通过上述代码就实现了C#自定义快捷键的设置,大家可以尝试一下。