一种比较少见的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.Runtime.InteropServices;

namespace CSharp_Message
{
    [Flags()]
    public enum KeyModifiers
    {
        None=0,
        Alt=1,
        Ctrl=2,
        Shift=4,
        WindowKey=5
    }
    public partial class Form3 : Form
    {
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegistryHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        public Form3()
        {
            InitializeComponent();
            RegistryHotKey(this.Handle, 100, KeyModifiers.None, Keys.Escape);
            RegistryHotKey(this.Handle, 101, KeyModifiers.Ctrl, Keys.F);
            RegistryHotKey(this.Handle, 102, KeyModifiers.Ctrl, Keys.A);
        }

        protected override void WndProc(ref Message m)
        {
            try
            {
                const int WM_HOTKEY = 0x0312;
                switch (m.Msg)
                {
                    case WM_HOTKEY:
                        switch(m.WParam.ToInt32())
                        {
                            case 101:
                                //...
                                break;
                            case 100:
                                //...
                                break;
                        }
                        break;
                    case 104:
                        break;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            base.WndProc(ref m);
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(this.Handle, 100);
            UnregisterHotKey(this.Handle, 101);
            UnregisterHotKey(this.Handle, 102);
        }
    }
}

转载于:https://www.cnblogs.com/QQ122252656/p/4293706.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值