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;
using System.Threading;

namespace AutoSendMessages
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region AIP

        //若是函數執行成功,返回值不爲0。
        //若是函數執行失敗,返回值爲0。要獲得擴展錯誤信息,調用GetLastError。
        [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
            );

        //定義了輔助鍵的名稱(將數字轉變爲字符以便於記憶,也可去除此枚舉而直接使用數值)
        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }

        //獲取鼠標當前位置
        [DllImport("user32.dll")]
        private static extern bool GetCursorPos(out Point p);

        //設置鼠標位置
        [DllImport("User32")]
        public extern static void SetCursorPos(int x, int y);

        //模擬鼠標
        [DllImport("User32")]
        public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);

        public enum MouseEventFlags
        {
            Move = 0x0001, //移動鼠標
            LeftDown = 0x0002,//模擬鼠標左鍵按下
            LeftUp = 0x0004,//模擬鼠標左鍵擡起
            RightDown = 0x0008,//鼠標右鍵按下
            RightUp = 0x0010,//鼠標右鍵擡起
            MiddleDown = 0x0020,//鼠標中鍵按下 
            MiddleUp = 0x0040,//中鍵擡起
            Wheel = 0x0800,
            Absolute = 0x8000//標示是否採用絕對座標
        }

        #endregion

        private void Form1_Load(object sender, EventArgs e)
        {
            RegisterHotKey(Handle, 100, KeyModifiers.None, Keys.F7);//F7 鎖定消息框位置
            RegisterHotKey(Handle, 101, KeyModifiers.None, Keys.F8);//F8 開始 / 暫停
            RegisterHotKey(Handle, 102, KeyModifiers.None, Keys.F9);//F9 隱藏 / 顯示

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            if (MessageBox.Show("你是否要退出?","退出提示",MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
            {
                e.Cancel = false;
            }
        }

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

        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷鍵 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 100:    // F7 鎖定位置
                            FindPoint();     
                            break;
                        case 101:    //F8 開始 / 暫停
                            MakeStart();
                            break;
                        case 102:    //F9 隱藏 / 顯示
                            MakeShow();
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }

        Point CP = new Point();

        //尋找位置
        private void FindPoint()
        {
            GetCursorPos(out CP);
            label5.Text = "(" + CP.X + " , "+ CP.Y + ")";
        }
        //開始/暫停
        private void MakeStart()
        {
            int t_interval = 0;
            int.TryParse(textBox2.Text.ToString(), out t_interval);
            if (t_interval < 100)
            {
                t_interval = 100;
            }
            timer1.Interval = t_interval;
            timer1.Enabled = !timer1.Enabled;
        }
        //隱藏/顯示
        private void MakeShow()
        {
            this.Visible = !this.Visible;
        }

        int indexs = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            indexs++;
            SendMessage();
        }
        //發送消息
        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        public const int WM_KEYDOWN = 256;
        public const int WM_KEYUP = 257;

        private void SendMessage()
        {
            if (textBox1.Text.ToString() != "")
            {
                MoveMouse();
                DoubleMouse();
                //Thread.Sleep(100);

                keybd_event(Keys.ControlKey, 0, 0, 0);
                keybd_event(Keys.A, 0, 0, 0);
                keybd_event(Keys.A, 0, 0x2, 0);
                keybd_event(Keys.ControlKey, 0, 0x2, 0);

                Thread.Sleep(10);

                SendKeys.SendWait(textBox1.Text.ToString());
                SendKeys.SendWait("{ENTER}");
            }
        }
        //移動鼠標
        private void MoveMouse()
        {
            SetCursorPos(CP.X, CP.Y);
        }
        //雙擊鼠標
        private void DoubleMouse()
        {
            mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);
            mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);

            //mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);
            //mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 0, 0, 0, IntPtr.Zero);
        }
    }
}
View Code
相關文章
相關標籤/搜索