VolumeDlg.cpp
上传用户:sdpcwz
上传日期:2009-12-14
资源大小:1237k
文件大小:8k
- // VolumeDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Volume.h"
- #include "VolumeDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CAboutDlg dialog used for App About
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // Dialog Data
- //{{AFX_DATA(CAboutDlg)
- enum { IDD = IDD_ABOUTBOX };
- //}}AFX_DATA
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CAboutDlg)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- //{{AFX_MSG(CAboutDlg)
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
- {
- //{{AFX_DATA_INIT(CAboutDlg)
- //}}AFX_DATA_INIT
- }
- void CAboutDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CAboutDlg)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
- //{{AFX_MSG_MAP(CAboutDlg)
- // No message handlers
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CVolumeDlg dialog
- CVolumeDlg::CVolumeDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CVolumeDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CVolumeDlg)
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CVolumeDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CVolumeDlg)
- DDX_Control(pDX, IDC_SLIDER, m_ctrlSlider);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CVolumeDlg, CDialog)
- //{{AFX_MSG_MAP(CVolumeDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_DESTROY()
- ON_WM_HSCROLL()
- ON_MESSAGE(MM_MIXM_CONTROL_CHANGE, OnMixerCtrlChange)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CVolumeDlg message handlers
- BOOL CVolumeDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- // IDM_ABOUTBOX must be in the system command range.
- ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
- ASSERT(IDM_ABOUTBOX < 0xF000);
- CMenu* pSysMenu = GetSystemMenu(FALSE);
- if (pSysMenu != NULL)
- {
- CString strAboutMenu;
- strAboutMenu.LoadString(IDS_ABOUTBOX);
- if (!strAboutMenu.IsEmpty())
- {
- pSysMenu->AppendMenu(MF_SEPARATOR);
- pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
- }
- }
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
- //初始化混合器设备
- if (amdInitialize())
- {
- // get the Control ID and the names
- //获得音量控制控件的ID和名称
- amdGetMasterVolumeControl();
- //初始化滚动条
- m_ctrlSlider.SetRange(m_dwMinimum, m_dwMaximum);
- // 获得当前音量值,并设置滚动条的初始位置
- DWORD dwVal;
- if(amdGetMasterVolumeValue(dwVal))
- m_ctrlSlider.SetPos(dwVal);
- UpdateData(FALSE);
- }
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CVolumeDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
- void CVolumeDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CVolumeDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CVolumeDlg::OnDestroy()
- {
- amdUninitialize();
- CDialog::OnDestroy();
- }
- void CVolumeDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- //根据滚动条的位置设置音量
- if (pScrollBar != NULL)
- amdSetMasterVolumeValue(((CSliderCtrl *)pScrollBar)->GetPos());
- CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
- }
- LONG CVolumeDlg::OnMixerCtrlChange(UINT wParam, LONG lParam)
- {
- //响应控件音量改变的消息,然后获得当前音量并设置滚动条
- if ((HMIXER)wParam == m_hMixer && (DWORD)lParam == m_dwVolumeControlID)
- {
- DWORD dwVal;
- if (amdGetMasterVolumeValue(dwVal))
- m_ctrlSlider.SetPos(dwVal);
- }
- return 0L;
- }
- BOOL CVolumeDlg::amdInitialize()
- {
- // get the number of mixer devices present in the system
- //获取当前混合设备数量
- m_nNumMixers = ::mixerGetNumDevs();
- m_hMixer = NULL;
- ::ZeroMemory(&m_mxcaps, sizeof(MIXERCAPS));
- if (m_nNumMixers != 0)
- {
- //打开混合设备
- if (::mixerOpen(&m_hMixer,
- 0,
- (DWORD)this->GetSafeHwnd(),
- NULL,
- MIXER_OBJECTF_MIXER | CALLBACK_WINDOW)
- != MMSYSERR_NOERROR)
- return FALSE;
- // 获取混合器性能
- if (::mixerGetDevCaps((UINT)m_hMixer, &m_mxcaps, sizeof(MIXERCAPS))
- != MMSYSERR_NOERROR)
- return FALSE;
- }
- return TRUE;
- }
- BOOL CVolumeDlg::amdUninitialize()
- {
- BOOL bSucc = TRUE;
- if (m_hMixer != NULL)
- {
- //关闭混合器
- bSucc = ::mixerClose(m_hMixer) == MMSYSERR_NOERROR;
- m_hMixer = NULL;
- }
- return bSucc;
- }
- //获得控件
- BOOL CVolumeDlg::amdGetMasterVolumeControl()
- {
- m_strDstLineName.Empty();
- m_strVolumeControlName.Empty();
- if (m_hMixer == NULL)
- return FALSE;
- //获得混合器性能
- MIXERLINE mxl;
- mxl.cbStruct = sizeof(MIXERLINE);
- mxl.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
- if (::mixerGetLineInfo((HMIXEROBJ)m_hMixer,
- &mxl,
- MIXER_OBJECTF_HMIXER |
- MIXER_GETLINEINFOF_COMPONENTTYPE)
- != MMSYSERR_NOERROR)
- return FALSE;
- MIXERCONTROL mxc;
- MIXERLINECONTROLS mxlc;
- mxlc.cbStruct = sizeof(MIXERLINECONTROLS);
- mxlc.dwLineID = mxl.dwLineID;
- mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
- mxlc.cControls = 1;
- mxlc.cbmxctrl = sizeof(MIXERCONTROL);
- mxlc.pamxctrl = &mxc;
- //获得混合器线控件
- if (::mixerGetLineControls((HMIXEROBJ)m_hMixer,
- &mxlc,
- MIXER_OBJECTF_HMIXER |
- MIXER_GETLINECONTROLSF_ONEBYTYPE)
- != MMSYSERR_NOERROR)
- return FALSE;
- //记录控件的信息
- m_strDstLineName = mxl.szName;
- m_strVolumeControlName = mxc.szName;
- m_dwMinimum = mxc.Bounds.dwMinimum;
- m_dwMaximum = mxc.Bounds.dwMaximum;
- m_dwVolumeControlID = mxc.dwControlID;
- return TRUE;
- }
- //获得音量
- BOOL CVolumeDlg::amdGetMasterVolumeValue(DWORD &dwVal) const
- {
- if (m_hMixer == NULL ||
- m_strDstLineName.IsEmpty() || m_strVolumeControlName.IsEmpty())
- return FALSE;
- MIXERCONTROLDETAILS_UNSIGNED mxcdVolume;
- MIXERCONTROLDETAILS mxcd;
- mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
- mxcd.dwControlID = m_dwVolumeControlID;
- mxcd.cChannels = 1;
- mxcd.cMultipleItems = 0;
- mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
- mxcd.paDetails = &mxcdVolume;
- //获取指定混合器控件
- if (::mixerGetControlDetails((HMIXEROBJ)m_hMixer,
- &mxcd,
- MIXER_OBJECTF_HMIXER |
- MIXER_GETCONTROLDETAILSF_VALUE)
- != MMSYSERR_NOERROR)
- return FALSE;
-
- dwVal = mxcdVolume.dwValue;
- return TRUE;
- }
- //设置音量
- BOOL CVolumeDlg::amdSetMasterVolumeValue(DWORD dwVal) const
- {
- if (m_hMixer == NULL ||
- m_strDstLineName.IsEmpty() || m_strVolumeControlName.IsEmpty())
- return FALSE;
- MIXERCONTROLDETAILS_UNSIGNED mxcdVolume = { dwVal };
- MIXERCONTROLDETAILS mxcd;
- mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
- mxcd.dwControlID = m_dwVolumeControlID;
- mxcd.cChannels = 1;
- mxcd.cMultipleItems = 0;
- mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
- mxcd.paDetails = &mxcdVolume;
- //放置混合器控件
- if (::mixerSetControlDetails((HMIXEROBJ)m_hMixer,
- &mxcd,
- MIXER_OBJECTF_HMIXER |
- MIXER_SETCONTROLDETAILSF_VALUE)
- != MMSYSERR_NOERROR)
- return FALSE;
-
- return TRUE;
- }