www.pudn.com > MCIWnd_MediaPlayer.rar > MyPlayer.cpp
// MyPlayer.cpp : 实现文件
//
#include "stdafx.h"
#include "MediaPlayer.h"
#include "MyPlayer.h"
#include ".\myplayer.h"
#include "vfw.h"
#include "afx.h"
// CMyPlayer
IMPLEMENT_DYNAMIC(CMyPlayer, CWnd)
CMyPlayer::CMyPlayer()
: m_parent(NULL)
, KeepRate(true)
{
mciWnd=NULL;
state.cur=state.total=state.order=0;
state.order=0;
state.ps=Stopped;
mciWnd=NULL;
}
CMyPlayer::~CMyPlayer()
{
}
BEGIN_MESSAGE_MAP(CMyPlayer, CWnd)
// ON_WM_PAINT()
ON_MESSAGE(MCIWNDM_NOTIFYPOS,OnNotifyPos)
ON_MESSAGE(MCIWNDM_NOTIFYMODE ,OnNotifyMode)
ON_WM_PAINT()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
// CMyPlayer 消息处理程序
LRESULT CALLBACK AFX_EXPORT
RygWndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CWnd *pWnd;
pWnd=CWnd::FromHandlePermanent(hWnd);
if(pWnd==NULL)
{
pWnd=new CMyPlayer();
pWnd->Attach(hWnd);
}
ASSERT(pWnd->m_hWnd==hWnd);
ASSERT(pWnd==CWnd::FromHandlePermanent(hWnd));
LRESULT lResult=AfxCallWndProc(pWnd,hWnd,message,wParam,lParam);
return lResult;
};
BOOL CMyPlayer::RegisterWndClass(HINSTANCE hInstance)
{
AfxOleInit();
WNDCLASS wc;
wc.lpszClassName="CMyPlayer";
wc.hInstance=hInstance;
wc.lpfnWndProc=RygWndProc;//
wc.hCursor=::LoadCursor(NULL,IDC_ARROW);
wc.hIcon=0;
wc.lpszMenuName=NULL;
wc.hbrBackground=(HBRUSH)::GetStockObject(BLACK_BRUSH);
wc.style=CS_GLOBALCLASS;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
return (::RegisterClass(&wc)!=0);
}
void CMyPlayer::Max()
{
CRect rect;
this->GetParent()->GetClientRect(rect);
this->MoveWindow(rect);
}
BOOL CMyPlayer::Play(CString file)
{
if(file.MakeLower().Find(".mp3")!=-1||file.MakeLower().Find(".wav")!=-1||file.MakeLower().Find(".mid")!=-1)
{
state.Message="音频文件"+file;
state.type=1;
}
else if(file.MakeLower().Find(".avi")!=-1||file.MakeLower().Find(".mpg")!=-1||file.MakeLower().Find(".wmv")!=-1)
{
state.Message="";
state.type=0;
}
else if(file.MakeLower().Find(".jpg")!=-1||file.MakeLower().Find(".bmp")!=-1||file.MakeLower().Find(".gif")!=-1)
{
state.Message=file;
state.type=2;
}
this->Invalidate();
if(state.type==1||state.type==0)
{
CRect rect;
this->GetClientRect(rect);
/*if(mciWnd!=NULL)
{
//Stop();
MCIWndClose(mciWnd);
MCIWndDestroy(mciWnd);
mciWnd=NULL;
}*/
//if(state.ps==Stopped)
{
if(mciWnd==NULL)
mciWnd =MCIWndCreate(this->GetSafeHwnd(), AfxGetInstanceHandle(),MCIWNDF_SHOWNAME|WS_CHILD|WS_VISIBLE/*|WS_OVERLAPPED*/|WS_VISIBLE|MCIWNDF_NOMENU |MCIWNDF_NOERRORDLG|MCIWNDF_NOTIFYALL|MCIWNDF_NOPLAYBAR, NULL);
//else
MCIWndOpen(mciWnd,(LPCSTR)file,0);
ModifyWindow();
state.total=MCIWndGetLength(mciWnd);
state.cur=0;
MCIWndPlay(mciWnd);
state.ps=Playing;
}
//else
//MCIWndResume(mciWnd);
::RedrawWindow(mciWnd,NULL,NULL,1|256|4);
}
if(state.type==2)
{
state.Message=file;
}
return 0;
}
LRESULT CMyPlayer::OnNotifyPos(WPARAM wParam, LPARAM lParam)
{
state.cur=(LONG)lParam;
this->GetParent()->SendMessage(WM_CHANGEPOS,lParam);
return LRESULT();
}
LRESULT CMyPlayer::OnNotifyMode(WPARAM wParam, LPARAM lParam)
{
if(lParam==MCI_MODE_STOP&&state.ps==Playing)
{
state.ps=Stopped;
state.cur=0;
this->GetParent()->SendMessage(WM_FINISH);
}
return LRESULT();
}
void CMyPlayer::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用
//CWnd::OnPaint();
dc.SetBkColor(RGB(0,0,0));
dc.SetTextColor(RGB(255,255,0));
if(state.type==1)
dc.TextOut(0,10,state.Message);
else if(state.type==2)
ShowPic(&dc);
}
BOOL CMyPlayer::PlayFromState(void)
{
MCIWndPlayFrom(mciWnd,state.cur);
state.ps=Playing;
return 0;
}
BOOL CMyPlayer::Stop(void)
{
state.cur=0;
state.ps=Stopped;
MCIWndStop(mciWnd);
state.cur=0;
this->GetParent()->SendMessage(WM_CHANGEPOS,state.cur);
return 0;
}
BOOL CMyPlayer::Pause(void)
{
MCIWndPause(mciWnd);
state.ps=Pausing;
return 0;
}
BOOL CMyPlayer::MoveWindow(int x,int y,int nWidth,int nHeight,BOOL bRepaint)
{
CWnd::MoveWindow(x,y,nWidth,nHeight,bRepaint);
ModifyWindow();
return 1;
}
BOOL CMyPlayer::MoveWindow(LPCRECT lpRect,BOOL bRepaint)
{
MoveWindow(lpRect->left,lpRect->top,lpRect->right-lpRect->left,lpRect->bottom-lpRect->top,bRepaint);
return 1;
}
void CMyPlayer::OnContextMenu(CWnd* pWnd, CPoint point)
{
//TRACE("Menu*/
// TODO: 在此处添加消息处理程序代码
this->GetParent()->SendMessage(WM_CONTEXTMENU,(WPARAM)this->GetParent()->GetSafeHwnd(),MAKELPARAM(point.x,point.y));
}
LONG CMyPlayer::GetVolume(void)
{
return MCIWndGetVolume(mciWnd);
}
BOOL CMyPlayer::SetVolumn(long vol)
{
return MCIWndSetVolume(mciWnd,vol);
}
CRect CMyPlayer::GetSource(void)
{
CRect rect;
MCIWndGetSource(mciWnd,rect);
return rect;
}
void CMyPlayer::ModifyWindow(void)
{
CRect rect1;
this->GetClientRect(rect1);
if(KeepRate)
{
CRect rect=GetSource();
if(1.0*rect.Width()/rect.Height()>1.0*rect1.Width()/rect1.Height())
::MoveWindow(mciWnd,0,(rect1.Height()-rect1.Width()*rect.Height()*1.0/rect.Width())/2,rect1.Width(),rect1.Width()*rect.Height()*1.0/rect.Width(),0);
else
::MoveWindow(mciWnd,(rect1.Width()-1.0*rect.Width()*rect1.Height()/rect.Height())/2,0,1.0*rect.Width()*rect1.Height()/rect.Height(),rect1.Height(),0);
}
else
::MoveWindow(mciWnd,0,0,rect1.Width(),rect1.Height(),0);
::RedrawWindow(mciWnd,NULL,NULL,1|256|4);
//if(state.type==2)
this->Invalidate();
}
void CMyPlayer::ShowPic(CDC* pDC)
{
CRect rect;
this->GetClientRect(rect);
CString file=state.Message;
IPicture *pPic;
IStream *pStm;
CFileStatus fstatus;
CFile fileR;
LONG cb;
if (fileR.Open(file,CFile::modeRead)&&fileR.GetStatus(file, fstatus)&&((cb = fstatus.m_size) != -1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal)) != NULL)
{
fileR.Read(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
double fX,fY;
fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
if(fX>rect.Width())
// if(fX/fY>hmWidth/hmHeight)
{
fX=rect.Width();
fY=fX*hmHeight/hmWidth;
}
if(fY>rect.Height())
{
fY=rect.Height();
fX=fY*hmWidth/hmHeight;
}
if(FAILED(pPic->Render(*pDC,(rect.Width()-fX)/2,(rect.Height()-fY)/2,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
AfxMessageBox("Failed To Render The picture!");
pPic->Release();
}
else
AfxMessageBox("Error Loading Picture From Stream!");
}
}
}
else
AfxMessageBox("Cant Open Image File!");
}
void CMyPlayer::OnLButtonUp(UINT nFlags, CPoint point)
{
if(state.type==2)
this->GetParent()->SendMessage(WM_NEXT);
CWnd::OnLButtonUp(nFlags, point);
}
BOOL CMyPlayer::PlayNew(CString file)
{
// MCIWndClose(mciWnd);
Stop();
Play(file);
return 0;
}