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;

namespace DataSource
{
    public partial class HotKey : Form
    {
        [DllImport("user32")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);
        //注册热键的api
        [DllImport("user32")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);


        public HotKey()
        {
            InitializeComponent();
        }

        private void HotKey_Load(object sender, EventArgs e)
        {
            //注册热键(窗体句柄,热键ID,辅助键,实键)
            RegisterHotKey(this.Handle, 888, 3, Keys.B);

        }

        private void HotKey_FormClosing(object sender, FormClosingEventArgs e)
        {
            //注消热键(句柄,热键ID)
            UnregisterHotKey(this.Handle, 888);

        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x0312: //这个是window消息定义的 注册的热键消息
                    if (m.WParam.ToString().Equals("888")) //如果是我们注册的那个热键
                        if (this.Visible == false)
                        {
                            this.Visible = true;
                        }
                        else
                        {
                            this.Visible = false;
                        }
                    break;
            }
            base.WndProc(ref m);
        }

        //辅助键说明:
        //None = 0,
        //Alt = 1,
        //crtl= 2,
        //Shift = 4,
        //Windows = 8

    }
}

posted on 2010-04-16 17:58  aparche  阅读(273)  评论(0编辑  收藏  举报