www.pudn.com > 1littlebox.rar > littleboxDlg.cpp


// littleboxDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "littlebox.h" 
#include "littleboxDlg.h" 
 
#include "VolumeControl.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#include "MediaType.h" 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
static UINT indicators[] = 
{ 
	ID_SEPARATOR,           // status line indicator 
	ID_INDICATOR_CAPS, 
	ID_INDICATOR_NUM, 
	ID_INDICATOR_SCRL, 
}; 
 
///////////////////////////////////////////////////////////////////////////// 
// CLittleboxDlg dialog 
 
CLittleboxDlg::CLittleboxDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CLittleboxDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CLittleboxDlg) 
		// 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); 
	m_openFileName = ""; 
	m_openFilePath = ""; 
	m_openFileType = ""; 
    m_openFileSize = ""; 
	m_running_status=false; 
    m_load_status=false; 
	m_media_type=0; 
	m_media_time_long=0; 
     
	//*********************变量初始化********** 
	aa=""; 
	hour=0,minute=0,second=0;fi=-1;fr=0; 
	 
 
} 
 
void CLittleboxDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CLittleboxDlg) 
	DDX_Control(pDX, IDC_PROGRESS1, m_ProgressBar); 
	DDX_Control(pDX, IDC_EDIT2, m_source); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CLittleboxDlg, CDialog) 
	//{{AFX_MSG_MAP(CLittleboxDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_COMMAND(ID_MENUITEM32778, OnMenuitem32778) 
	ON_COMMAND(ID_MENUITEM32774, OnFileOpen) 
	ON_COMMAND(ID_MENUITEM32775, OnFileClose) 
	ON_COMMAND(ID_MENUITEM32776, OnFilePlay) 
	ON_COMMAND(ID_MENUITEM32777, OnFileStop) 
	ON_BN_CLICKED(IDC_BUTTON1, OnFileBrowse) 
	ON_BN_CLICKED(IDC_BUTTON2, OnPlay) 
	ON_WM_TIMER() 
	ON_BN_CLICKED(IDC_BUTTON3, OnStop) 
	ON_BN_CLICKED(IDC_BUTTON4, OnVolumeControl) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLittleboxDlg message handlers 
 
BOOL CLittleboxDlg::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 
	SetWindowText("my little player"); 
 
    if (!m_wndToolBar.Create(this) || 
		!m_wndToolBar.LoadToolBar(IDR_TOOLBAR1)) 
	{ 
		TRACE0("Failed to create toolbar\n"); 
		return -1;      // fail to create 
	} 
 
   if (!m_wndStatusBar.Create(this,WS_CHILD|WS_VISIBLE|WS_BORDER) || 
		!m_wndStatusBar.SetIndicators(indicators, 
		  sizeof(indicators)/sizeof(UINT))) 
	{ 
		TRACE0("Failed to create status bar\n"); 
		return -1;      // fail to create 
	} 
	UINT nID,nStyle; 
	int nWidth; 
	m_wndStatusBar.GetPaneInfo(0, nID, nStyle, nWidth) ; 
	m_wndStatusBar.SetPaneInfo(0, nID, SBPS_STRETCH, nWidth); 
 
 
	CRect	rcClientOld; // 原客户区矩形 
	CRect	rcClientNew; // 加入工具栏后的客户区矩形 
	GetClientRect(rcClientOld);  
 
 
