mplayerdlg.cpp

来自「c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++」· C++ 代码 · 共 737 行 · 第 1/2 页

CPP
737 行
字号
void CMPlayerDlg::UpdateWindowEx(bool bOpened)
{
	m_btnFullScreen.EnableWindow(bOpened);
	m_btnPause.EnableWindow(bOpened);
//	m_btnPlay.EnableWindow(bOpened);
	m_btnStop.EnableWindow(bOpened);
	m_btnVolDec.EnableWindow(bOpened);
	m_btnVolInc.EnableWindow(bOpened);
	m_sld.EnableWindow(bOpened);
}

void CMPlayerDlg::OnFullscreen() 
{
	// TODO: Add your control notification handler code here

	if(!bIsVideo)
		return;
	CWnd *pMediaWnd=GetMediaWnd();
/////////// for release virsion debug/////////////
//	CString strHandle;
//	strHandle.Format("%d",pMediaWnd);
//	AfxMessageBox(strHandle);
//////////////////////////////////////////////////
	LONG widthWnd=m_mediaWndRect.right-m_mediaWndRect.left;
	LONG heightWnd=m_mediaWndRect.bottom-m_mediaWndRect.top;
	switch(iZoom){
	case NORMALSIZE:	
		pMediaWnd->ShowWindow(SW_SHOWNORMAL);
		pMediaWnd->ModifyStyle(0,WS_CAPTION);
		pMediaWnd->MoveWindow(&m_mediaWndRect);
		iZoom=DOUBLESIZE;
		break;
	case DOUBLESIZE:
		pMediaWnd->MoveWindow(0,0,widthWnd*2,heightWnd*2);
		iZoom=FULLSCREEN;
		break;
	case FULLSCREEN:
		m_media.SetFullScreen();
		iZoom=NORMALSIZE;
		break;
	}
}

void CMPlayerDlg::OnStop() 
{
	// TODO: Add your control notification handler code here
	m_media.Stop();
	m_curPlayTime.min=(UINT)0;
	m_curPlayTime.sec=(UINT)0;
	m_lcd.SetText(_T("Stop  00;00"));

	m_sld.SetPos(0);
	KillTimer(m_nTimer);
}

void CMPlayerDlg::OnPause() 
{
	// TODO: Add your control notification handler code here
	m_media.Pause();
	bPause=!bPause;
	CString strTime;
	if(bPause)
	{
		KillTimer(m_nTimer);
		strTime.Format("Pause %2d;%2d",m_curPlayTime.min,m_curPlayTime.sec);
	}else
	{  //continue 
		m_nTimer = SetTimer(1, 1000, 0); 
		strTime.Format("Play  %2d;%2d",m_curPlayTime.min,m_curPlayTime.sec);
	}
	m_lcd.SetText(strTime);
}

void CMPlayerDlg::InitSlider()
{
	m_curPlaySec=0;
	m_totalSec=m_media.GetTotalSec();
	m_sld.SetRange(0,m_totalSec);
	m_sld.SetTicFreq(1);
}

CWnd* CMPlayerDlg::GetMediaWnd()
{
	return FromHandle(m_media.GetHwnd());
}

void CMPlayerDlg::OnVolinc() 
{
	// TODO: Add your control notification handler code here
//	static int iFactor=10000;
	m_media.SetVolume(1);
//	iFactor+=100;
	ShellExecute(this->m_hWnd, "open", "sndvol32.exe", NULL, NULL, SW_SHOWNORMAL);

}

void CMPlayerDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CString dispText;
	TimeIncToStr(dispText);
	m_lcd.SetText((LPCTSTR)dispText);

	///set slider 
	if(m_curPlaySec==m_totalSec)
	{
		if(m_totalFiles>1)		//play next media
		{
			m_iList++;
			Play();	
		}else				//else stop	, not cycle
		{
			OnStop();
			return;
		}
	}
	m_curPlaySec++;
	m_sld.SetPos(m_curPlaySec);

	CDialog::OnTimer(nIDEvent);
}

