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.Diagnostics;
using  System.Runtime.InteropServices;   


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

        
        [DllImport(
"user32.dll", EntryPoint = "SendMessageA")]
        
public static extern int SendMessage(IntPtr handle, int wMsg, int wParam, int lParam);   

        
private const int WM_APPCOMMAND = 0x319;
        
private const int APPCOMMAND_VOLUME_UP = 10;
        
private const int APPCOMMAND_VOLUME_DOWN = 9;
        
private const int APPCOMMAND_VOLUME_MUTE = 8;

        
private void button1_Click(object sender, EventArgs e)
       
{
            SendMessage(Handle, WM_APPCOMMAND, 
0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
            
        }


        
private void button2_Click(object sender, EventArgs e)
       
{
            SendMessage(Handle, WM_APPCOMMAND, 
0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);
        }


        
private void button3_Click(object sender, EventArgs e)
       
{
            SendMessage(Handle, WM_APPCOMMAND, 
0x200EB0, APPCOMMAND_VOLUME_MUTE * 0x10000);
        }



    }

}

三个按钮分别为增加音量,减少音量,静音

以下代码捕捉系统消息,改变系统声音音量



protected   override   void  WndProc( ref  Message m) // 监视Windows消息
        {
            
const int WM_APPCOMMAND = 0x319;

            
switch (m.Msg)
           
{
                
case WM_APPCOMMAND:
                   
{
                        MessageBox.Show(m.ToString());
                    }

                
break;
            }

            
            
base.WndProc(ref m); //将系统消息传递自父类的WndProc
        }

转载于:https://www.cnblogs.com/zhahost/archive/2008/06/02/1212416.html

你可能感兴趣的:(c# 系统音量的控制)