// 计算加入工具栏后的客户区矩形大小,但不实际改变控件条布局 
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNew); 
 
	// 所有的对话框子窗口将被移动,以免被工具条覆盖,ptOffset为移动的距离 
	CPoint ptOffset(rcClientNew.left-rcClientOld.left, 
			rcClientNew.top-rcClientOld.top); 
 
	CRect	rcChild; 
	CWnd*	pwndChild = GetWindow(GW_CHILD);  //得到子窗口 
	while(pwndChild) // 历遍所有子窗口 
	{	// 移动所有子窗口 
		pwndChild->GetWindowRect(rcChild); 
		ScreenToClient(rcChild);  
		rcChild.OffsetRect(ptOffset);  
		pwndChild->MoveWindow(rcChild,FALSE);  
		pwndChild = pwndChild->GetNextWindow(); 
	} 
 
	CRect	rcWindow; 
	GetWindowRect(rcWindow); 
	// 修改对话框尺寸 
	rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); 
	rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height();  
	MoveWindow(rcWindow,FALSE); // 改变对话框大小并重绘 
 
	// 实现控件条布局,显示工具栏 
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0); 
    // 
	GetDlgItem(IDC_STATIC_STATUS)->GetWindowRect(&rcWindow); 
	ScreenToClient(&rcWindow); 
	m_wndStatusBar.MoveWindow(&rcWindow); 
 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CLittleboxDlg::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 CLittleboxDlg::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 CLittleboxDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
 
 
void CLittleboxDlg::OnMenuitem32778()  
{ 
	// TODO: Add your command handler code here 
		CAboutDlg dlgAbout; 
		dlgAbout.DoModal(); 
	 
} 
 
void CLittleboxDlg::OnFileOpen()  
{ 
	// TODO: Add your command handler code here 
	char filters[] = 
		"MP3 Files (*.mp3)|*.mp3|Wav Files (*.wav)|*.wav|Midi Files (*.mid)|*.mid|Midi Files (*.RMI)|*.RMI|All Files (*.*)|*.*|"; 
	CFileDialog fileDlg(TRUE, NULL, NULL, 
		NULL, filters, NULL);  
 
	int result = fileDlg.DoModal(); 
 
	if (result == IDOK) 
	{ 
		m_openFileName = fileDlg.GetFileName(); 
		m_openFilePath = fileDlg.GetPathName(); 
		m_openFileType = fileDlg.GetFileExt(); 
		//CString m_Path=fileDlg.GetFolderPath(); 
		CFileStatus status; 
	    CFile::GetStatus( m_openFilePath, status ) ; 
	 
		m_openFileSize.Format("%d",status.m_size); 
	     
       CString file_view_info=""; 
 
       file_view_info.Format("Name:%s Size:%sbytes.Type:%s", 
		   m_openFileName,m_openFileSize,m_openFileType); 
        CString temp_info; 
		 
	    m_source.SetWindowText(file_view_info); 
		 
		m_load_status=true; 
        char file_type[3]; 
		memset(file_type,0,3); 
		sprintf(file_type,"%s",m_openFileType); 
        if(file_type[0]=='m' && file_type[1]=='i'&& file_type[2]=='d'   )			 
		m_media_type=MEDIA_TYPE_MID; 
 
        if(file_type[0]=='R' && file_type[1]=='M'&& file_type[2]=='I'   )			 
		m_media_type=MEDIA_TYPE_MID; 
 
      if(file_type[0]=='w' && file_type[1]=='a'&& file_type[2]=='v'   )			 
		m_media_type=MEDIA_TYPE_WAV; 
 
	   if(file_type[0]=='W' && file_type[1]=='A'&& file_type[2]=='V'   )			 
		m_media_type=MEDIA_TYPE_WAV; 
       
	  if(file_type[0]=='m' && file_type[1]=='p'&& file_type[2]=='3'   )			 
		m_media_type=MEDIA_TYPE_MP3; 
       
          int  this_minutes=0; 
		  int  this_seconds=0; 
        m_media_time_long=0; 
         
		switch(m_media_type) 
        { 
		    case MEDIA_TYPE_MID: 
			m_Midi.OpenMid( m_openFilePath );            
            m_Midi.GetLengthMid(&this_minutes,&this_seconds);       
            m_media_time_long = this_minutes*60 + this_seconds; 
            temp_info.Format(" total %d seconds",m_media_time_long); 
			file_view_info+=temp_info; 
		    m_source.SetWindowText(file_view_info);	 
			break; 
			case MEDIA_TYPE_WAV: 
				//MessageBox("wav tobe process!"); 
            break; 
			case MEDIA_TYPE_MP3: 
			//	MessageBox("mp3 tobe process!"); 
            break; 
			default: 
			//	MessageBox("Unknown media type!"); 
			break; 
		} 
		 
 
 
		Invalidate(); 
	} 
	 
} 
 
