Windows Hotkey control volume

来源:互联网 发布:理财入门知乎 编辑:程序博客网 时间:2024/06/12 00:09
using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace HotkeyTest
{
    public partial class Form1 : Form
    {         


        public Form1()
        {
            InitializeComponent();
        }


        private int second=3;
        private int counter;


        Image turnOFF = Resource1.OFF;
        Image turnON = Resource1.ON;


        private const int WM_HOTKEY = 0x312; //窗口消息:热键
        private const int WM_CREATE = 0x1; //窗口消息:创建
        private const int WM_DESTROY = 0x2; //窗口消息:销毁


        private const int HotKeyVOLUME_UPID = 1; //热键ID(自定义)
        private const int HotKeyVOLUME_DOWN = 2; //热键ID(自定义)
        private const int HotKeyVOLUME_MUTE = 3; //热键ID(自定义)


        //音量
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
        const uint WM_APPCOMMAND = 0x319;
        const uint APPCOMMAND_VOLUME_UP = 0x0a;
        const uint APPCOMMAND_VOLUME_DOWN = 0x09;
        const uint APPCOMMAND_VOLUME_MUTE = 0x08;


        //picturebox show time 
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.counter <= this.second)
            {
                this.Refresh();
                this.counter++;
                Debug.WriteLine("-------timer start----");
            }
            else
            {
                this.timer1.Enabled = false;
                pictureBox_OFF.Visible = false;
                this.timer1.Stop();
               // this.Close();
                Debug.WriteLine("-------timer Close----");
            }
        }


        protected override void WndProc(ref Message msg)
        {
            base.WndProc(ref msg);
            switch (msg.Msg)
            {
                case WM_HOTKEY: //窗口消息:热键
                    int tmpWParam = msg.WParam.ToInt32();
                    
                    if (tmpWParam == HotKeyVOLUME_UPID)
                    {
      
                        //加音量 
                        //SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
                        pictureBox_OFF.Image = turnOFF;
                                                
                        this.counter = 0;
                        this.timer1.Enabled = true;
                        this.timer1.Interval = 1000;
                        this.timer1.Start();
                        Debug.WriteLine("按了F6");


                    }
                   
                  
                    if (tmpWParam == HotKeyVOLUME_DOWN)
                    {
                        //减音量 
                        //SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);
                        Debug.WriteLine("按了F7");
                        pictureBox_OFF.Image = turnON;
                        this.counter = 0;
                        this.timer1.Enabled = true;
                        this.timer1.Interval = 1000;
                        this.timer1.Start();
                    }
                    
                    if (tmpWParam == HotKeyVOLUME_MUTE)
                    {
                        //静音 
                        Debug.WriteLine("按了F5");
                        SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000);
                        Debug.WriteLine("静量");
                    }


                    break;
                case WM_CREATE: //窗口消息:创建
                    SystemHotKey.RegHotKey(this.Handle, HotKeyVOLUME_UPID, SystemHotKey.KeyModifiers.None, Keys.F6);
                    SystemHotKey.RegHotKey(this.Handle, HotKeyVOLUME_DOWN, SystemHotKey.KeyModifiers.None, Keys.F7);
                    SystemHotKey.RegHotKey(this.Handle, HotKeyVOLUME_MUTE, SystemHotKey.KeyModifiers.None, Keys.F5);
                    break;
                case WM_DESTROY: //窗口消息:销毁
                    SystemHotKey.UnRegHotKey(this.Handle, HotKeyVOLUME_UPID); //销毁热键
                    SystemHotKey.UnRegHotKey(this.Handle, HotKeyVOLUME_DOWN); //销毁热键
                    SystemHotKey.UnRegHotKey(this.Handle, HotKeyVOLUME_MUTE); //销毁热键
                    break;
                default:
                    break;
            }
            
        }
        
        
      
    }
}
0 0