c#系统热键和快捷键

c#系统热键和快捷键

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 WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        //注册热键的api 

        [DllImport("user32.dll")]

        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk);

        [DllImport("user32.dll")]

        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        //系统音量API

        [DllImport("winmm.dll")]

        public static extern UInt32 waveOutSetVolume(UInt32 deviceID, UInt32 Volume); //设置音量

        [DllImport("winmm.dll")]

        public static extern UInt32 waveOutGetVolume(UInt32 deviceID, ref UInt32 Volume); //获取音量

         public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            //窗体锚定在0,0

            this.Location = new Point(-100, -100);

            this.Height = 20;

            this.Width = 20;

            this.Activate();

            //注册热键(窗体句柄,热键ID,辅助键,实键)

            RegisterHotKey(this.Handle, 225, 0, Keys.VolumeUp);

            RegisterHotKey(this.Handle, 226, 0, Keys.VolumeDown);

            RegisterHotKey(this.Handle, 227, 0, Keys.VolumeMute);


            //====================================================

            //注册热键Shift+S,Id号为100。HotKey.KeyModifiers.Shift也可以直接使用数字4来表示。

            //HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S);

            ////注册热键Ctrl+B,Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。

            //HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.B);

            ////注册热键Alt+D,Id号为102。HotKey.KeyModifiers.Alt也可以直接使用数字1来表示。

            //HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt, Keys.D);

            //====================================================================

            //注册热键Ctrl+B,Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。

            RegisterHotKey(Handle, 101, (uint)KeyModifiers.Ctrl, Keys.B);

          }

        //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)

        [Flags()]

        public enum KeyModifiers

        {

            None = 0,

            Alt = 1,

            Ctrl = 2,

            Shift = 4,

            WindowsKey = 8

        }

        protected override void WndProc(ref Message m)

        {

            switch (m.Msg)

            {

                case 0x0312:    //这个是window消息定义的注册的热键消息 

                    if (m.WParam.ToString().Equals("225"))  //提高音量热键 

                    {

                         MessageBox.Show("你按了VolumeUp ");

                    }

                    else if (m.WParam.ToString().Equals("226"))  //降低音量热键 

                    {

                         MessageBox.Show("你按了VolumeDown ");

                    }

                    else if (m.WParam.ToString().Equals("227"))  //静音热键 

                    {

                         MessageBox.Show("你按了VolumeMute");

                    }

                    else if (m.WParam.ToString().Equals("101"))  // 

                    {

                        MessageBox.Show("你按了Ctrl+B");

                    }

                    break;

            }

            base.WndProc(ref m);

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)

        {

            //注消热键(句柄,热键ID)

            UnregisterHotKey(this.Handle, 225);

            UnregisterHotKey(this.Handle, 226);

            UnregisterHotKey(this.Handle, 227);

            UnregisterHotKey(this.Handle, 101);

        }

    }

posted @ 2010-01-06 15:57  星释天狼  阅读(1553)  评论(0)    收藏  举报