C#自定义快捷键实现介绍

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

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