Delphi Vista以上系统的进程静音

2018-10-30

单个进程的音量控制是从Vista系统中才开始支持的,所以此函数需要在Vista及以上的系统中使用。
XP下面使用这个函数会导致系统全局静音。所以在XP下要使进程静音要使用Hook技术.

Delphi XE + Win7 x64 编译测试通过


uses

  MMSystem;


function EnableMute(const Mute: Boolean): Boolean;

var

  hmx: HMIXER;

  mxlc: MIXERLINECONTROLS;

  mxl: MIXERLINE;

  mxc: MIXERCONTROL;

  mxcd: tMIXERCONTROLDETAILS;

  mxcdMute: MIXERCONTROLDETAILS_BOOLEAN;

begin

  Result := False;


  if Win32MajorVersion < 6 then

    Exit;


  if mixerOpen(@hmx, 0, 0, 0, 0) <> MMSYSERR_NOERROR then

    Exit;


  mxl.cbStruct := SizeOf(mxl);

  mxl.dwComponentType := MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;

  if mixerGetLineInfo(hmx, @mxl, MIXER_GETLINEINFOF_COMPONENTTYPE) = MMSYSERR_NOERROR then

  begin

    mxlc.cbStruct := SizeOf(MIXERLINECONTROLS);

    mxlc.dwLineID := mxl.dwLineID;

    mxlc.dwControlType := MIXERCONTROL_CONTROLTYPE_MUTE;

    mxlc.cControls := 1;

    mxlc.cbmxctrl := SizeOf(MIXERCONTROL);

    mxlc.pamxctrl := @mxc;

    if mixerGetLineControls(hmx, @mxlc, MIXER_GETLINECONTROLSF_ONEBYTYPE) = MMSYSERR_NOERROR then

    begin

      mxcdMute.fValue := Integer(Mute);

      mxcd.hwndOwner := 0;

      mxcd.dwControlID := mxc.dwControlID;

      mxcd.cbStruct := SizeOf(mxcd);

      mxcd.cbDetails := SizeOf(mxcdMute);

      mxcd.paDetails := @mxcdMute;

      mxcd.cChannels := 1;

      mxcd.cMultipleItems := 0;

      Result := mixerSetControlDetails(hmx, @mxcd, MIXER_OBJECTF_HMIXER or MIXER_SETCONTROLDETAILSF_VALUE) = MMSYSERR_NOERROR;

    end;

  end;


  mixerClose(hmx);

end;


阅读12