www.pudn.com > crain.rar > crainDlg.cpp


// crainDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "crain.h" 
#include "crainDlg.h" 
#include "math.h" 
 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCrainDlg dialog 
 
CCrainDlg::CCrainDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CCrainDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CCrainDlg) 
	m_dropnumber = 0; 
	m_ripplenumber = 0; 
	m_wind = 0; 
	//}}AFX_DATA_INIT 
    israinning=false; 
	soundon=false; 
	m_wind=0; 
	m_ripplenumber=0; 
	m_dropnumber=0;  
 
	dc=new struct dropletchain; 
	dc->drop=NULL; 
	dc->pre=dc; 
    dc->next=dc; 
	 
    rc=new struct ripplechain; 
	rc->aripple=NULL; 
	rc->pre=rc; 
	rc->next=rc; 
	 
	int i ; 
	for(i=0;i<1000;i++) 
	{ 
		vsin[i]=sin(i*6.284/1000); 
		vcos[i]=cos(i*6.284/1000); 
	} 
	 
 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CCrainDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CCrainDlg) 
	DDX_Text(pDX, IDC_EDIT5, m_dropnumber); 
	DDX_Text(pDX, IDC_EDIT6, m_ripplenumber); 
	DDX_Text(pDX, IDC_EDIT1, m_wind); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CCrainDlg, CDialog) 
	//{{AFX_MSG_MAP(CCrainDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_CANCELMODE() 
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1) 
	ON_WM_MOUSEMOVE() 
	ON_WM_CAPTURECHANGED() 
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2) 
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCrainDlg message handlers 
 
BOOL CCrainDlg::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); 
		} 
	} 
 
	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 
} 
 
void CCrainDlg::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 CCrainDlg::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(); 
	} 
} 
 
HCURSOR CCrainDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
//DEL void CCrainDlg::show() 
//DEL { 
//DEL  
//DEL } 
 
void CCrainDlg::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	 
 
	CDialog::OnLButtonDown(nFlags, point); 
} 
 
void CCrainDlg::OnCancelMode()  
{ 
	CDialog::OnCancelMode(); 
	 
 
} 
 
void CCrainDlg::draw() 
{ 
         CWnd * PaintWind=this->GetDlgItem(ID_P); 
        CDC * PaintWindDC=PaintWind->GetDC(); 
        PaintWind->GetClientRect(aRect); 
		 
		Rect1.bottom=aRect.bottom ; 
		Rect1.right =aRect.right ; 
		Rect1.left =aRect.left ; 
		Rect1.top =aRect.bottom * 2 / 3; 
		 
		 
	 
		 
       PaintWindDC->SetROP2(R2_XORPEN); 
 
    struct dropletchain * adropchain; 
	droplet * adrop; 
    if(dc->next != dc) 
	{ 
		adropchain=dc->next; 
	    while(adropchain!=dc) 
		{ 
            adrop=adropchain->drop; 
			if(adrop->visibility) 
			{ 
				aBrush.DeleteObject(); 
			    aBrush.CreateSolidBrush(adrop->color); 
				aRgn.DeleteObject (); 
				aRgn.CreateEllipticRgn (adrop->x-adrop->radius, 
				                 adrop->y-adrop->radius, 
								 adrop->x+adrop->radius, 
								 adrop->y+adrop->radius); 
			    PaintWindDC->FillRgn(&aRgn,&aBrush); 
			     
			 
            } 
			adropchain=adropchain->next; 
     
		} 
	} 
	 
     
	struct ripplechain * aripplechain; 
	ripple * aripple; 
	if(rc->next != rc) 
	{ 
		aripplechain=rc->next; 
		 
		 
		while(aripplechain!=rc) 
		{ 
		     
			aripple=aripplechain ->aripple; 
		    aripple->shown=1-aripple->shown ; 
                    
            
			this->drawellipse (PaintWindDC,aripple->xdrop-aripple->radius, 
				                 aripple->ydrop-aripple->radius/2, 
								 aripple->xdrop+aripple->radius, 
								 aripple->ydrop+aripple->radius/2,aripple->color ); 
            this->drawellipse (PaintWindDC,aripple->xdrop-aripple->radius-1, 
				                 aripple->ydrop-aripple->radius/2-1, 
								 aripple->xdrop+aripple->radius+1, 
								 aripple->ydrop+aripple->radius/2+1,aripple->color ); 
			 
			 
		    aripplechain=aripplechain->next; 
		  
		 
		} 
	} 
	 
	PaintWind->ReleaseDC (PaintWindDC);	 
} 
 