void CMPlayerDlg::TimeIncToStr(CString &strTime)
{
	m_curPlayTime.sec++;
	if(m_curPlayTime.sec>=60)
	{
		m_curPlayTime.min++;
		m_curPlayTime.sec=(UINT)0;
	}
	strTime.Format("Play  %2d;%2d",m_curPlayTime.min,m_curPlayTime.sec);
}



void CMPlayerDlg::OnReleasedcaptureSlider(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	m_curPlaySec=m_sld.GetPos();
	m_curPlayTime.min=m_curPlaySec/60;
	m_curPlayTime.sec=m_curPlaySec%60;
	if(bIsVideo)	//video media
	{
		m_currentFrame=m_curPlaySec*m_frameRate;
		m_media.Play(m_currentFrame);	//play from new frame
	}else	//music media
	{
		m_media.Play(m_curPlaySec*1000);	//play from new position in milliseconds
	}
	*pResult = 0;
}

void CMPlayerDlg::OnExt() 
{
	// TODO: Add your control notification handler code here
	if(!bExt)	//如果不处在列表展开状态,现在展开
	{
		SetWindowPos(0,0,0,MPLAYERWIDTH,MPLAYEREXT,SWP_NOZORDER|SWP_NOMOVE);
		m_btnExt.SetBitmaps(IDB_N_EXT,NULL,NULL);
		m_btnExt.SetTooltipText("隐藏文件列表");
	}
	else	//如果已经显示列表了,则恢复正常显示状态
	{
		SetWindowPos(0,0,0,MPLAYERWIDTH,MPLAYERHEIGHT,SWP_NOZORDER|SWP_NOMOVE);
		m_btnExt.SetBitmaps(IDB_EXT,NULL,NULL);
		m_btnExt.SetTooltipText("显示文件列表");
	}
	bExt=!bExt;
}

void CMPlayerDlg::OnDblclkList() 
{
	// TODO: Add your control notification handler code here
	m_iList=m_list.GetCurSel();
	OnPlay();
}

bool CMPlayerDlg::OpenFiles(bool bListAdd)
{
	//open files and add to list
	CFileDialog FileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR,
		_OPENTYPE);
	if(FileDlg.DoModal()!=IDOK)
		return FALSE;
	if(!bListAdd)
		m_list.ResetContent();
	POSITION pos=FileDlg.GetStartPosition();
	CString lpszFile;
	lpszFile=FileDlg.GetNextPathName(pos);
	while(1)
	{
		m_list.AddString(lpszFile);
		if(!pos)	break;
		lpszFile=FileDlg.GetNextPathName(pos);
	}	
	m_iList=0;
	m_totalFiles=m_list.GetCount();
	return TRUE;
}

bool CMPlayerDlg::Play()
{
	if(bOpened)	//close previous media
	{
		m_media.Close();
		bOpened=FALSE;
	}

	CString strError;
	m_list.SetSel(m_iList-1,FALSE);

	if(m_iList==m_totalFiles)	//list pointer flow out
		m_iList=0;
	m_list.SetSel(m_iList,TRUE);
	m_list.GetText(m_iList,m_curFile);
	m_media.Create(m_curFile,strError);
	if(strError!="OK")
	{
#if _DEBUG
		AfxMessageBox(strError);		//get error message
#endif
		CString errorMsg;
		errorMsg="Can not open file: ";
		errorMsg+=m_curFile;
		AfxMessageBox(errorMsg);
		return FALSE;
	}
/////////////////////////////////
	bFirstPlay=TRUE;	//first play the media
	BeginWaitCursor(); 
	m_media.Play(0);	//play from 0 position

	// get orginal size of Media window
	bIsVideo=m_media.IsVideo();
	if(bFirstPlay && bIsVideo)	//first play the media ,get the orginal window rectangle size
	{
		CWnd* pMediaWnd=GetMediaWnd();
		pMediaWnd->GetWindowRect(&m_mediaWndRect);
		iZoom=DOUBLESIZE;
		bFirstPlay=FALSE;
	}
	//update window
	bOpened=TRUE;
	bPause=FALSE;
	UpdateWindowEx(bOpened);
	InitSlider();			//initialize the slider bar
	//////set the timer, 1 second interval
	m_nTimer = SetTimer(1, 1000, 0);
	m_curPlayTime.min=(UINT)0;
	m_curPlayTime.sec=(UINT)0;
//	m_lcd.AdjustClientXToSize(10);
	m_lcd.SetText(_T("Play  00;00"));
	m_lcd.StopScroll();
//////////// get framerate if video file
	if(bIsVideo)
		m_frameRate=m_media.GetFrameRate();
	else
		m_frameRate=0;
	EndWaitCursor(); 
	return TRUE;
}