void CLittleboxDlg::OnFileClose()  
{ 
	// TODO: Add your command handler code here 
	if(m_load_status) 
		{ 
	 
		m_source.SetWindowText("Nothing has been opened."); 
	 
		   switch(m_media_type) 
        { 
		    case MEDIA_TYPE_MID: 
			 if(m_running_status)  
				{ 
				 m_Midi.StopMid(); 
				 m_running_status=false; 
				 m_ProgressBar.SetPos(1); 
				 KillTimer(1); 
				} 
				m_Midi.CloseMid(); 
			    break; 
			case MEDIA_TYPE_WAV: 
				 
				m_Wave.Stop(); 
			    m_Wave.CloseDevice(); 
                m_running_status=false; 
				m_ProgressBar.SetPos(1); 
				KillTimer(1); 
 
			break; 
			 
			case MEDIA_TYPE_MP3: 
				//MessageBox("mp3 tobe process!"); 
				KillTimer(0); 
	            cdfrom=MCI_MAKE_HMS(0,0,0); 
	            mciSendCommand(m_count,MCI_CLOSE,0,NULL); 
				m_count=0; 
            break; 
 
			default: 
					KillTimer(0); 
	            cdfrom=MCI_MAKE_HMS(0,0,0); 
	            mciSendCommand(m_count,MCI_CLOSE,0,NULL); 
				m_count=0; 
			break; 
		}        
         
		m_openFileName = ""; 
		m_openFilePath = ""; 
		m_openFileType = ""; 
        m_openFileSize = ""; 
		m_load_status=false; 
 
		}else 
		{ 
		MessageBox("nothing open yet."); 
		} 
		Invalidate(); 
	 
} 
 
void CLittleboxDlg::OnFilePlay()  
{ 
	// TODO: Add your command handler code here 
		 
	//MCI打开参数 
	//MCI_OPEN_PARMS mciopenparms; 
				 
	//MCI播放参数 
	//MCI_PLAY_PARMS mciplayparms; 
 
	if(m_load_status) 
	{ 
	m_running_status=true;   
    m_ProgressBar.SetRange(1,3000); 
	m_ProgressBar.SetStep(10); 
	m_ProgressBar.SetPos(1); 
    SetTimer(1,15,NULL); 
 
	  switch(m_media_type) 
        { 
		    case MEDIA_TYPE_MID: 
			 m_Midi.PlayMid();		  
 
		    break; 
			case MEDIA_TYPE_WAV: 
				 
				// 最简单的播放WAV文件的方法  
				//sndPlaySound(m_openFilePath,SND_ASYNC); 
				 
				 m_Wave.OpenDevice(); 
				 m_Wave.Play(this,m_openFilePath); 
 
				break; 
			case MEDIA_TYPE_MP3:			 
                 CLittleboxDlg::Tplay(); 
             
			break; 
			 
			default: 
                 CLittleboxDlg::Tplay(); 
				//MessageBox("Unknown media type!"); 
			break; 
		} 
		 
			 
		  
	 
	 
 
	}else{ 
	MessageBox("nothing open yet."); 
	} 
	 
} 
 
void CLittleboxDlg::OnFileStop()  
{ 
	// TODO: Add your command handler code here 
	if(m_running_status) 
	{ 
		m_running_status=false; 
		m_ProgressBar.SetRange(1,1000); 
		m_ProgressBar.SetStep(10); 
		m_ProgressBar.SetPos(1); 
		KillTimer(1); 
	 
		 switch(m_media_type) 
        { 
		    case MEDIA_TYPE_MID: 
			m_Midi.StopMid();	  
 
		    break; 
			case MEDIA_TYPE_WAV: 
				 
			m_Wave.Stop(); 
			m_Wave.CloseDevice(); 
			break; 
			 
			case MEDIA_TYPE_MP3: 
				 
				KillTimer(0); 
	            cdfrom=MCI_MAKE_HMS(0,0,0); 
	            mciSendCommand(m_count,MCI_CLOSE,0,NULL); 
				m_count=0; 
            break; 
 
			default: 
					KillTimer(0); 
	            cdfrom=MCI_MAKE_HMS(0,0,0); 
	            mciSendCommand(m_count,MCI_CLOSE,0,NULL); 
				m_count=0; 
			break; 
 
		} 
	} 
	else 
	MessageBox("nothing playing."); 
	 
} 
 