void CCrainDlg::drive() 
{ 
    char s[10];    
 
     
		struct dropletchain * ndc=new struct dropletchain; 
	    droplet * ndrop=new droplet; 
	 
		ndrop->color=RGB(rand()%256,rand()%256,rand()%256); 
		ndrop->visibility =1; 
	 
		ndrop->radius =rand()%3+1; 
		ndrop->x=rand()%(aRect.right -aRect.left); 
		ndrop->y=rand()%(Rect1.top -aRect.top ); 
		ndrop->xvelocity=0; 
		ndrop->yvelocity =10; 
		ndc->drop =ndrop; 
		ndc->pre =dc; 
		ndc->next =dc->next ; 
		ndc->next->pre =ndc; 
	    dc->next =ndc; 
		this->m_dropnumber ++; 
		sprintf(s,"%d",m_dropnumber); 
		this->SetDlgItemText(IDC_EDIT5,s); 
 
 
 
     
    struct dropletchain * adropchain; 
	droplet * adrop; 
    if(dc->next != dc) 
	{ 
		adropchain=dc->next; 
	    while(adropchain!=dc) 
		{ 
            adrop=adropchain->drop; 
			adrop->x+=(adrop->xvelocity+xacce) ; 
			adrop->y+=(adrop->yvelocity+yacce) ; 
             
			if(adrop->x>aRect.right || 
				adrop->xyy>aRect.bottom ) 
			{ 
                 
				adropchain->next->pre=adropchain->pre; 
				adropchain=adropchain->next; 
				delete adropchain->pre->next->drop ; 
                delete adropchain->pre->next ; 
				adropchain->pre->next=adropchain; 
				this->m_dropnumber --; 
				sprintf(s,"%d",m_dropnumber); 
				this->SetDlgItemText(IDC_EDIT5,s); 
			} 
			else if(adrop->y>Rect1.top+40 && rand()%4==1) 
			{ 
			    struct ripplechain * arc=new struct ripplechain; 
				ripple * aripple=new ripple; 
				arc->aripple =aripple; 
				aripple->color=adrop->color ; 
				aripple->radius =adrop->radius ; 
				aripple->xdrop =adrop->x; 
				aripple->ydrop =adrop->y; 
				aripple->shown =0; 
				arc->next=rc->next ; 
				arc->pre =rc; 
				rc->next->pre =arc; 
				rc->next=arc; 
 
				adropchain->next->pre=adropchain->pre; 
				adropchain=adropchain->next; 
				delete adropchain->pre->next->drop ; 
                delete adropchain->pre->next ; 
				adropchain->pre->next=adropchain; 
				this->m_dropnumber --; 
				sprintf(s,"%d",m_dropnumber); 
				this->SetDlgItemText(IDC_EDIT5,s); 
 
				this->m_ripplenumber ++; 
		        sprintf(s,"%d",m_ripplenumber); 
		        this->SetDlgItemText(IDC_EDIT6,s); 
				if(rand()%10==1 && soundon)  
					Beep(4000,10); 
				 
 
			}	 
			else 
			{ 
				adropchain=adropchain->next; 
            }     
		} 
	} 
 
 
 
 
	struct ripplechain * aripplechain; 
	ripple * aripple; 
	if(rc->next != rc) 
	{ 
		aripplechain=rc->next; 
		 
		 
		while(aripplechain!=rc) 
		{ 
		     
			aripple=aripplechain ->aripple; 
		    if(aripple->radius>40 && aripple->shown ==0) 
			{ 
                aripplechain->next->pre=aripplechain->pre; 
				aripplechain=aripplechain->next; 
				delete aripplechain->pre->next->aripple  ; 
                delete aripplechain->pre->next ; 
				aripplechain->pre->next=aripplechain; 
				this->m_ripplenumber --; 
		        sprintf(s,"%d",m_ripplenumber); 
		        this->SetDlgItemText(IDC_EDIT6,s); 
			} 
			else 
			{ 
				aripple->radius +=1; 
		        aripplechain=aripplechain->next; 
			} 
		 
		 
		} 
	} 
 
} 
 
