📄 musicplayerdlg.cpp
字号:
// MusicPlayerDlg.cpp : implementation file
//音乐播放器对话框程序
#include "stdafx.h"
#include "MusicPlayerDlg.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CPlaylist g_Playlist;
/////////////////////////////////////////////////////////////////////////////
// CMusicPlayerDlg dialog
CMusicPlayerDlg::CMusicPlayerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMusicPlayerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMusicPlayerDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_InitDC = false;
// 初始化背景画刷
CBitmap tmpBmp;
// 从资源中加载背景图片
tmpBmp.LoadBitmap(IDB_MUSIC_WND);
m_BgDC.CreateCompatibleDC(NULL);
m_BgDC.SelectObject(&tmpBmp);
tmpBmp.DeleteObject();
// m_MemDC.SelectObject(m_titleFont);
WCHAR strRes[MAX_PATH];
CString(LoadString(GetResourceLib(),IDS_MUSICPLAYER_TITLE,strRes,MAX_PATH));
m_TitleText = strRes;
CString(LoadString(GetResourceLib(),IDS_MUSICPLAYERDIR_TITLE,strRes,MAX_PATH));
m_TitleDirText = strRes;
// m_MemDC.DrawText(m_TitleText,CRect(43,4,320,32),DT_LEFT|DT_VCENTER);
m_BlockD = FALSE;
m_ReplayAll = TRUE;
m_RandomPlay = FALSE;
m_Timer = NULL;
m_txtFontNumber = 0;
}
CMusicPlayerDlg::~CMusicPlayerDlg()
{
if (!m_SourceFile.IsEmpty()) //防止无效路径覆盖以前
{
m_InitInf.FileListSize=g_Playlist.m_FileList.GetSize();
m_InitInf.CurrentFileNO=g_Playlist.m_CurrentFile;
m_InitInf.SourceFile=m_SourceFile;
CFile myFile(L"\\residentflash\\MusicPlayer.ini", CFile::modeCreate | CFile::modeReadWrite);
CArchive arStore(&myFile, CArchive::store);
arStore.Write(&m_InitInf,sizeof(int)*3);
arStore.WriteString( m_InitInf.SourceFile );
arStore.WriteString( L"\n" );
for (int i=0;i<m_InitInf.FileListSize;i++)
{
arStore.WriteString( g_Playlist.m_FileList.GetAt(i) );
arStore.WriteString( L"\n" );
}
arStore.Close();
myFile.Close();
}
m_BgDC.DeleteDC();
m_MemDC.DeleteDC();
m_BlockDC.DeleteDC();
m_BlockDCD.DeleteDC();
if (m_Timer)
{
timeKillEvent(m_Timer);
}
if (m_pGraph)
{
m_pGraph->Release(); // Graph
m_pGraph = NULL;
}
if (m_pEvent)
{
m_pEvent->Release(); // 事件接口
m_pEvent = NULL;
}
if (m_pSeek)
{
m_pSeek->Release(); // 数据流定位接口
m_pSeek = NULL;
}
if (m_pMediaControl)
{
m_pMediaControl->Release(); // 播放器接口
m_pMediaControl = NULL;
}
}
void CMusicPlayerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMusicPlayerDlg)
DDX_Control(pDX, IDC_STATIC_TIME, m_labTime);
DDX_Control(pDX, IDC_STATIC_FILENAME, m_labFile);
DDX_Control(pDX, IDC_STATIC_ALL, m_labAllTime);
DDX_Control(pDX, IDC_BUTTON_STOP, m_butStop);
DDX_Control(pDX, IDC_BUTTON_REDOM, m_butRandom);
DDX_Control(pDX, IDC_BUTTON_PRE, m_butPre);
DDX_Control(pDX, IDC_BUTTON_OPEN, m_butOpen);
DDX_Control(pDX, IDC_BUTTON_PLAY, m_butPlay);
DDX_Control(pDX, IDC_BUTTON_NEXT, m_butNext);
DDX_Control(pDX, IDC_BUTTON_EXIT, m_butExit);
DDX_Control(pDX, IDC_BUTTON_CIRCLE, m_butAll);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMusicPlayerDlg, CDialog)
//{{AFX_MSG_MAP(CMusicPlayerDlg)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
ON_BN_CLICKED(IDC_BUTTON_CIRCLE, OnButtonCircle)
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
ON_BN_CLICKED(IDC_BUTTON_NEXT, OnButtonNext)
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
ON_BN_CLICKED(IDC_BUTTON_PRE, OnButtonPre)
ON_BN_CLICKED(IDC_BUTTON_REDOM, OnButtonRedom)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
ON_MESSAGE(WM_DSPLAYER,OnDSPlayer)
ON_WM_TIMER()
ON_MESSAGE(WM_DRAWPLAYTIME, OnPlayerTimer)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMusicPlayerDlg message handlers
BOOL CMusicPlayerDlg::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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
MoveWindow(0,0,320,240);
SetWindowText(L"MusicPlayer");
m_butExit.MoveWindow(282,6,25,25);
m_butExit.LoadBitmaps(IDB_MUSIC_CLOSE,IDB_MUSIC_CLOSED);
if (m_ReplayAll)
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLE,IDB_MUSIC_CIRCLE);
}
else
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLED,IDB_MUSIC_CIRCLED);
}
m_butAll.MoveWindow(2,56,46,46);
if (m_RandomPlay)
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOMD,IDB_MUSIC_RANDOMD);
}
else
{
m_butRandom.LoadBitmaps(IDB_MUSIC_RANDOM,IDB_MUSIC_RANDOM);
}
m_butRandom.MoveWindow(2,113,46,46);
m_butOpen.MoveWindow(2,170,46,46);
m_butOpen.LoadBitmaps(IDB_MUSIC_OPEN,IDB_MUSIC_OPEND);
m_butPlay.MoveWindow(272,37,46,46);
m_butPlay.LoadBitmaps(IDB_MUSIC_PLAY,IDB_MUSIC_PLAYD,NULL,IDB_MUSIC_PLAYDIS);
m_butStop.MoveWindow(272,87,46,46);
m_butStop.LoadBitmaps(IDB_MUSIC_STOP,IDB_MUSIC_STOPD,NULL,IDB_MUSIC_STOPDIS);
m_butPre.MoveWindow(272,137,46,46);
m_butPre.LoadBitmaps(IDB_MUSIC_BACK,IDB_MUSIC_BACKD,NULL,IDB_MUSIC_BACKDIS);
m_butNext.MoveWindow(272,187,46,46);
m_butNext.LoadBitmaps(IDB_MUSIC_NEXT,IDB_MUSIC_NEXTD,NULL,IDB_MUSIC_NEXTDIS);
m_labTime.MoveWindow(76,87,81,22);
m_labTime.SetTxtFont(1);
m_labTime.SetWindowText(L"00:00/");
m_labAllTime.MoveWindow(166,87,81,22);
m_labAllTime.SetTxtFont(2);
m_labAllTime.SetWindowText(L"00:00");
//文件名
m_labFile.MoveWindow(60,60,198,26);
m_labFile.SetTxtFont(0);
m_labFile.SetWindowText(L"");
SetTimer(1,750,NULL);
m_Timer = timeSetEvent(1000,100,(LPTIMECALLBACK)PlayerTimeProc,(DWORD)this,TIME_PERIODIC);
//读初始化文件
CFile myFile;
if ( myFile.Open(L"\\residentflash\\MusicPlayer.ini",CFile::modeRead,NULL) ) //打开成功;
{
myFile.SeekToBegin();
CArchive arStore(&myFile, CArchive::load);
arStore.Read(&m_InitInf.FileviewPosition,sizeof(int));
arStore.Read(&m_InitInf.FileListSize,sizeof(int));
arStore.Read(&m_InitInf.CurrentFileNO,sizeof(int));
CString str;
arStore.ReadString( str);
m_InitInf.SourceFile=str;
g_Playlist.m_FileList.RemoveAll();
for (int i=0;i<m_InitInf.FileListSize;i++)
{
arStore.ReadString( str);
g_Playlist.m_FileList.Add(str) ;
}
arStore.Close();
myFile.Close();
m_SourceFile=m_InitInf.SourceFile;
if ((!m_SourceFile.IsEmpty()) && (GetFileAttributes(m_SourceFile) != 0xFFFFFFFF) )
{
m_SourceFileName=m_SourceFile.Right(m_SourceFile.GetLength() -m_SourceFile.ReverseFind(L'\\')-1);
g_Playlist.m_TotalFiles = m_InitInf.FileListSize;
g_Playlist.m_CurrentFile = m_InitInf.CurrentFileNO;
g_Playlist.m_FilePath = m_SourceFile.Left(m_SourceFile.ReverseFind(L'\\')+1);
g_Playlist.InitSeed();
if (g_Playlist.m_TotalFiles > 1)
{ m_butNext.EnableWindow(TRUE);
m_butPre.EnableWindow(TRUE);
}
else
{
m_butNext.EnableWindow(FALSE);
m_butPre.EnableWindow(FALSE);
}
CreateGraph();
OnButtonPlay();
return TRUE;
}
}
m_SourceFile = L"";
m_SourceFileName=L"";
m_butNext.EnableWindow(FALSE);
m_butPre.EnableWindow(FALSE);
m_pGraph = NULL;
m_pSeek = NULL;
m_pEvent = NULL;
m_pMediaControl = NULL;
m_InitGraph = FALSE;
//播放列表初始化
g_Playlist.m_FileList.RemoveAll();
g_Playlist.m_TotalFiles = 0;
g_Playlist.m_CurrentFile = 0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CMusicPlayerDlg::OnPaint()
{
// TODO: Add your message handler code here
CPaintDC dc(this); // device context for painting
CString m_PlayDir = m_SourceFile;
int DirLength = wcslen(m_PlayDir);
m_MemDC.SetTextColor(RGB(255,255,255));
m_MemDC.SelectObject(&m_titleFont);
m_MemDC.BitBlt(0,0,320,240,&m_BgDC,0,0,SRCCOPY);
m_MemDC.DrawText(m_TitleText,CRect(40,4,280,32),DT_CENTER|DT_VCENTER);
m_MemDC.SelectObject(&m_txtFont);
m_MemDC.SetTextColor(RGB(255,100,255));
if ((m_SourceFile.GetLength()>8) && (GetFileAttributes(m_SourceFile) != 0xFFFFFFFF) )
{
if (g_Playlist.m_TotalFiles)
{
int k;
if (m_RandomPlay)
k=g_Playlist.m_Seed[g_Playlist.m_CurrentFile]+1;
else
k=g_Playlist.m_CurrentFile+1;
CString str;
str.Format(L"%d / %d",k,g_Playlist.m_TotalFiles);
m_MemDC.DrawText(str,CRect(40,115,280,145),DT_CENTER|DT_VCENTER);
}
//文件路径
if(DirLength <= 7)
{
// m_CurrentPath.MakeUpper();
// dc.DrawText(L":"+m_CurrentPath,CRect(83,6,320,28),DT_LEFT|DT_BOTTOM);
}
else if(DirLength <= 25)
{
m_MemDC.DrawText(L":\\SDMEM\\"+(m_PlayDir.Right(DirLength-7)),CRect(60,155,258,170),DT_LEFT|DT_BOTTOM);
}
else if(DirLength > 25)
{
m_MemDC.DrawText(L":\\SDMEM\\..."+m_PlayDir.Right(19),CRect(60,155,258,170),DT_LEFT|DT_BOTTOM);
}
dc.BitBlt(0,0,320,240,&m_MemDC,0,0,SRCCOPY);
}
else
{
m_labTime.SetWindowText(L"");
m_labAllTime.SetWindowText(L"");
m_labFile.SetWindowText(L"");
}
ShowProgress();
// Do not call CDialog::OnPaint() for painting messages
}
BOOL CMusicPlayerDlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
if (!m_InitDC)
{
CBitmap tmpBmp;
tmpBmp.CreateCompatibleBitmap(pDC,320,240);
m_MemDC.CreateCompatibleDC(pDC);
m_MemDC.SelectObject(&tmpBmp);
m_MemDC.SelectObject(&m_txtFont);
m_MemDC.SetBkMode(TRANSPARENT);
m_MemDC.SetTextColor(RGB(255,255,255));
tmpBmp.DeleteObject();
tmpBmp.LoadBitmap(IDB_MUSIC_BAR);
m_BlockDC.CreateCompatibleDC(pDC);
m_BlockDC.SelectObject(&tmpBmp);
tmpBmp.DeleteObject();
tmpBmp.LoadBitmap(IDB_MUSIC_BAR);
m_BlockDCD.CreateCompatibleDC(pDC);
m_BlockDCD.SelectObject(&tmpBmp);
tmpBmp.DeleteObject();
m_InitDC = true;
}
return FALSE;
//return CDialog::OnEraseBkgnd(pDC);
}
void CMusicPlayerDlg::OnButtonCircle()
{
// TODO: Add your control notification handler code here
if (m_ReplayAll)
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLED,IDB_MUSIC_CIRCLED);
}
else
{
m_butAll.LoadBitmaps(IDB_MUSIC_CIRCLE,IDB_MUSIC_CIRCLE);
}
m_butAll.Invalidate();
m_ReplayAll = !m_ReplayAll;
}
void CMusicPlayerDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
//如歌音乐正在播放,这在后台播放
if (IsPlaying())
{
ShowWindow(SW_HIDE);
}
else
{ EndDialog(IDOK);
}
}
void CMusicPlayerDlg::OnButtonNext()
{
// TODO: Add your control notification handler code here
//跳过无效文件
int FauseTimes=0;
m_SourceFile=L" ";
while (GetFileAttributes(m_SourceFile) == 0xFFFFFFFF)
{
if (FauseTimes++ == g_Playlist.m_TotalFiles)
{
m_SourceFile=L" ";
m_SourceFileName=L"";
m_labFile.SetWindowText(m_SourceFileName);
g_Playlist.m_TotalFiles=0;
g_Playlist.m_CurrentFile=0;
return;
}
m_SourceFile = g_Playlist.GetNextWorkFileName(false,m_RandomPlay);
}
bool bPlay = IsPlaying();
if (m_pMediaControl)
{
m_pMediaControl->Stop();
}
CreateGraph();
if (bPlay)
{
m_pMediaControl->Run();
m_butPlay.LoadBitmaps(IDB_MUSIC_PAUSE,IDB_MUSIC_PAUSED,0,0);
}
m_SourceFileName=m_SourceFile.Right(m_SourceFile.GetLength() -m_SourceFile.ReverseFind(L'\\')-1);
m_labFile.SetWindowText(m_SourceFileName);
InvalidateRect(CRect(60,163,258,177));
}
void CMusicPlayerDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
COpenFileDlg fileDlg;
fileDlg.LoadBitmap(IDB_FILEVIEW_WND,IDB_ICON_MUSICDIR,IDB_ICON_MUSICDIRD,
IDB_ICON_MUSIC,IDB_ICON_MUSICD);
fileDlg.SetFileType( L".mp3.wma.pcm.ima.mpa.mp2.wav.snd.aif.aifc.au.");
fileDlg.SetPosition(m_InitInf.FileviewPosition);
fileDlg.SetSourceFile(m_InitInf.SourceFile);
if (fileDlg.DoModal() == IDOK)
{
m_InitInf.FileviewPosition=fileDlg.GetPosition();
m_InitInf.SourceFile=fileDlg.GetSourceFile();
m_SourceFile = fileDlg.GetSourceFile();
m_SourceFileName=m_SourceFile.Right(m_SourceFile.GetLength() -m_SourceFile.ReverseFind(L'\\')-1);
g_Playlist.m_TotalFiles = fileDlg.m_FileList.GetSize() ;
g_Playlist.m_CurrentFile = fileDlg.m_FileNO;
g_Playlist.m_FileList.RemoveAll();
g_Playlist.m_FileList.Append(fileDlg.m_FileList);
g_Playlist.m_FilePath = m_SourceFile.Left(m_SourceFile.ReverseFind(L'\\')+1);
g_Playlist.InitSeed();
if (g_Playlist.m_TotalFiles > 1)
{
m_butNext.EnableWindow(TRUE);
m_butPre.EnableWindow(TRUE);
}
else
{
m_butNext.EnableWindow(FALSE);
m_butPre.EnableWindow(FALSE);
}
if (m_pMediaControl)
{
m_pMediaControl->Stop();
}
CreateGraph();
//OnButtonPlay();
m_butPlay.LoadBitmaps(IDB_MUSIC_PAUSE,IDB_MUSIC_PAUSED);
m_pMediaControl->Run();
m_labFile.SetWindowText(m_SourceFileName);
m_butStop.EnableWindow(TRUE);
m_butPlay.Invalidate();
}
}
void CMusicPlayerDlg::OnButtonPlay()
{
// TODO: Add your control notification handler code here
if (m_SourceFile.IsEmpty())
{
if (m_SourceFile.IsEmpty())
{
OnButtonOpen();
}
}
else
{
if (IsPlaying())
{
if (m_pMediaControl)
{
m_butPlay.LoadBitmaps(IDB_MUSIC_PLAY,IDB_MUSIC_PLAYD);
m_pMediaControl->Pause();
}
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -