📄 multim~2.cpp
字号:
// MultimediaDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Multimedia.h"
#include "MultimediaDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMultimediaDlg dialog
CMultimediaDlg::CMultimediaDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMultimediaDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMultimediaDlg)
// 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);
fileOpen = false;
count = 0;
}
void CMultimediaDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMultimediaDlg)
DDX_Control(pDX, IDC_SLIDER2, m_ctrlVolume);
DDX_Control(pDX, IDC_EXIT, m_exitButton);
DDX_Control(pDX, IDC_PAUSE, m_pauseButton);
DDX_Control(pDX, IDC_OPEN, m_openButton);
DDX_Control(pDX, IDC_STOP, m_stopButton);
DDX_Control(pDX, IDC_PLAY, m_playButton);
DDX_Control(pDX, IDC_ACTIVEMOVIECONTROL1, m_activeMovie);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMultimediaDlg, CDialog)
//{{AFX_MSG_MAP(CMultimediaDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_PLAY, OnPlay)
ON_BN_CLICKED(IDC_EXIT, OnExit)
ON_BN_CLICKED(IDC_OPEN, OnOpen)
ON_BN_CLICKED(IDC_PAUSE, OnPause)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_WM_TIMER()
ON_COMMAND(ID_MENUITEM_RECORD, OnMenuitemRecord)
ON_COMMAND(ID_MENUITEM_SAVE, OnMenuitemSave)
ON_WM_VSCROLL()
ON_COMMAND(ID_MENUITEM_REPLAY, OnMenuitemReplay)
ON_COMMAND(ID_MENUITEM_OPENCD, OnMenuitemOpencd)
ON_COMMAND(ID_MENUITEM_CLOSECD, OnMenuitemClosecd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMultimediaDlg message handlers
int static strPartDim[4]= {200, 350, 450, -1};//StatusBar
BOOL CMultimediaDlg::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
//为按钮加载位图
m_hBmp = ::LoadBitmap(HINSTANCE(GetModuleHandle(0)),MAKEINTRESOURCE(IDB_PLAY));
m_playButton.SetBitmap(m_hBmp);
m_hBmp = ::LoadBitmap(HINSTANCE(GetModuleHandle(0)),MAKEINTRESOURCE(IDB_STOP));
m_stopButton.SetBitmap(m_hBmp);
m_hBmp = ::LoadBitmap(HINSTANCE(GetModuleHandle(0)),MAKEINTRESOURCE(IDB_OPEN));
m_openButton.SetBitmap(m_hBmp);
m_hBmp = ::LoadBitmap(HINSTANCE(GetModuleHandle(0)),MAKEINTRESOURCE(IDB_PAUSE));
m_pauseButton.SetBitmap(m_hBmp);
m_hBmp = ::LoadBitmap(HINSTANCE(GetModuleHandle(0)),MAKEINTRESOURCE(IDB_EXIT));
m_exitButton.SetBitmap(m_hBmp);
//状态条控件初始化
m_wndStatusBar.Create(WS_CHILD|WS_VISIBLE|CCS_BOTTOM|CCS_NODIVIDER,CRect(0,0,0,0),this,102);
m_wndStatusBar.SetParts(4,strPartDim);
m_wndStatusBar.SetText("当前无文件播放",0,0);
m_wndStatusBar.SetText("时间",1,0);
m_wndStatusBar.SetText("Computer74",2,0);
//为菜单项加载位图
pMainMenu = GetMenu();
pSubMenu = pMainMenu->GetSubMenu(0);
m_bmp[0].LoadBitmap(IDB_MOPEN);
pSubMenu->SetMenuItemBitmaps(IDC_OPEN,MF_BYCOMMAND,&m_bmp[0],&m_bmp[0]);
m_bmp[1].LoadBitmap(IDB_MSAVE);
pSubMenu->SetMenuItemBitmaps(ID_MENUITEM_SAVE,MF_BYCOMMAND,&m_bmp[1],&m_bmp[1]);
m_bmp[2].LoadBitmap(IDB_MEXIT);
pSubMenu->SetMenuItemBitmaps(IDC_EXIT,MF_BYCOMMAND,&m_bmp[2],&m_bmp[2]);
//柱状音效显示控件初始化
CRect rect;
GetDlgItem(IDC_STATIC_HISTOGRAM)->GetWindowRect(rect);
ScreenToClient(rect);
m_HistogramCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this, 100);
m_HistogramCtrl.SetRange(0,100);
//滑块控件初始化
pMasterVolume = (IVolume*)(new CVolumeOutMaster());
m_ctrlVolume.SetRange(pMasterVolume->GetMinimalVolume(),30000);
m_ctrlVolume.SetLineSize(1000);
m_ctrlVolume.SetPageSize(5000);
m_ctrlVolume.SetTicFreq(1000);
m_ctrlVolume.SetPos(3000);
SetVolume(3000);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMultimediaDlg::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 CMultimediaDlg::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 CMultimediaDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMultimediaDlg::OnPlay()
{
//按钮事件,播放媒体文件
// TODO: Add your control notification handler code here
if(fileOpen)
{
m_media.Play(this,pFileName);
SetTimer(1,250,NULL);
}
}
BOOL CMultimediaDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
DeleteObject(m_hBmp);
return CDialog::DestroyWindow();
}
void CMultimediaDlg::OnExit()
{
//按钮事件,关闭设备并退出程序
// TODO: Add your control notification handler code here
m_media.CloseDevice();
KillTimer(1);
OnOK();
}
void CMultimediaDlg::OnOpen()
{
//按钮事件,打开设备
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE, "midi", "*.mid",OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT,"Midi Files(*.mid)|*.mid|Wave Files(*.wav)|*.wav|AVI Files(*.AVI)|*.AVI|All Files(*.*)|*.*||");
if(dlg.DoModal() == IDOK)
{
pFileName = dlg.GetPathName();
pFileExt = dlg.GetFileExt();
m_media.OpenDevice((LPCTSTR)pFileName,(LPCSTR)pFileExt);
fileOpen = true;
m_wndStatusBar.SetText(pFileName,0,0);
}
}
void CMultimediaDlg::OnPause()
{
//按钮事件,暂停文件的播放
// TODO: Add your control notification handler code here
if(fileOpen)
{
m_media.Pause();
KillTimer(1);
}
}
void CMultimediaDlg::OnStop()
{
//按钮事件,停止文件的播放
// TODO: Add your control notification handler code here
if(fileOpen)
{
m_media.Stop();
KillTimer(1);
m_HistogramCtrl.InvalidateCtrl();
count = 0;
}
}
void CMultimediaDlg::OnTimer(UINT nIDEvent)
{
//定时器响应,显示柱状音效和已播放过的时间
// TODO: Add your message handler code here and/or call default
CTime t = CTime::GetCurrentTime();
int nRandom;
srand(t.GetSecond());
do
{
nRandom = rand();
} while (nRandom < 0 || nRandom > 100);
m_HistogramCtrl.SetPos(nRandom);
nIDEvent=10;
CString TimeCon,Min,Sec;
char temp[10];
count ++;
_itoa(count/4/60,temp,10);
Min = (CString)temp;
TimeCon = "时间 " + Min + " 分";
_itoa(count/4%60,temp,10);
Sec = (CString)temp;
TimeCon = TimeCon + Sec + " 秒";
m_wndStatusBar.SetText(TimeCon,1,0);
CDialog::OnTimer(nIDEvent);
}
void CMultimediaDlg::OnMenuitemRecord()
{
//菜单事件,开始录音
// TODO: Add your command handler code here
if(m_media.type == WAVE)
{
m_media.BeginRecord();
}
}
void CMultimediaDlg::OnMenuitemSave()
{
//菜单事件,保存录音段
// TODO: Add your command handler code here
if(m_media.type == WAVE)
{
CFileDialog dlg(TRUE, "wave", "*.wav",OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT,"Wave Files(*.wav)|*.wav|");
if(dlg.DoModal() == IDOK)
{
m_media.SaveRecord(dlg.GetFileName());
OnStop();
}
}
}
void CMultimediaDlg::SetVolume(DWORD dwValue)
{
//设定音量
if ( !pMasterVolume || !pMasterVolume->IsAvailable() )
{
// handle error
}
pMasterVolume->Enable();
pMasterVolume->RegisterNotificationSink( MasterVolumeChanged, dwValue );
pMasterVolume->SetCurrentVolume( dwValue );
//DWORD dwCurrentVolume = pMasterVolume->SetCurrentVolume(dwValue);
}
void CALLBACK MasterVolumeChanged( DWORD dwCurrentVolume, DWORD dwUserValue )
{
//音量设定的回调函数
}
void CMultimediaDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
//滑块事件
// TODO: Add your message handler code here and/or call default
int nNewPos = 0;
switch(nSBCode)
{
case SB_TOP: //Scroll to far left.
nNewPos = m_ctrlVolume.GetRangeMin();
break;
case SB_ENDSCROLL: //end scroll.
return;
case SB_LINELEFT: //Scroll left.
nNewPos = m_ctrlVolume.GetPos() - m_ctrlVolume.GetLineSize();
break;
case SB_LINERIGHT: //Scroll right.
nNewPos = m_ctrlVolume.GetPos() + m_ctrlVolume.GetLineSize();
break;
case SB_PAGELEFT: //Scroll one page left.
nNewPos = m_ctrlVolume.GetPos() - m_ctrlVolume.GetPageSize();
break;
case SB_PAGERIGHT: //Scroll one page right.
nNewPos = m_ctrlVolume.GetPos() + m_ctrlVolume.GetPageSize();
break;
case SB_BOTTOM: //Scroll to far right.
nNewPos = m_ctrlVolume.GetRangeMax();
break;
case SB_THUMBPOSITION: //Scroll to absolute position. The current position is specified by the nPos parameter.
nNewPos = nPos;
break;
case SB_THUMBTRACK: //rag scroll box to specified position. The current position is specified by the
nNewPos = nPos;
break;
}
nNewPos = max(nNewPos, m_ctrlVolume.GetRangeMin());
nNewPos = min(nNewPos, m_ctrlVolume.GetRangeMax());
SetVolume(nNewPos);
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CMultimediaDlg::OnMenuitemReplay()
{
//菜单函数,重新播放文件
// TODO: Add your command handler code here
if(fileOpen)
{
m_media.paused = false;
m_media.Play(this,pFileName);
SetTimer(1,250,NULL);
}
}
void CMultimediaDlg::OnMenuitemOpencd()
{
//菜单函数,开光驱门
// TODO: Add your command handler code here
m_media.OpenCDRom();
}
void CMultimediaDlg::OnMenuitemClosecd()
{
//菜单函数,关光驱门
// TODO: Add your command handler code here
m_media.CloseCDRom();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -