private static int GetVolume() { int result = 0; InvokeTryCatch("WinXpVolumeOperate.GetVolume", () => { int currVolume; int mixerControl; WinXpDllInterface.mixerOpen(out mixerControl, 0, 0, 0, 0); uint type = WinXpDllInterface.MIXERCONTROL_CONTROLTYPE_VOLUME; InnerGetMixer(mixerControl, WinXpDllInterface.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, type, out currVolume); WinXpDllInterface.mixerClose(mixerControl); result = currVolume; }); return(result); }
private static void SetVolume(int volume) { InvokeTryCatch("WinXpVolumeOperate.SetVolume", () => { int currVolume; int mixerControl; WinXpDllInterface.mixerOpen(out mixerControl, 0, 0, 0, 0); uint controlType = WinXpDllInterface.MIXERCONTROL_CONTROLTYPE_VOLUME; WinXpDllInterface.MIXER mixer = InnerGetMixer(mixerControl, WinXpDllInterface.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out currVolume); bool setSucceed = false; for (int i = 0; i < 3; i++) { if (volume > mixer.lMaximum) { volume = mixer.lMaximum; } if (volume < mixer.lMinimum) { volume = mixer.lMinimum; } InnerSetMixer(mixerControl, mixer, volume); mixer = InnerGetMixer(mixerControl, WinXpDllInterface.MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, controlType, out currVolume); if (volume == currVolume) { setSucceed = true; break; } //如果设置失败, 则最多重试3次 } WinXpDllInterface.mixerClose(mixerControl); if (!setSucceed) { throw new Exception("多次尝试设置 Xp操作系统音量 失败 (该异常可能是偶发异常, 可以忽略)!"); } }); }