C# 设置软件扬声器音量大小
public static class VolumeHelper
{
[DllImport("Winmm.dll")]
private static extern int waveOutSetVolume(int hwo, System.UInt32 pdwVolume);
[DllImport("Winmm.dll")]
private static extern uint waveOutGetVolume(int hwo, out System.UInt32 pdwVolume);
public static double CurrentVolume = 50;
public static void SetVolume(double volumeSize)
{
if (volumeSize < 0)
volumeSize = 0;
if (volumeSize > 100)
volumeSize = 100;
System.UInt32 Value = (System.UInt32)((double)0xffff * (double)volumeSize / 100.0);//先把trackbar的value值映射到0x0000~0xFFFF范围
//限制value的取值范围
if (Value < 0)
Value = 0;
if (Value > 0xffff)
Value = 0xffff;
System.UInt32 left = (System.UInt32)Value;//左声道音量
System.UInt32 right = (System.UInt32)Value;//右
waveOutSetVolume(0, left << 16 | right); //"<<"左移,“|”逻辑或运算
CurrentVolume = volumeSize;
}
public static int GetVolume()
{
System.UInt32 value = 0;
waveOutGetVolume(0, out value);
return (int)(value >> 16);
}
}
**************************************************************************************************************