程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#實現注冊全局熱鍵(register hot key)

C#實現注冊全局熱鍵(register hot key)

編輯:C#入門知識

想實現注冊類似於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);
        }
    }
}
 

    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved