程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#利用WIN32實現按鍵注冊

C#利用WIN32實現按鍵注冊

編輯:C#基礎知識
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System;
using System.Windows.Forms;

namespace Phoenix
{
    //注冊系統按鍵消息
    class HotKeys
    {
        [DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);
        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        public enum HotkeyModifiers
        {
            Alt = 1, Control = 2, Shift = 4, Win = 8
        }

        public delegate void HotkeyCallbackHandler();
        int keyId = 10;
        Dictionary<int, HotkeyCallbackHandler> keyMap = new Dictionary<int, HotkeyCallbackHandler>();

        /// <summary>
        /// 注冊快捷鍵
        /// </summary>
        /// <param name="hWnd">持有快捷鍵窗口的句柄</param>
        /// <param name="modifiers">組合鍵</param>
        /// <param name="vk">快捷鍵的虛擬碼</param>
        /// <param name="callback">回調函數,在按下快捷鍵後調用</param>
        public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotkeyCallbackHandler callback)
        {
            int id = keyId++;
            if (!RegisterHotKey(hWnd, id, modifiers, vk))
            {
                throw new Exception("RegisterHotKey Error!");
            }
            keyMap[id] = callback;
        }
        public void UnRegist(IntPtr hWnd, HotkeyCallbackHandler callback)
        {
            foreach (var item in keyMap)
            {
                if (item.Value == callback)
                    UnregisterHotKey(hWnd, item.Key);
            }
        }

        public void ProcessHotKey(Message message)
        {
            if (message.Msg == 0x312)
            {
                int id = message.WParam.ToInt32();
                HotkeyCallbackHandler handler;
                if (keyMap.TryGetValue(id, out handler))
                    handler();
            }
        }
    }
}
//http://hovertree.com/

using System;
using System.Collections.Generic;

using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Phoenix
{

    class MyForm : Form
    {
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string className, string titleName);
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr child, string className, string formText);
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, string lParam);


        HotKeys hotKey = new HotKeys();

        protected override void WndProc(ref Message message)
        {
            hotKey.ProcessHotKey(message);
            base.WndProc(ref message);
        }

        
        //跨越程序輸入,向記事本的文本框寫入指定字符
        public void callBack()
        {
            const int WM_SETTEXT = 0x00c;
            IntPtr hWndNotepad = FindWindow(null, "無標題 - 記事本");
            IntPtr hTextbox1 = FindWindowEx(hWndNotepad, IntPtr.Zero, "EDIT", null);
            IntPtr hTextbox2 = FindWindowEx(hWndNotepad, hTextbox1, "EDIT", null);
            SendMessage(hTextbox1, WM_SETTEXT, IntPtr.Zero, this.Text);
        }

        static void Main(string[] args)
        {
            MyForm form = new MyForm() { Text = "C#模擬鍵盤輸入" };
            form.Load += delegate
             {
                 //register Ctrl+E 
                 form.hotKey.Regist(form.Handle, (int)HotKeys.HotkeyModifiers.Control, Keys.E, form.callBack);
             };
            Application.Run(form);

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