void CLittleboxDlg::OnFileBrowse()  
{ 
	// TODO: Add your control notification handler code here 
	CLittleboxDlg::OnFileOpen() ; 
} 
 
void CLittleboxDlg::OnPlay()  
{ 
	// TODO: Add your control notification handler code here 
	CLittleboxDlg::OnFilePlay() ; 
} 
 
void CLittleboxDlg::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	m_ProgressBar.StepIt(); 
	CDialog::OnTimer(nIDEvent); 
} 
 
void CLittleboxDlg::OnStop()  
{ 
	// TODO: Add your control notification handler code here 
	CLittleboxDlg::OnFileStop() ; 
} 
void CLittleboxDlg::comp() 
{ 
	DWORD dwsf=getinfo(MCI_STATUS_POSITION); 
			cdfrom=MCI_MAKE_MSF(MCI_MSF_MINUTE(dwsf),MCI_MSF_SECOND(dwsf), 
                      MCI_MSF_FRAME(dwsf)); 
			mciSendCommand(m_count,MCI_CLOSE,0,NULL); 
			m_count=0; 
} 
 
DWORD CLittleboxDlg::getinfo(DWORD item) 
{ 
	MCI_STATUS_PARMS mcistatusparms; 
	mcistatusparms.dwCallback=(DWORD)GetSafeHwnd(); 
	mcistatusparms.dwItem=item; 
	mcistatusparms.dwReturn=0; 
	mciSendCommand(m_count,MCI_STATUS,MCI_STATUS_ITEM,(DWORD)&mcistatusparms); 
	return mcistatusparms.dwReturn; 
} 
void CLittleboxDlg::forfor() 
{ 
	int i=0; 
	for(i;i<=fi;i++) 
	{ 
		file1[i]=file[i]; 
	} 
} 
void CLittleboxDlg::forfor1() 
{ 
	int i=0; 
	for(i;i<=fi;i++) 
	{ 
		file[i]=file1[i]; 
	} 
} 
 
void CLittleboxDlg::Tplay() 
{ 
		MCI_OPEN_PARMS mciopenparms; 
		MCI_PLAY_PARMS mciplayparms; 
		mciopenparms.lpstrElementName=m_openFilePath; 
		mciopenparms.lpstrDeviceType=NULL; 
		mciSendCommand(0,MCI_OPEN,MCI_DEVTYPE_WAVEFORM_AUDIO,(DWORD)(LPVOID)&mciopenparms); 
		m_count=mciopenparms.wDeviceID; 
		mciplayparms.dwCallback=(DWORD)GetSafeHwnd();	 
		cdlen=getinfo(MCI_STATUS_LENGTH);                                                                                                                                                                                                                                               
		cdfrom=MCI_MAKE_HMS(0,0,0); 
		cdto=MCI_MAKE_HMS(MCI_HMS_HOUR(cdlen),MCI_HMS_MINUTE(cdlen),MCI_HMS_SECOND(cdlen));   
		mciplayparms.dwFrom=cdfrom; 
		mciplayparms.dwTo=cdto; 
		mciSendCommand(m_count,MCI_PLAY,MCI_TO|MCI_FROM,(DWORD)(LPVOID)& mciplayparms); 
	    SetTimer(0,1000,NULL); 
	//	m_sl.SetRange(0,cdlen); 
} 
 
void CLittleboxDlg::OnVolumeControl()  
{ 
	// TODO: Add your control notification handler code here 
	CVolumeControl vctDlg ; 
	vctDlg.DoModal(); 
	 
}