文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C#实现注册全局热键(register hot key)

C#实现注册全局热键(register hot key)

时间:2010-12-13  来源:网络小筑


想实现注册类似于ctr+alt+shit+A+Z的方法很简单,将RegisterHotKey的第3个参数设置为KeyModifiers.Alt|KeyModifiers.Control|KeyModifiers.Shift,
第4个参数设置为Keys.B|Keys.Z。
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace rgHotKeys
{
    public enum KeyModifiers
    {
        None = 0,
        Alt = 1,
        Control = 2,
        Shift = 4,
        Windows = 8
    }
    public partial class Form1 : Form
    {
        [DllImport("user32.dll",SetLastError=true)]
        public static extern bool  RegisterHotKey(IntPtr hwnd,int id,int fsModifiers,int vk);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
         IntPtr hWnd, // handle to window
         int id // hot key identifier
        );
        private int id;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            id = Thread.CurrentThread.GetHashCode();
            RegisterHotKey(this.Handle, id, (int)KeyModifiers.Alt, (int)Keys.F12);
        }
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            switch (m.Msg)
            {
                case WM_HOTKEY:
                  if(id==(int)m.WParam)
                  {
                            System.Windows.Forms.MessageBox.Show ("你好!");
                    }
               break;
            }
            base.WndProc(ref m);
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            UnregisterHotKey(this.Handle, 10001);
        }
    }
}

 

 

相关阅读 更多 +
排行榜 更多 +
《怪物猎人:荒野》卧龙苍天陨落青龙偃月刀MOD

《怪物猎人:荒野》卧龙苍天陨落青龙偃月刀MOD

下载
《怪物猎人:荒野》只狼格挡特效MOD

《怪物猎人:荒野》只狼格挡特效MOD

下载
《艾尔登法环》黑夜君临学者盔甲MOD

《艾尔登法环》黑夜君临学者盔甲MOD

下载