C# 关于自定义快捷键研究

这篇文章采用C#编写的程序代码说明C#自定义快捷键的实现。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;    
using System.Diagnostics;
using System.Runtime.InteropServices;              //要使用DllImport语句必须引用该命名空间  
using System.Diagnostics;                        //要使用Process语句必须引用该命名空间 


namespace tsts
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
        IntPtr hWnd,             //要定义热键的窗口的句柄   
        int id,                        //定义热键ID(不能与其它ID重复)  
        KeyModifiers fsModifiers,         //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效 
        Keys vk                   //定义热键的内容   
        );
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
        IntPtr hWnd,             //要取消热键的窗口的句柄   
        int id                        //要取消热键的ID  
        );
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8,
            CtrlAndShift = 6
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //注册热键Shift+S,Id号为100。KeyModifiers.Shift也可以直接使用数字4来表示。  
            RegisterHotKey(Handle, 100, KeyModifiers.Shift, Keys.S);
            //注册热键Ctrl+B,Id号为101。KeyModifiers.Ctrl也可以直接使用数字2来表示。  
            RegisterHotKey(Handle, 101, KeyModifiers.Ctrl, Keys.B);
            //注册热键Alt+D,Id号为102。KeyModifiers.Alt也可以直接使用数字1来表示。   
            RegisterHotKey(Handle, 102, KeyModifiers.Alt, Keys.D);
            //注册热键Ctrl+Alt+0,Id号为103。KeyModifiers.CtrlAndAlt也可以直接使用数字3来表示。   
            RegisterHotKey(Handle, 103, KeyModifiers.CtrlAndShift, Keys.D0);  


        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //注销Id号为100的热键设定   
            UnregisterHotKey(Handle, 100);
            //注销Id号为101的热键设定   
            UnregisterHotKey(Handle, 101);
            //注销Id号为102的热键设定   
            UnregisterHotKey(Handle, 102);
            //注销Id号为103的热键设定   
            UnregisterHotKey(Handle, 103);


        }
//ref功能:  ref 关键字使参数按引用传递。其效果是,当控制权传递回调用方法时,在方法中对参数所做的任何更改都将反映在该变量中。简单点说就是,使用了refout的效果就几乎和C中使用了指针变量一样。它能够让你直接对原数进行操作,而不是对那个原数的Copy进行操作。

      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   
            //此处填写快捷键响应代码   
            break;   
            case 101: //按下的是Ctrl+B   
            MessageBox.Show("SB"); 
            break;   
            case 102: //按下的是Alt+D   
        MessageBox.Show("D");
            break;  
            }   
            break;   
            }   
            base.WndProc(ref m);   
        }  
 
        public Form1()
        {         
            InitializeComponent();
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值