www.pudn.com > MEDIAPLAYER.rar > MEDIAPLAYERDlg.cpp
// MEDIAPLAYERDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MEDIAPLAYER.h"
#include "MEDIAPLAYERDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CMEDIAPLAYERDlg dialog
CMEDIAPLAYERDlg::CMEDIAPLAYERDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMEDIAPLAYERDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMEDIAPLAYERDlg)
// 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);
}
void CMEDIAPLAYERDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMEDIAPLAYERDlg)
DDX_Control(pDX, IDC_AMOVIE, m_amovie);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMEDIAPLAYERDlg, CDialog)
//{{AFX_MSG_MAP(CMEDIAPLAYERDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_FILEOPEN, OnFileopen)
ON_COMMAND(ID_FILECLOSE, OnFileclose)
ON_COMMAND(ID_FILEEXIT, OnFileexit)
ON_COMMAND(ID_PLAYSTART, OnPlaystart)
ON_COMMAND(ID_PAAYSTOP, OnPaaystop)
ON_COMMAND(ID_PLAYPAUSE, OnPlaypause)
ON_COMMAND(ID_VIDEO1X, OnVideo1x)
ON_COMMAND(ID_VIDEO2X, OnVideo2x)
ON_COMMAND(ID_VIDEOFULL, OnVideofull)
ON_COMMAND(ID_VOLUME, OnVolume)
ON_COMMAND(ID_BALANCE, OnBalance)
ON_COMMAND(ID_HELPABOUT, OnHelpabout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMEDIAPLAYERDlg message handlers
BOOL CMEDIAPLAYERDlg::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 CMEDIAPLAYERDlg::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 CMEDIAPLAYERDlg::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 CMEDIAPLAYERDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMEDIAPLAYERDlg::OnFileopen()
{
// TODO: Add your command handler code here
//打开一个媒体文件
CFileDialog dlg(TRUE,NULL,NULL,0,"所有文件|*.*",NULL);
if(dlg.DoModal()==IDOK)
{
m_amovie.SetFileName(dlg.GetPathName());
}
}
void CMEDIAPLAYERDlg::OnFileclose()
{
// TODO: Add your command handler code here
//关闭文件,即简单将ActiveMovie控件的FileName属性设置为空
m_amovie.SetFileName("");
}
void CMEDIAPLAYERDlg::OnFileexit()
{
// TODO: Add your command handler code here
//关闭应用程序
OnCancel();
}
void CMEDIAPLAYERDlg::OnPlaystart()
{
// TODO: Add your command handler code here
m_amovie.Run();
}
void CMEDIAPLAYERDlg::OnPaaystop()
{
// TODO: Add your command handler code here
m_amovie.Stop();
}
void CMEDIAPLAYERDlg::OnPlaypause()
{
// TODO: Add your command handler code here
m_amovie.Pause();
}
void CMEDIAPLAYERDlg::OnVideo1x()
{
// TODO: Add your command handler code here
m_amovie.Pause();
m_amovie.SetMovieWindowSize(0);
m_amovie.Run();
}
void CMEDIAPLAYERDlg::OnVideo2x()
{
// TODO: Add your command handler code here
m_amovie.Pause();
m_amovie.SetMovieWindowSize(1);
m_amovie.Run();
}
void CMEDIAPLAYERDlg::OnVideofull()
{
// TODO: Add your command handler code here
m_amovie.Pause();
m_amovie.SetFullScreenMode(TRUE);
m_amovie.Run();
}
void CMEDIAPLAYERDlg::OnVolume()
{
// TODO: Add your command handler code here
CVolumeDlg dlgVolume;
dlgVolume.SetVolume(&m_amovie);
}
void CMEDIAPLAYERDlg::OnBalance()
{
// TODO: Add your command handler code here
CBalanceDlg dlgBalance;
dlgBalance.SetBlance(&m_amovie);
}
void CMEDIAPLAYERDlg::OnHelpabout()
{
// TODO: Add your command handler code here
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
void CMEDIAPLAYERDlg::MoveMovieWindow()
{
//改变窗口大小以适应控件大小
CRect rc1,rc2,rc3;
//得到ActiveMovie控件大小
m_amovie.GetWindowRect(rc1);
//保证对话框客户区的宽不小于300象素,高不小于225象素
if(rc1.Width()<300||rc1.Height()<255)
{
rc1.right=rc1.left+300;
rc1.bottom=rc1.top+225;
}
//获得对话框的大小
GetWindowRect(rc2);
//获得对话框客户区大小
GetClientRect(rc3);
//改变对话框大小以适应ActiveMovie控件大小
MoveWindow(rc2.left,rc2.top,rc2.Width()-rc3.Width()+rc1.Width(),
rc2.Height()-rc3.Height()+rc1.Height());
//获得控件ActiveMovie的大小
m_amovie.GetWindowRect(rc1);
GetClientRect(rc3);
//使ActiveMovie控件在对话框的客户区居中
m_amovie.MoveWindow((rc3.Width()-rc1.Width())/2,
(rc3.Height()-rc1.Height())/2,rc1.Width(),rc1.Height());
}
BEGIN_EVENTSINK_MAP(CMEDIAPLAYERDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CMEDIAPLAYERDlg)
ON_EVENT(CMEDIAPLAYERDlg, IDC_AMOVIE, 1 /* StateChange */, OnStateChangeAmovie, VTS_I4 VTS_I4)
ON_EVENT(CMEDIAPLAYERDlg, IDC_AMOVIE, -609 /* ReadyStateChange */, OnReadyStateChangeAmovie, VTS_I4)
ON_EVENT(CMEDIAPLAYERDlg, IDC_AMOVIE, 51 /* DisplayModeChange */, OnDisplayModeChangeAmovie, VTS_NONE)
ON_EVENT(CMEDIAPLAYERDlg, IDC_AMOVIE, 999 /* Error */, OnErrorAmovie, VTS_I2 VTS_BSTR VTS_BSTR VTS_PBOOL)
ON_EVENT(CMEDIAPLAYERDlg, IDC_AMOVIE, 50 /* OpenComplete */, OnOpenCompleteAmovie, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CMEDIAPLAYERDlg::OnStateChangeAmovie(long oldState, long newState)
{
// TODO: Add your control notification handler code here
//当ActiveMovie控件状态发生改变时,主对话框的大小也将发生改变
MoveMovieWindow();
}
void CMEDIAPLAYERDlg::OnReadyStateChangeAmovie(long ReadyState)
{
// TODO: Add your control notification handler code here
//当ActiveMovie控件准备状态发生改变时,主对话框的大小也将发生改变
MoveMovieWindow();
}
void CMEDIAPLAYERDlg::OnDisplayModeChangeAmovie()
{
// TODO: Add your control notification handler code here
//当ActiveMovie控件显示模式改变时,主对话框的大小也将发生改变
MoveMovieWindow();
}
void CMEDIAPLAYERDlg::OnErrorAmovie(short SCode, LPCTSTR Description, LPCTSTR Source, BOOL FAR* CancelDisplay)
{
// TODO: Add your control notification handler code here
//处理ActiveMovie的出错事件,弹出一个消息以告诉用户出错的代码和描述
CString str;
str.Format("出现错误[%d]:\n\n%s",SCode,Description);
MessageBox(str);
*CancelDisplay=TRUE;
}
void CMEDIAPLAYERDlg::OnOpenCompleteAmovie()
{
// TODO: Add your control notification handler code here
//当ActiveMovie控件完成媒体文件的加载时自动播放该文件
m_amovie.Run();
}