void CCrainDlg::OnButton1()  
{ 
      
 
if(!israinning) 
	{ 
		israinning=true; 
		endthread=false; 
		this->SetDlgItemText(IDC_BUTTON1,"stop rain"); 
	     
		aThread=AfxBeginThread(onthread,this ); 
    } 
	else 
	{ 
        israinning=false; 
		endthread=true; 
		this->SetDlgItemText(IDC_BUTTON1,"rain please"); 
 
	} 
	 
 
     
 
} 
//DEL UINT CCrainDlg::pThread(LPVOID pParam) 
//DEL { 
//DEL      AfxMessageBox("sldkfs"); 
//DEL 	 return 0; 
//DEL } 
 
void CCrainDlg::dothread() 
{ 
     
		 
	    CWnd * PaintWind=this->GetDlgItem(ID_P); 
 
 
 
	    
 
		aBrush.CreateSolidBrush(RGB(0,0,0)); 
		PaintWind->GetClientRect(aRect); 
        CDC * PaintWindDC=PaintWind->GetDC(); 
		PaintWindDC->FillRect(aRect,& aBrush); 
		 
        aBrush.DeleteObject(); 
		aBrush.CreateSolidBrush (RGB(0,0,255)); 
       
		Rect1.bottom=aRect.bottom ; 
		Rect1.right =aRect.right ; 
		Rect1.left =aRect.left ; 
		Rect1.top =aRect.bottom * 2 / 3; 
		PaintWindDC->FillRect(Rect1,&aBrush); 
        aBrush.DeleteObject ();   
		 
	 
 
        while(!this->endthread ) 
		{ 
			draw();	 
		    draw(); 
			drive(); 
			 
		} 
 
} 
UINT CCrainDlg::onthread(LPVOID wParam) 
{ 
    CCrainDlg * ad=(CCrainDlg *)wParam; 
	ad->dothread (); 
	return 0; 
} 
 
 
void CCrainDlg::drawellipse(CDC *adc, int x1, int y1, int x2, int y2, COLORREF color) 
{ 
    int angle; 
	int x0=(x1+x2)/2; 
	int y0=(y1+y2)/2; 
	int rx=x0-x1; 
	int ry=y0-y1; 
    for(angle=0;angle<1000;angle+=20) 
	{ 
		adc->SetPixel (x0+rx*vcos[angle],y0+ry*vsin[angle],color); 
	} 
	 
} 
 
void CCrainDlg::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	char s[10];    
	CWnd * PaintWind=this->GetDlgItem(ID_P); 
	 
		PaintWind->GetClientRect(aRect); 
        CDC * PaintWindDC=PaintWind->GetDC(); 
  
		Rect1.bottom=aRect.bottom ; 
		Rect1.right =aRect.right ; 
		Rect1.left =aRect.left ; 
		Rect1.top =aRect.bottom * 2 / 3; 
		 
		if(point.x>aRect.left &&point.xaRect.top && point.ySetDlgItemText(IDC_EDIT2,s); 
		} 
		else 
		{ 
			    dir=90+asin(yacce/dir)/3.14 * 180; 
				if(xacce<0) dir=360-dir; 
				dir=dir/30; 
			    sprintf(s,"%d clock",(int)dir); 
		        this->SetDlgItemText(IDC_EDIT2,s); 
		} 
		this->m_wind =(int)sqrt(xacce*xacce+yacce*yacce); 
		sprintf(s,"%d",m_wind); 
		this->SetDlgItemText(IDC_EDIT1,s); 
 
	CDialog::OnMouseMove(nFlags, point); 
} 
 
void CCrainDlg::OnCaptureChanged(CWnd *pWnd)  
{ 
	// TODO: Add your message handler code here 
	 
	CDialog::OnCaptureChanged(pWnd); 
} 
 
void CCrainDlg::OnButton2()  
{ 
    this->endthread =true;  
	CAboutDlg ad; 
	ad.DoModal (); 
	this->OnOK();	 
} 
 
void CCrainDlg::OnButton3()  
{ 
    if(this->soundon ) 
	{ 
		soundon=false; 
		this->SetDlgItemText(IDC_BUTTON3,"hear rain"); 
	} 
	else 
	{ 
		soundon=true; 
			this->SetDlgItemText(IDC_BUTTON3,"you see , the nature is not always musical !"); 
	} 
}