VC中调节系统音量

// #include // link with winmm.lib

MMRESULT rc; // Return code. HMIXER hMixer; // Mixer handle used in mixer API calls. MIXERCONTROL mxc; // Holds the mixer control data. MIXERLINE mxl; // Holds the mixer line data. MIXERLINECONTROLS mxlc; // Obtains the mixer control.

        //   打开mixer.   单声卡的deviceID为0.
        rc   =   mixerOpen(&hMixer;,   0,0,0,0);
        if   (MMSYSERR_NOERROR   !=   rc)   {
                //   Couldn't   open   the   mixer.
        }

        //   初始化MIXERLINE结构体.
        ZeroMemory(&mxl;,sizeof(mxl));
        mxl.cbStruct   =   sizeof(mxl);

        //   指出需要获取的通道,扬声器用MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
        mxl.dwComponentType   =   MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;

        rc   =   mixerGetLineInfo((HMIXEROBJ)hMixer,   &mxl;,
                                                      MIXER_GETLINEINFOF_COMPONENTTYPE);
        if   (MMSYSERR_NOERROR   ==   rc)   {
                //   Couldn't   get   the   mixer   line.
        }

        //   取得控制器.
        ZeroMemory(&mxlc;,   sizeof(mxlc));
        mxlc.cbStruct   =   sizeof(mxlc);
        mxlc.dwLineID   =   mxl.dwLineID;
        mxlc.dwControlType   =   MIXERCONTROL_CONTROLTYPE_VOLUME;
        mxlc.cControls   =   1;
        mxlc.cbmxctrl   =   sizeof(mxc);
        mxlc.pamxctrl   =   &mxc;
        ZeroMemory(&mxc;,   sizeof(mxc));
        mxc.cbStruct   =   sizeof(mxc);
        rc   =   mixerGetLineControls((HMIXEROBJ)hMixer,&mxlc;,
                                                              MIXER_GETLINECONTROLSF_ONEBYTYPE);
        if   (MMSYSERR_NOERROR   !=   rc)   {
                //   Couldn't   get   the   control.
        }

        //   获取控制器中的值的音量范围:mxc.Bounds.lMinimum到mxc.Bounds.lMaximum.

        MIXERCONTROLDETAILS   mxcd;                           //   Gets   the   control   values.
        MIXERCONTROLDETAILS_SIGNED   volStruct;   //   Gets   the   control   values.
        long   volume;                                                     //   Holds   the   final   volume   value.

        //   初始化MIXERCONTROLDETAILS结构体
        ZeroMemory(&mxcd;,   sizeof(mxcd));
        mxcd.cbStruct   =   sizeof(mxcd);
        mxcd.cbDetails   =   sizeof(volStruct);
        mxcd.dwControlID   =   mxc.dwControlID;
        mxcd.paDetails   =   &volStruct;
        mxcd.cChannels   =   1;

        //   获得音量值
        rc   =   mixerGetControlDetails((HMIXEROBJ)hMixer,   &mxcd;,
                                                                  MIXER_GETCONTROLDETAILSF_VALUE);
        if   (MMSYSERR_NOERROR   ==   rc)   {
                //   Couldn't   get   the   current   volume.
        }
        volume   =   volStruct.lValue;

volStruct.lValue   =   25000; //想要设置的值
rc   =   mixerSetControlDetails((HMIXEROBJ)hMixer,   &mxcd;,
MIXER_SETCONTROLDETAILSF_VALUE);
支持原创技术分享,据说打赏我的人,都找到了女朋友!