www.pudn.com > Music.rar > MusicDlg.cpp


// MusicDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "Music.h" 
#include "MusicDlg.h" 
#include "MusicData.h" 
//#include "math.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMusicDlg dialog 
 
CMusicDlg::CMusicDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMusicDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMusicDlg) 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	GraTop = 20; 
    GraBottom = 280; 
    GraLeft = 30; 
    GraRight =445; 
	myThread1=NULL; 
} 
 
void CMusicDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMusicDlg) 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMusicDlg, CDialog) 
	//{{AFX_MSG_MAP(CMusicDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(ID_QUIT, OnQuit) 
	ON_BN_CLICKED(ID_PLAY, OnPlay) 
	ON_BN_CLICKED(ID_STOP, OnStop) 
    ON_MESSAGE(WM_CONTROL,OnSendMsg) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMusicDlg message handlers 
 
BOOL CMusicDlg::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 
	 
	// TODO: Add extra initialization here 
	 
	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 CMusicDlg::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(); 
		DrawGra(); 
	} 
} 
 
// The system calls this to obtain the cursor to display while the user drags 
//  the minimized window. 
HCURSOR CMusicDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CMusicDlg::OnOK()  
{ 
	// TODO: Add extra validation here 
 
} 
 
void CMusicDlg::OnQuit()  
{ 
	// TODO: Add your control notification handler code here 
	SendMessage(WM_CLOSE); 
} 
 
 
 
HWND h=NULL; 
	 
UINT play(LPVOID param) 
{ 
   CMusicDlg *dlg=((CMusicDlg *)CWnd::FromHandle(h)); 
   int a,b; 
   CFont font2; 
   font2.CreateFont (25, 10, 0, 0, FW_THIN, 0, 0, 0, ANSI_CHARSET, 0, CLIP_STROKE_PRECIS, PROOF_QUALITY , VARIABLE_PITCH | FF_ROMAN,_T("Times New Roman")); 
   int ToPlay=(int)param; 
   int Currentx; 
   int Currenty; 
   for(int j=0;j<4;j++) 
   {  
	   Currentx=45; 
       Currenty=110;      
     for(int i=0;i<=59;i++) 
	 { 
    	 a=type[i]; 
	     b=music[i]; 
         dlg->DrawRect(Currentx+2,Currenty ,Currentx+18 , Currenty+25, RGB(0,0,0), RGB(0,0,0)); 
       	 if(j==0) 
             dlg->OutText(Currentx,Currenty,Context1[i], RGB(0, 180, 0), RGB(0,0,0), &font2); 
		 else if(j==1) 
 	     	 dlg->OutText(Currentx,Currenty,Context2[i], RGB(0, 180, 180), RGB(0,0,0), &font2); 
         else if(j==2) 
	    	 dlg->OutText(Currentx,Currenty,Context3[i], RGB(180, 0, 0), RGB(0,0,0), &font2); 
         else  
		 	 dlg->OutText(Currentx,Currenty,Context4[i], RGB(180, 180, 0), RGB(0,0,0), &font2); 
 
		 Currentx+=26; 
		 if(!((i+1)%15)&&(i+1)/15) 
		 { 
			 Currentx=45; 
		     Currenty+=30; 
		 } 
		 Beep(frequency[a][tone][b],3500*LastTime[i]/16); 
	 } 
   } 
	dlg->OutText(200,240 , "Yours Sincere Canon", RGB(0, 180, 0), RGB(0,0,0), &font2); 
    (dlg->GetDlgItem(ID_STOP))->EnableWindow(0); 
	(dlg->GetDlgItem(ID_PLAY))->SetWindowText("播放"); 
    return 0; 
} 
void CMusicDlg::OnPlay()  
{ 
     GetDlgItem(ID_STOP)->EnableWindow(1); 
     CString Name; 
     GetDlgItem(ID_PLAY)->GetWindowText(Name); 
	 if(Name=="播放") 
	 { 
		 GetDlgItem(ID_PLAY)->SetWindowText("暂停"); 
		 h=this->m_hWnd; 
		 myThread1=AfxBeginThread(play,NULL); 
	 } 
	 else if(Name=="暂停") 
	 { 
	     GetDlgItem(ID_PLAY)->SetWindowText("继续"); 
	     myThread1->SuspendThread(); 
	 } 
	 else 
	 { 
	 	 GetDlgItem(ID_PLAY)->SetWindowText("暂停"); 
	     myThread1->ResumeThread(); 
	 } 
 
      
} 
 
 
void CMusicDlg::DrawRect(int x1, int y1, int x2, int y2, long penColor, long brushColor) 
{ 
	CClientDC dc(this); 
 
	CPen Pen; 
	Pen.CreatePen(PS_SOLID, 3, penColor); 
	dc.SelectObject(&Pen); 
 
	CBrush brush(brushColor); 
	dc.SelectObject(&brush); 
	dc.Rectangle(x1, y1, x2, y2); 
 
	Pen.DeleteTempMap(); 
	ReleaseDC(&dc); 
} 
 
