代码如下:
winmm
[DllImport("winmm.dll", EntryPoint = "waveOutSetVolume")]
public static extern int WaveOutSetVolume(IntPtr hwo, uint dwVolume);
private void SetVol(double arg){
double newVolume = ushort.MaxValue * arg / 10.0;
uint v = ((uint)newVolume) & 0xffff;
uint vAll = v | (v 16);
int retVal = WaveOutSetVolume(IntPtr.Zero, vAll);
}
2.user32控制方式:
代码如下:
user32
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
public void SetVol(){
p = Process.GetCurrentProcess();
for (int i = 0; i 5; i++) {
SendMessageW(p.Handle, WM_APPCOMMAND, p.Handle, (IntPtr)APPCOMMAND_VOLUME_UP);
}
}
private Process p;
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int APPCOMMAND_VOLUME_UP = 0x0a0000;
private const int APPCOMMAND_VOLUME_DOWN = 0x090000;
private const int WM_APPCOMMAND = 0x319;
3.CoreAudioApi
代码如下:
CoreAudioApi
Using CoreAudioApi;
public void SetVol(double arg) {
device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
device.AudioEndpointVolume.MasterVolumeLevelScalar = (float)arg;
}
private MMDevice device;
private MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();