mixerOpen(&hMixer,0,(DWORD)this->m_hWnd,NULL,MIXER_OBJECTF_MIXER | CALLBACK_WINDOW)
我写了一个音量控制程序,想实现这样功能:
用户通过控制面板或其他程序修改音量后,我的程序能发觉并更新。mixerOpen()函数能够设置消息回调窗口,但我不知道是什么消息?
或者有其他方法,可以发觉系统音量已经改变,但我不想用TIMER

解决方案 »

  1.   

    #include <windows.h>
        #include <mmsystem.h>    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.    // Open the mixer. This opens the mixer with a deviceID of 0. If you
        // have a single sound card/mixer, then this will open it. If you have
        // multiple sound cards/mixers, the deviceIDs will be 0, 1, 2, and
        // so on.
        rc = mixerOpen(&hMixer, 0,0,0,0);
        if (MMSYSERR_NOERROR != rc) {
            // Couldn't open the mixer.
        }    // Initialize MIXERLINE structure.
        ZeroMemory(&mxl,sizeof(mxl));
        mxl.cbStruct = sizeof(mxl);    // Specify the line you want to get. You are getting the input line
        // here. If you want to get the output line, you need to use
        // MIXERLINE_COMPONENTTYPE_SRC_WAVEOUT.
        mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_WAVEIN;    rc = mixerGetLineInfo((HMIXEROBJ)hMixer, &mxl,
                               MIXER_GETLINEINFOF_COMPONENTTYPE);
        if (MMSYSERR_NOERROR == rc) {
            // Couldn't get the mixer line.
        }    // Get the control.
        ZeroMemory(&mxlc, sizeof(mxlc));
        mxlc.cbStruct = sizeof(mxlc);
        mxlc.dwLineID = mxl.dwLineID;
        mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_PEAKMETER;
        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.
        }    // After successfully getting the peakmeter control, the volume range
        // will be specified by mxc.Bounds.lMinimum to mxc.Bounds.lMaximum.    MIXERCONTROLDETAILS mxcd;             // Gets the control values.
        MIXERCONTROLDETAILS_SIGNED volStruct; // Gets the control values.
        long volume;                          // Holds the final volume value.    // Initialize the MIXERCONTROLDETAILS structure
        ZeroMemory(&mxcd, sizeof(mxcd));
        mxcd.cbStruct = sizeof(mxcd);
        mxcd.cbDetails = sizeof(volStruct);
        mxcd.dwControlID = mxc.dwControlID;
        mxcd.paDetails = &volStruct;
        mxcd.cChannels = 1;    // Get the current value of the peakmeter control. Typically, you
        // would set a timer in your program to query the volume every 10th
        // of a second or so.
        rc = mixerGetControlDetails((HMIXEROBJ)hMixer, &mxcd,
                                     MIXER_GETCONTROLDETAILSF_VALUE);
        if (MMSYSERR_NOERROR == rc) {
            // Couldn't get the current volume.
        }
        volume = volStruct.lValue;    // Get the absolute value of the volume.
        if (volume < 0)
            volume = -volume;
      

  2.   

    潜龙ROOT(30101) 19:49:30
    当我们调用mixeropen时可以传递一个窗口的句柄作为回调窗口,当系统的设置改变,比如音量改变,某个音频线路被静音时,mixer都会给我们的回调窗口发送消息的。  一般只有两个消息,如下afx_msg void OnMLChange(WPARAM wParam, LPARAM lParam);
    afx_msg void OnMCChange(WPARAM wParam, LPARAM lParam);ON_MESSAGE(MM_MIXM_LINE_CHANGE, OnMLChange)
    ON_MESSAGE(MM_MIXM_CONTROL_CHANGE, OnMCChange) 
     
    潜龙ROOT(30101) 19:49:47
    其中MM_MIXM_CONTROL_CHANGE 消息中,发送消息的两个参数代表的意思如下wParam = (WPARAM) hMixer 
    lParam = (LPARAM) dwControlID   在MM_MIXM_LINE_CHANGE 消息中,发送消息的参数代表的意思如下wParam = (WPARAM) hMixer 
    lParam = (LPARAM) dwLineID
      

  3.   

    在我们的应用程序中,我们可以在这两个消息处理函数中调整我们的设置,以对应于系统的改变,比如你的代码可以这样写:void CMixerControlDlg::OnMCChange(WPARAM wParam, LPARAM lParam)
    {
     DWORD dwLValue;
     DWORD dwRValue;
     GetVolume(MIXERLINE_COMPONENTTYPE_DST_SPEAKERS, &dwLValue,&dwRValue);
     //GetVolume函数的定义见下面,然后根据返回的值调整滑动条的位置
     m_SliderVolR.SetPos(MAX_VOL_VALUE - dwLValue);
     m_SliderVolL.SetPos(MAX_VOL_VALUE - dwRValue);
     //你也可以在这里调用GetMute查看Volume是否被静音, 
    }   如此你的程序就可以自动的响应系统设置的改变了。