📄 rxmediaplayerdlg.cpp
字号:
// RxMediaPlayerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "RxMediaPlayer.h"
#include "RxMediaPlayerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRxMediaPlayerDlg dialog
CRxMediaPlayerDlg::CRxMediaPlayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRxMediaPlayerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRxMediaPlayerDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRxMediaPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRxMediaPlayerDlg)
DDX_Control(pDX, IDC_STABMP, m_StaBmp);
DDX_Control(pDX, IDC_STATITLE, m_StaTitle);
DDX_Control(pDX, IDC_BUTSTOP, m_ButStop);
DDX_Control(pDX, IDC_BUTPLAY, m_ButPlay);
DDX_Control(pDX, IDC_BUTPAUSE, m_ButPause);
DDX_Control(pDX, IDC_BUTEXIT, m_ButExit);
DDX_Control(pDX, IDC_TREE, m_Tree);
DDX_Control(pDX, IDC_BUTADD, m_ButAdd);
DDX_Control(pDX, IDC_MEDIAPLAYER, m_Media);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRxMediaPlayerDlg, CDialog)
//{{AFX_MSG_MAP(CRxMediaPlayerDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTADD, OnButadd)
ON_BN_CLICKED(IDC_BUTPLAY, OnButplay)
ON_BN_CLICKED(IDC_BUTPAUSE, OnButpause)
ON_BN_CLICKED(IDC_BUTSTOP, OnButstop)
ON_BN_CLICKED(IDC_BUTEXIT, OnButexit)
ON_NOTIFY(NM_RDBLCLK, IDC_TREE, OnRdblclkTree)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE, OnSelchangedTree)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRxMediaPlayerDlg message handlers
BOOL CRxMediaPlayerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
m_Media.ShowWindow(SW_HIDE);
m_bPause=false;
//取得当前程序目录
char cPath[_MAX_DIR];
::GetCurrentDirectory(sizeof(cPath),cPath);
m_CurPath.Format("%s",cPath);
// TODO: Add extra initialization here
m_ImageList.Create(16,16,ILC_COLOR32|ILC_MASK,0,0);
m_ImageList.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_SOUND)));
m_ImageList.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_VIDEO)));
m_ImageList.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON1)));
m_ImageList.Add(::LoadIcon(::AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON2)));
this->AddFile();
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CRxMediaPlayerDlg::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 CRxMediaPlayerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CRxMediaPlayerDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
void CRxMediaPlayerDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
void CRxMediaPlayerDlg::OnButadd()
{
CFileDialog dlg(true,"","",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"媒体文件|*.wav;*.mp3;*.mov;*.rm;*.mpg;*.mid;*.avi|所有文件|*.*|");
dlg.DoModal();
CString sOldPath,sFile;
sOldPath=dlg.GetPathName();
sFile=dlg.GetFileName();
if(sOldPath.IsEmpty()==false)
{ //判断程序目录下是否存在指定文件夹
CString sPath;
sPath.Format("%s\\媒体文件",m_CurPath);
CFileFind FileFind;
bool bIsFind=FileFind.FindFile(sPath);
if(bIsFind==false) //没有找到文件夹
::CreateDirectory(sPath,NULL);
CString sFileName;
sFileName.Format("%s\\%s",sPath,dlg.GetFileName());
SHFILEOPSTRUCT FileStruct;
int nFromLength=sOldPath.GetLength()+1;
int nToLength=sFileName.GetLength()+1;
//非常重要,为字符串指针增加2个结束符
char* pFrom=sOldPath.GetBufferSetLength(nFromLength);
char* pTo=sFileName.GetBufferSetLength(nToLength);
pFrom[nFromLength]=0;
pFrom[nFromLength-1]=0;
pTo[nToLength]=0;
pTo[nToLength-1]=0;
FileStruct.hwnd=this->GetSafeHwnd();
FileStruct.wFunc=FO_COPY ;
FileStruct.pFrom=pFrom;
FileStruct.pTo=pTo;
FileStruct.fFlags=FOF_SIMPLEPROGRESS;
FileStruct.fAnyOperationsAborted=true;
FileStruct.hNameMappings=NULL;
CString sText;
sText.Format("正在复制文件 %s 请稍后...",sFile);
FileStruct.lpszProgressTitle=sText.GetBuffer(sText.GetLength());
SHFileOperation(&FileStruct);
}
AddFile();
}
void CRxMediaPlayerDlg::OnButplay()
{
CString sPath,sName;
sName=this->m_Tree.GetItemText(m_Tree.GetSelectedItem());
if(sName.Find(".")==-1)
return;
this->Enabled(true);
if(this->m_bPause==true)
{
this->m_Media.Play();
m_StaTitle.SetWindowText("正在播放--"+sName);
this->m_bPause=false;
return;
}
m_StaTitle.SetWindowText("正在准备播放--"+sName);
sName=this->m_Tree.GetItemText(m_Tree.GetSelectedItem());
sPath.Format("%s\\媒体文件\\%s",this->m_CurPath,sName);
this->m_Media.SetFileName(sPath);
try{
this->m_Media.Play();
m_StaTitle.SetWindowText("正在播放--"+sName);
}
catch(...)
{
this->Enabled(false);
m_StaTitle.SetWindowText("播放文件--"+sName+"--失败,文件已损坏或没有硬件支持!");
}
}
void CRxMediaPlayerDlg::OnButpause()
{
m_bPause=true;
this->m_Media.Pause();
this->m_ButPlay.EnableWindow(true);
this->m_ButPause.EnableWindow(false);
this->m_ButStop.EnableWindow(false);
CString sText;
this->m_StaTitle.GetWindowText(sText);
sText=sText+"(暂停状态)";
this->m_StaTitle.SetWindowText(sText);
}
void CRxMediaPlayerDlg::OnButstop()
{
m_bPause=false;
this->m_Media.Stop();
this->Enabled(false);
this->m_StaTitle.SetWindowText("");
}
void CRxMediaPlayerDlg::OnButexit()
{
this->OnCancel();
}
void CRxMediaPlayerDlg::AddFile()
{
CFileFind ff;
CString sPath,sFileName,sExternName,sTitleName;
sPath.Format("%s\\媒体文件\\*.*",m_CurPath);
bool res=ff.FindFile(sPath.GetBuffer(sPath.GetLength()));
m_Tree.EnableAutomation();
m_Tree.SetImageList(&m_ImageList,TVSIL_NORMAL);
m_Tree.DeleteAllItems();
HTREEITEM itemSound,itemViedo;
itemSound=m_Tree.InsertItem("音频文件",0,0,TVI_ROOT,TVI_LAST);
itemViedo=m_Tree.InsertItem("视频文件",1,1,TVI_ROOT,TVI_LAST);
while(res)
{
res=ff.FindNextFile();
if(!ff.IsDirectory())
{
sTitleName=ff.GetFileTitle();
sFileName=ff.GetFileName();
sTitleName=sFileName.Mid(sTitleName.GetLength()+1);
sTitleName.MakeUpper();
sTitleName.MakeUpper();
if(sTitleName=="MP3" || sTitleName=="WAV")
this->m_Tree.InsertItem(sFileName,3,3,itemSound,TVI_LAST);
if(sTitleName=="MID" || sTitleName=="AVI"|| sTitleName=="MPG"|| sTitleName=="MOV"|| sTitleName=="RM")
this->m_Tree.InsertItem(sFileName,2,2,itemViedo,TVI_LAST);
}
}
}
void CRxMediaPlayerDlg::OnRdblclkTree(NMHDR* pNMHDR, LRESULT* pResult)
{
*pResult = 0;
}
void CRxMediaPlayerDlg::Enabled(bool bEnabled)
{
this->m_ButPlay.EnableWindow(!bEnabled);
this->m_ButPause.EnableWindow(bEnabled);
this->m_ButStop.EnableWindow(bEnabled);
if(bEnabled==true)
{
this->m_Media.ShowWindow(SW_SHOW);
this->m_StaBmp.ShowWindow(SW_HIDE);
}
else
{
this->m_Media.ShowWindow(SW_HIDE);
this->m_StaBmp.ShowWindow(SW_SHOW);
}
this->m_ButAdd.EnableWindow(!bEnabled);
}
void CRxMediaPlayerDlg::OnSelchangedTree(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
this->OnButplay();
*pResult = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -