?? wavmcidemodlg.cpp
字號:
// WAVMCIDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "WAVMCIDemo.h"
#include "WAVMCIDemoDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CWAVMCIDemoDlg dialog
CWAVMCIDemoDlg::CWAVMCIDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CWAVMCIDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CWAVMCIDemoDlg)
m_strbits = _T("");
m_strcha = _T("");
m_strname = _T("");
m_strtime = _T("");
m_strsam = _T("");
m_strstatus = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CWAVMCIDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CWAVMCIDemoDlg)
DDX_Control(pDX, IDC_LIST1, m_List);
DDX_Text(pDX, IDC_BITS, m_strbits);
DDX_Text(pDX, IDC_CHANNEL, m_strcha);
DDX_Text(pDX, IDC_FILENAME, m_strname);
DDX_Text(pDX, IDC_FILETIME, m_strtime);
DDX_Text(pDX, IDC_SAMPLERATE, m_strsam);
DDX_Text(pDX, IDC_STATUS, m_strstatus);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CWAVMCIDemoDlg, CDialog)
//{{AFX_MSG_MAP(CWAVMCIDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_OPENWAV, OnOpenwav)
ON_BN_CLICKED(IDC_PLAYWAV, OnPlaywav)
ON_BN_CLICKED(IDC_RECORDWAV, OnRecordwav)
ON_BN_CLICKED(IDC_SAVEWAV, OnSavewav)
ON_BN_CLICKED(IDC_STOPWAV, OnStopwav)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWAVMCIDemoDlg message handlers
BOOL CWAVMCIDemoDlg::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
GetDlgItem(IDC_PLAYWAV)->EnableWindow(FALSE); // 使"播放"按鈕操作無效
GetDlgItem(IDC_SAVEWAV)->EnableWindow(FALSE); // 使"保存"按鈕操作無效
GetDlgItem(IDC_STOPWAV)->EnableWindow(FALSE); // 使"停止"按鈕操作無效
return TRUE; // return TRUE unless you set the focus to a control
}
void CWAVMCIDemoDlg::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 CWAVMCIDemoDlg::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 CWAVMCIDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CWAVMCIDemoDlg::OnOpenwav()
{
// TODO: Add your control notification handler code here
CString Filter;
char buf[128];
DWORD dwFlag;
Filter = "波形音頻文件(*.WAV)|*.WAV||";
CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, Filter);
if (fileDlg.DoModal() ==IDOK) // 顯示打開文件的對話框
{
m_strname = fileDlg.GetPathName(); // 獲得打開的文件名
dwFlag = m_Mciwave.Open(m_strname); // 打開波形音頻文件
if (dwFlag)
{
mciGetErrorString(dwFlag, buf, strlen(buf)); // 獲取錯誤信息
AfxMessageBox(buf); // 顯示錯誤信息
}
GetDlgItem(IDC_PLAYWAV)->EnableWindow(TRUE); // 使"播放"按鈕有效
m_strstatus = "打開文件:"+m_strname; // 構造信息字符串并顯示
CTime m_time;
m_time=CTime::GetCurrentTime(); //獲取當前時間日期
CString str=m_time.Format("%H:%M:%S:"); //格式化
str=str+m_strstatus;
m_List.AddString(str);//添加到列表框
m_strsam.Format("%5.3f kHZ", m_Mciwave.GetSampleRate()/1000.);//獲取采樣頻率
switch (m_Mciwave.GetChannel())//獲取聲道
{
case 1:
m_strcha = "單聲道";
break;
case 2:
m_strcha = "立體聲";
break;
}
m_strbits.Format("%d位", m_Mciwave.GetBitLength());//數據位
m_strtime.Format("%02.2fs",m_Mciwave.GetWaveLength()/1000.);//時長
UpdateData(FALSE);//顯示信息
}
}
void CWAVMCIDemoDlg::OnPlaywav()
{
// TODO: Add your control notification handler code here
if (m_Mciwave.Play())
{
AfxMessageBox("播放出錯");
return;
}
GetDlgItem(IDC_RECORDWAV)->EnableWindow(FALSE); // 使"錄音"按鈕操作無效
GetDlgItem(IDC_OPENWAV)->EnableWindow(FALSE); // 使"打開"按鈕操作無效
GetDlgItem(IDC_PLAYWAV)->EnableWindow(FALSE); // 使"播放"按鈕操作無效
GetDlgItem(IDC_STOPWAV)->EnableWindow(true); // 使"停止"按鈕操作生效
m_Mciwave.m_bPlaying = TRUE;//設置播放標志
m_strstatus = "正在播放……";//播放狀態
CTime m_time;
m_time=CTime::GetCurrentTime();
CString str=m_time.Format("%H:%M:%S:"); //格式化
str=str+"開始播放";
m_List.AddString(str);//添加到列表框
UpdateData(FALSE);
}
void CWAVMCIDemoDlg::OnRecordwav()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_PLAYWAV)->EnableWindow(FALSE); // 使"播放"按鈕操作無效
GetDlgItem(IDC_OPENWAV)->EnableWindow(FALSE); // 使"打開"按鈕操作無效
GetDlgItem(IDC_RECORDWAV)->EnableWindow(FALSE); // 使"錄音"按鈕操作無效
GetDlgItem(IDC_STOPWAV)->EnableWindow(true); // 使"停止"按鈕操作生效
m_strstatus = "正在錄音……"; // 構造信息字符串并顯示
CTime m_time;
m_time=CTime::GetCurrentTime();
CString str=m_time.Format("%H:%M:%S:"); //格式化
str=str+"開始錄音";
m_List.AddString(str);//添加到列表框
UpdateData(FALSE);
m_Mciwave.m_bRecord=true;
m_Mciwave.Record(); // 開始錄音
}
void CWAVMCIDemoDlg::OnSavewav()
{
// TODO: Add your control notification handler code here
CString Filter;
char buf[128];
DWORD dwFlag;
Filter = "Wave Files(*.WAV)|*.WAV||";//過濾器
CFileDialog fileDlg(FALSE, NULL, NULL, OFN_OVERWRITEPROMPT, Filter);//保存文件對話框
fileDlg.m_ofn.lpstrDefExt = "wav";
if (fileDlg.DoModal() ==IDOK)
{
dwFlag =m_Mciwave.Save(fileDlg.GetPathName());
if (dwFlag)
{
mciGetErrorString(dwFlag, buf, strlen(buf));//獲取錯誤信息
AfxMessageBox(buf);
return;
}
}
m_strstatus = "保存結束……";
CTime m_time;
m_time=CTime::GetCurrentTime();
CString str=m_time.Format("%H:%M:%S:"); //格式化
str=str+"保存錄音文件";
m_List.AddString(str);//添加到列表框
UpdateData(FALSE);
}
void CWAVMCIDemoDlg::OnStopwav()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_STOPWAV)->EnableWindow(FALSE); // 使"停止"按鈕操作無效
GetDlgItem(IDC_PLAYWAV)->EnableWindow(true); // 使"播放"按鈕操作生效
GetDlgItem(IDC_OPENWAV)->EnableWindow(true); // 使"打開"按鈕操作生效
GetDlgItem(IDC_RECORDWAV)->EnableWindow(true); // 使"錄音"按鈕操作生效
CTime m_time;
m_time=CTime::GetCurrentTime();
CString str=m_time.Format("%H:%M:%S:"); //格式化
if (m_Mciwave.m_bRecord)//正在錄音
{
GetDlgItem(IDC_SAVEWAV)->EnableWindow(true); // 使"保存"按鈕操作生效
m_strstatus = "錄音停止……";
str=str+"錄音停止";
m_List.AddString(str);//添加到列表框
}
if (m_Mciwave.m_bPlaying)
{
m_strstatus = "播放停止……";
str=str+"播放停止";
m_List.AddString(str);//添加到列表框
}
UpdateData(FALSE);
m_Mciwave.Stop();//停止
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -