C# 对windows 音量调节

using System.Runtime.InteropServices; //加入命名空间

[DllImport("winmm.dll")]   //引用winmm.dll
        public static extern long waveOutSetVolume(uint deviceID, uint Volume);      //参数为uint

//音量调节
        private void progressBar1_MouseDown(object sender, MouseEventArgs e)
        {
            int tmp = 46 + e.X;
            if (tmp > 160) tmp = 160;
            labelVoiceUpDown.Location = new Point(tmp, 8);
            progressBar1.Value = e.X;
            UInt32 left = (UInt32)progressBar1.Value * 500;//左声道音量
            UInt32 right= (UInt32)progressBar1.Value *500;//右声道音量
            waveOutSetVolume(0, left<<16|right);
        }

 

一、引入API定义

[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;

二、 提高音量

 

SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);

三、 降低音量

SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);

四、 静音与恢复(执行2次恢复原状态)

SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000);

 

你可能感兴趣的:(C# 对windows 音量调节)