ex123Dlg.cpp
资源名称:VC6.0.rar [点击查看]
上传用户:qdhmjx
上传日期:2022-07-11
资源大小:2226k
文件大小:7k
源码类别:
书籍源码
开发平台:
Visual C++
- // ex123Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "ex123.h"
- #include "ex123Dlg.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()
- /////////////////////////////////////////////////////////////////////////////
- // CEx123Dlg dialog
- CEx123Dlg::CEx123Dlg(CWnd* pParent /*=NULL*/)
- : CDialog(CEx123Dlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CEx123Dlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- m_bIsPause=FALSE;
- m_bIsPlaying=FALSE;
- m_MCIDeviceID=0;
- }
- void CEx123Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CEx123Dlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CEx123Dlg, CDialog)
- //{{AFX_MSG_MAP(CEx123Dlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_DESTROY()
- ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
- ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
- ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
- //}}AFX_MSG_MAP
- ON_MESSAGE(MM_MCINOTIFY,OnMciNotify)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CEx123Dlg message handlers
- BOOL CEx123Dlg::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
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CEx123Dlg::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 CEx123Dlg::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 CEx123Dlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- void CEx123Dlg::OnDestroy()
- {
- CDialog::OnDestroy();
- //退出程序前先停止声音播放,再关闭设备
- //这里忽略了错误处理
- mciSendCommand(m_MCIDeviceID,MCI_STOP,MCI_WAIT,NULL);
- mciSendCommand(m_MCIDeviceID,MCI_CLOSE,MCI_WAIT,NULL);
- }
- void CEx123Dlg::ShowMCIError(DWORD dwError)
- {
- char szErrorBuf[MAXERRORLENGTH];
- MessageBeep(MB_ICONEXCLAMATION);
- if(mciGetErrorString(dwError,(LPSTR)szErrorBuf,MAXERRORLENGTH))
- MessageBox(szErrorBuf,"MCI错误",MB_ICONEXCLAMATION);
- else
- MessageBox("未知错误!","MCI错误",MB_ICONEXCLAMATION);
- }
- LRESULT CEx123Dlg::OnMciNotify(WPARAM wParam,LPARAM lParam)
- {
- //如果一个文件播放完毕,则重置两个状态为FALSE
- //使得用户可以再次播放该文件
- if(wParam==MCI_NOTIFY_SUCCESSFUL)
- {
- m_bIsPause=FALSE;
- m_bIsPlaying=FALSE;
- return 0;
- }
- return -1;
- }
- void CEx123Dlg::OnButtonOpen()
- {
- CString filename;
- MCI_OPEN_PARMS mciOpenParms;
- DWORD dwError;
- //利用通用对话框打开一个WAV波形文件
- static char szFilter[]="波形音频文件(*.wav)|*.wav||";
- CFileDialog dlg(TRUE,"wav",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);
- if(dlg.DoModal()==IDOK)
- {
- filename=dlg.GetPathName();//得到用户选择的文件名
- if(m_bIsPlaying) //如果正在播放,则先关掉
- {
- dwError=mciSendCommand(m_MCIDeviceID,MCI_CLOSE,0,NULL);
- if(dwError)
- {
- ShowMCIError(dwError);
- return;
- }
- }
- mciOpenParms.lpstrDeviceType="waveaudio";
- mciOpenParms.lpstrElementName=filename;
- dwError=mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,(DWORD)(LPVOID)&mciOpenParms);
- //如果打不开音频设备则报告错误并返回
- if(dwError)
- {
- ShowMCIError(dwError);
- return;
- }
- //如果打开成功,则取得设备ID值并设置两个状态指示
- m_MCIDeviceID=mciOpenParms.wDeviceID;
- m_bIsPause=FALSE;
- m_bIsPlaying=FALSE;
- }
- }
- void CEx123Dlg::OnButtonPlay()
- {
- MCI_PLAY_PARMS mciPlayParms;
- DWORD dwError;
- if(!m_bIsPlaying)
- {
- //处理MM_MCINOTIFY消息的窗口句柄
- mciPlayParms.dwCallback=(long)GetSafeHwnd();
- //从头开始播放文件
- mciPlayParms.dwFrom=0;
- dwError=mciSendCommand(m_MCIDeviceID,MCI_PLAY,MCI_FROM|MCI_NOTIFY,(DWORD)(LPVOID)&mciPlayParms);
- //如果错误,则显示错误并返回
- if(dwError)
- {
- ShowMCIError(dwError);
- return;
- }
- }
- if(m_bIsPlaying)
- {
- if(!m_bIsPause) //若正在播放,先暂停
- dwError=mciSendCommand(m_MCIDeviceID,MCI_PAUSE,0,NULL);
- else //若现在处于暂停,则恢复放音
- dwError=mciSendCommand(m_MCIDeviceID,MCI_RESUME,0,NULL);
- if(dwError) //遇到错误返回
- {
- ShowMCIError(dwError);
- return;
- }
- m_bIsPause=!m_bIsPause;//切换放音/暂停状态
- }
- m_bIsPlaying=TRUE;
- }
- void CEx123Dlg::OnButtonStop()
- {
- DWORD dwError;
- dwError=mciSendCommand(m_MCIDeviceID,MCI_STOP,MCI_WAIT,NULL);
- if(dwError)
- {
- ShowMCIError(dwError);
- return;
- }
- m_bIsPause=FALSE;
- m_bIsPlaying=FALSE;
- }