C#自定義快捷鍵的實(shí)現(xiàn)
這篇文章以按下Ctrl+Shift+0實(shí)現(xiàn)顯示桌面為例,采用C#編寫的程序代碼說(shuō)明C#自定義快捷鍵的實(shí)現(xiàn)。
讀者可以依此類推,通過(guò)按下某些鍵可以實(shí)現(xiàn)一些自定義的功能,只要修改下面代碼中RegisterHotKey 的參數(shù)和case語(yǔ)句中的執(zhí)行內(nèi)容即可。
下面給的示例程序中關(guān)鍵處都具有注釋。
下面給出一個(gè)完整的可運(yùn)行的C#編寫的示例程序
打開(kāi)VS2005集成開(kāi)發(fā)環(huán)境,新建一個(gè)windows應(yīng)用程序,下面的是Form1.cs的全部代碼。
(說(shuō)明:要使該程序正確運(yùn)行,必須把下面代碼中的C:\ShowDesktop.scf替換成你本機(jī)的“顯示桌面.scf”文件所在的路徑)
C#自定義快捷鍵實(shí)現(xiàn)代碼
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- //要使用DllImport語(yǔ)句必須引用該命名空間
- using System.Runtime.InteropServices;
- //要使用Process語(yǔ)句必須引用該命名空間
- using System.Diagnostics;
- namespace WindowsApplication4
- {
- public partial class Form1 : Form
- {
- //user32.dll是非托管代碼,不能用命名空間的方式直接引用,所以需要用“DllImport”進(jìn)行引入后才能使用
- [DllImport("user32.dll", SetLastError = true)]
- public static extern bool RegisterHotKey(
- IntPtr hWnd, //要定義熱鍵的窗口的句柄
- int id, //定義熱鍵ID(不能與其它ID重復(fù))
- KeyModifiers fsModifiers, //標(biāo)識(shí)熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時(shí)才會(huì)生效
- Keys vk //定義熱鍵的內(nèi)容
- );
- [DllImport("user32.dll", SetLastError = true)]
- public static extern bool UnregisterHotKey(
- IntPtr hWnd, //要取消熱鍵的窗口的句柄
- int id //要取消熱鍵的ID
- );
- //定義了輔助鍵的名稱(將數(shù)字轉(zhuǎn)變?yōu)樽址员阌谟洃洠部扇コ嗣杜e而直接使用數(shù)值)
- [Flags()]
- public enum KeyModifiers
- {
- None = 0,
- Alt = 1,
- Ctrl = 2,
- Shift = 4,
- WindowsKey = 8,
- CtrlAndShift = 6
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- //注冊(cè)熱鍵Shift+S,Id號(hào)為100。KeyModifiers.Shift也可以直接使用數(shù)字4來(lái)表示。
- RegisterHotKey(Handle, 100, KeyModifiers.Shift, Keys.S);
- //注冊(cè)熱鍵Ctrl+B,Id號(hào)為101。KeyModifiers.Ctrl也可以直接使用數(shù)字2來(lái)表示。
- RegisterHotKey(Handle, 101, KeyModifiers.Ctrl, Keys.B);
- //注冊(cè)熱鍵Alt+D,Id號(hào)為102。KeyModifiers.Alt也可以直接使用數(shù)字1來(lái)表示。
- RegisterHotKey(Handle, 102, KeyModifiers.Alt, Keys.D);
- //注冊(cè)熱鍵Ctrl+Alt+0,Id號(hào)為103。KeyModifiers.CtrlAndAlt也可以直接使用數(shù)字3來(lái)表示。
- RegisterHotKey(Handle, 103, KeyModifiers.CtrlAndShift, Keys.D0);
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- //注銷Id號(hào)為100的熱鍵設(shè)定
- UnregisterHotKey(Handle, 100);
- //注銷Id號(hào)為101的熱鍵設(shè)定
- UnregisterHotKey(Handle, 101);
- //注銷Id號(hào)為102的熱鍵設(shè)定
- UnregisterHotKey(Handle, 102);
- //注銷Id號(hào)為103的熱鍵設(shè)定
- UnregisterHotKey(Handle, 103);
- }
- protected override void WndProc(ref Message m)
- {
- const int WM_HOTKEY = 0x0312;
- //按快捷鍵
- switch (m.Msg)
- {
- case WM_HOTKEY:
- switch (m.WParam.ToInt32())
- {
- case 100: //按下的是Shift+S
- //此處填寫快捷鍵響應(yīng)代碼
- break;
- case 101: //按下的是Ctrl+B
- //此處填寫快捷鍵響應(yīng)代碼
- break;
- case 102: //按下的是Alt+D
- //此處填寫快捷鍵響應(yīng)代碼
- break;
- case 103: //按下的是Ctrl+Shift+0
- {
- Process Myprocess;
- try
- {
- //這段程序功能為:按下Ctrl+Shift+0后顯示桌面
- Myprocess = new System.Diagnostics.Process();
- Myprocess.StartInfo.FileName = @"C:\ShowDesktop.scf";
- Myprocess.StartInfo.Verb = "Open";
- Myprocess.Start();
- }
- catch (Exception ex)
- {
- //程序出錯(cuò)時(shí)提示信息
- MessageBox.Show(ex.Message, "信息提示!", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- break;
- }
- }
- break;
- }
- base.WndProc(ref m);
- }
- public Form1()
- {
- InitializeComponent();
- }
- }
- }
通過(guò)上述代碼就實(shí)現(xiàn)了C#自定義快捷鍵的設(shè)置,大家可以嘗試一下。
【編輯推薦】