void CMPlayerDlg::OnListAdd() 
{
	// TODO: Add your control notification handler code here
	OpenFiles(1);
}

void CMPlayerDlg::OnListRemove() 
{
	// TODO: Add your control notification handler code here
	int selectCounts=m_list.GetSelCount();
	int* lpInt=new int[m_totalFiles+1];
	//for(int i=0;i<m_totalFiles;i++)
	//	lpInt[i]=-1;

	int temp=m_list.GetSelItems(m_totalFiles,lpInt);
	for(int j=0;j<temp;)
	{
		m_list.DeleteString(lpInt[j]);
		j++;
		lpInt[j]-=j;
	}
	delete []lpInt;
	m_totalFiles=m_list.GetCount();
}

void CMPlayerDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	SaveList();
}

bool CMPlayerDlg::SaveList()
{
	CFile fileList;
	if(!fileList.Open("MPlayer.mdr",CFile::modeCreate|CFile::modeWrite))
		return FALSE;	
	char fileName[MAXPATH];
	for(int i=0;i<m_totalFiles;i++)
	{
		m_list.GetText(i,fileName);
		fileList.Write(fileName,strlen(fileName));
		fileList.Write("\r\n",2);
	}
	fileList.Write("----",4);
	fileList.Close();
	return TRUE;
}

bool CMPlayerDlg::OpenList()
{
	CFile fileList;
	if(!fileList.Open("MPlayer.mdr",CFile::modeNoTruncate|CFile::modeRead))
		return FALSE;	
	char ch;
	while(1)
	{
		CString fileName;
		fileList.Read(&ch,1);	//read first ch for each line
		if(ch=='-') break;  //end file
		fileName+=ch;
		while(1)
		{
			fileList.Read(&ch,1);
			if(ch=='\r')
			{
				fileList.Read(&ch,1);//read '\n' and move pointer to next line
				break;
			}
			fileName+=ch;
//			if(ch=='\\')
//				fileName+='\\';
		}
		m_list.AddString(fileName);
	}
	fileList.Close();
	m_totalFiles=m_list.GetCount();
	return TRUE;
}

BOOL CMPlayerDlg::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(m_hAccelTable)
	{
	if(::TranslateAccelerator(m_hWnd,m_hAccelTable,pMsg))
		return true; 
	}	

	return CDialog::PreTranslateMessage(pMsg);
}

void CMPlayerDlg::OnVolDec() 
{
	// TODO: Add your control notification handler code here
	ShellExecute(this->m_hWnd, "open", "sndvol32.exe", NULL, NULL, SW_SHOWNORMAL);
/*	CAlexfMixer mixer(this->m_hWnd, MIXERLINE_COMPONENTTYPE_DST_SPEAKERS,
                  NO_SOURCE, MIXERCONTROL_CONTROLTYPE_VOLUME);
	if (!mixer.IsOk()) 
		 return;

	mixer.SetControlValue(0);
*/ //no usefull
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?