void CMusicDlg::OutText(int x, int y, CString str, long textColor, long bkColor, CFont *font) 
{ 
	CClientDC dc(this); 
	dc.SelectObject(font); 
	dc.SetBkMode(TRANSPARENT); 
	dc.SetTextColor(textColor); 
	dc.SetBkColor(bkColor); 
	dc.TextOut(x, y, str); 
	ReleaseDC(&dc); 
} 
 
void CMusicDlg::DrawLine(int x1, int y1, int x2, int y2, int DrawWidth, long rgb) 
{ 
	CClientDC dc(this); 
	CPen pen; 
	pen.CreatePen(PS_SOLID, DrawWidth, rgb); 
	dc.SelectObject(&pen); 
	 
    dc.MoveTo (x1, y1); 
    dc.LineTo (x2, y2); 
	pen.DeleteTempMap(); 
	ReleaseDC(&dc); 
} 
 
void CMusicDlg::DrawGra(/*int *data1, int *data2, int len*/) 
{ 
	DrawRect(GraLeft, GraTop, GraRight, GraBottom, RGB(0,0,0), RGB(0,0,0)); 
    
	int x,y,i; 
	int stepx,stepy; 
	int Currentx,Currenty; 
 
 	stepx=(GraRight-GraLeft)/20; 
	stepy=(GraBottom-GraTop)/20; 
	Currentx=GraLeft; 
    Currenty=GraBottom; 
	y=GraTop; 
	//画竖的网格线 
	for(i=0;i<20;i++) 
	{ 
        x=Currentx; 
		DrawLine ( Currentx, Currenty, x, y, 1, RGB(9, 17, 119)); 
        Currentx = Currentx+stepx; 
	} 
	Currenty = GraTop;	 
	//画横的网格线 
	 
	x=GraRight; 
	Currentx = GraLeft; 
	for(i=0;i<20;i++) 
	{ 
        y=Currenty; 
		DrawLine ( Currentx, Currenty, x, y, 1, RGB(9, 17, 119)); 
        Currenty = Currenty+stepy; 
	} 
 	CFont font1,font2; 
    font1.CreateFont (35, 20, 0, 0, FW_THIN, 0, 0, 0, ANSI_CHARSET, 0, CLIP_STROKE_PRECIS, PROOF_QUALITY , VARIABLE_PITCH | FF_ROMAN,_T("Times New Roman")); 
    font2.CreateFont (25, 10, 0, 0, FW_THIN, 0, 0, 0, ANSI_CHARSET, 0, CLIP_STROKE_PRECIS, PROOF_QUALITY , VARIABLE_PITCH | FF_ROMAN,_T("Times New Roman")); 
	OutText(90,30 , "友 谊 天 长 地 久", RGB(0, 180, 180), RGB(0,0,0), &font1); 
	OutText(150,70 , "-----献给我所有亲爱的兄弟姐妹", RGB(0, 180, 0), RGB(0,0,0), &font2); 
} 
 
void CMusicDlg::OnStop()  
{ 
	// TODO: Add your control notification handler code here 
	TerminateThread(myThread1->m_hThread,1); 
    GetDlgItem(ID_PLAY)->SetWindowText("播放");  
	GetDlgItem(ID_STOP)->EnableWindow(0); 
} 
 
void CMusicDlg::OnSendMsg(WPARAM wParam, LPARAM lParam)  
{ 
    if(wParam==0 && lParam==0) OnPlay(); 
	if(wParam==0 && lParam==1) OnStop(); 
	if(wParam==1 && lParam==1) OnQuit(); 
}