www.pudn.com > 2_1.zip > 2_1View.cpp, change:2001-10-27,size:11261b


// 2_1View.cpp : implementation of the CMy2_1View class 
// 
 
#include "stdafx.h" 
#include "2_1.h" 
 
#include "2_1Doc.h" 
#include "2_1View.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy2_1View 
 
IMPLEMENT_DYNCREATE(CMy2_1View, CView) 
 
BEGIN_MESSAGE_MAP(CMy2_1View, CView) 
	//{{AFX_MSG_MAP(CMy2_1View) 
	ON_WM_TIMER() 
	ON_WM_LBUTTONDOWN() 
	ON_WM_RBUTTONDOWN() 
	ON_WM_CREATE() 
	ON_COMMAND(ID_START, OnStart) 
	ON_WM_LBUTTONUP() 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy2_1View construction/destruction 
 
CMy2_1View::CMy2_1View() 
{ 
	// TODO: add construction code here 
	for(int ii=0;ii<16;ii++) 
		m_Bitmap[ii].LoadBitmap(IDB_BITMAP14+ii); 
    for(int jj=0;jj<4;jj++) 
		m_anniu[jj].LoadBitmap(IDB_ANNIU1+jj); 
    second=0;//计时 
	secondstart=0;//1时开始计时 
    m_RowCount=25;//行数 
	m_ColCount=16;//列数 
    leinum=80; 
	leftnum=leinum;//剩余雷数 
	jieshu=0;//jieshu=1时停止 
	int aa=0; 
	//初始化0 
	for(int i=0;i<m_RowCount;i++) 
	{ 
		for(int j=0;j<m_ColCount;j++) 
		{ 
			lei[i][j].shumu=0; 
			lei[i][j].weitu=0; 
		} 
	} 
	//获取当前时间 
	CTime time=GetCurrentTime(); 
	int s; 
	//获取秒数 
	s=time.GetSecond(); 
	//设置40个雷 
	do 
	{ 
		//以当前秒数为产生随机算法 
		int k=(rand()*s)%m_RowCount; 
		int l=(rand()*s)%m_ColCount; 
		if(lei[k][l].shumu!=-1) 
		{ 
			lei[k][l].shumu=-1;  
		    aa++;  
		} 
	  
	}while(aa!=leinum);    
    //给方格赋值,计算雷数 
	for(int a=0;a<m_RowCount;a++) 
		for(int b=0;b<m_ColCount;b++) 
			if(lei[a][b].shumu==0) 
			{ 
				for(int c=a-1;c<a+2;c++) 
					for(int d=b-1;d<b+2;d++) 
						if(c>=0&&c<m_RowCount&&d>=0&&d<m_ColCount) 
							if(lei[c][d].shumu==-1) 
								lei[a][b].shumu++;		 
			} 
} 
 
CMy2_1View::~CMy2_1View() 
{  
     
} 
 
BOOL CMy2_1View::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy2_1View drawing 
 
void CMy2_1View::OnDraw(CDC* pDC) 
{ 
	CMy2_1Doc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	//画背景 
    CBrush mybrush1; 
 	mybrush1.CreateSolidBrush(RGB(192,192,192)); 
 	CRect myrect1(0,0,1200,800); 
 	pDC->FillRect(myrect1,&mybrush1); 
  //画黑框 
	CBrush mybrush; 
 	mybrush.CreateSolidBrush(RGB(0,0,0)); 
 	CRect myrect(20,10,70,40); 
 	pDC->FillRect(myrect,&mybrush); 
 
	CRect myrect2(325,10,375,40); 
 	pDC->FillRect(myrect2,&mybrush); 
 
	CPen mypen; 
	CPen*myoldPen; 
	mypen.CreatePen(PS_SOLID,2,RGB(255,255,255)); 
    myoldPen=pDC->SelectObject(&mypen); 
//画黑框的白线 
	pDC->MoveTo(20,40); 
	pDC->LineTo(70,40); 
	pDC->LineTo(70,10); 
	pDC->MoveTo(325,40); 
	pDC->LineTo(375,40); 
	pDC->LineTo(375,10); 
//画雷区边线 
    for(int i=0;i<m_RowCount;i++) 
		for(int j=0;j<m_ColCount;j++) 
		{ 
			pDC->MoveTo(10+i*15,50+j*15+14); 
			pDC->LineTo(10+i*15,50+j*15);	 
			pDC->LineTo(10+i*15+14,50+j*15); 
		} 
	pDC->SelectObject(myoldPen); 
 
	CPen mypen2; 
	CPen*myoldPen2; 
	mypen2.CreatePen(PS_SOLID,1,RGB(0,0,0)); 
    myoldPen2=pDC->SelectObject(&mypen2); 
    for(int ii=0;ii<m_RowCount;ii++) 
		for(int jj=0;jj<m_ColCount;jj++) 
		{ 
			pDC->MoveTo(10+ii*15,50+jj*15+14); 
			pDC->LineTo(10+ii*15+14,50+jj*15+14);	 
			pDC->LineTo(10+ii*15+14,50+jj*15); 
		} 
	pDC->SelectObject(myoldPen2); 
 
 		 CDC Dc; 
  	 if(Dc.CreateCompatibleDC(pDC)==FALSE) 
  		  AfxMessageBox("Can't create DC");	 
	 //显示按钮 
	 Dc.SelectObject(m_anniu[0]); 
	 pDC->BitBlt(180,10,160,160,&Dc,0,0,SRCCOPY); 
     //判断显示什么位图 
	 //weitu=1已按下的数字区 
	 //weitu=2显示旗 
	 //weitu=3显示问号 
	 for(int a=0;a<m_RowCount;a++) 
		 for(int b=0;b<m_ColCount;b++) 
			{ 
				if(lei[a][b].weitu==1) 
				{ 
                    Dc.SelectObject(m_Bitmap[lei[a][b].shumu]); 
					pDC->BitBlt(a*15+10,b*15+50,160,160,&Dc,0,0,SRCCOPY); 
				} 
				if(lei[a][b].weitu==2) 
				{ 
					Dc.SelectObject(m_Bitmap[9]); 
					pDC->BitBlt(a*15+10,b*15+50,160,160,&Dc,0,0,SRCCOPY); 
				} 
				if(lei[a][b].weitu==3) 
				{ 
					Dc.SelectObject(m_Bitmap[10]); 
					pDC->BitBlt(a*15+10,b*15+50,160,160,&Dc,0,0,SRCCOPY); 
				} 
				//结束 
				if(jieshu==1&&lei[a][b].shumu==-1) 
				{ 
                    Dc.SelectObject(m_Bitmap[11]); 
					pDC->BitBlt(a*15+10,b*15+50,160,160,&Dc,0,0,SRCCOPY); 
					Dc.SelectObject(m_anniu[3]); 
					pDC->BitBlt(180,10,160,160,&Dc,0,0,SRCCOPY); 
				} 
				 
			} 
//显示黑框里的数字 
	int nOldDC=pDC->SaveDC(); 
	pDC->SetTextColor(RGB(255,0,0)); 
	pDC->SetBkColor(RGB(0,0,0)); 
	CFont font; 					              
	if(0==font.CreatePointFont(160,"Comic Sans MS")) 
	{ 
		AfxMessageBox("Can't Create Font"); 
	} 
	pDC->SelectObject(&font); 
	CString str;   
	if(leftnum<10)  
		str.Format("00%d",leftnum); 
	else 
		str.Format("0%d",leftnum);					 
	pDC->TextOut(25,10,str); 
	if(second<10) 
		str.Format("00%d",second); 
	else if(second<100) 
			str.Format("0%d" ,second); 
		else 
			str.Format("%d" ,second); 
	pDC->TextOut(330,10,str); 
	pDC->RestoreDC(nOldDC); 
			////////////////////////////////////////////// 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy2_1View printing 
 
BOOL CMy2_1View::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMy2_1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMy2_1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy2_1View diagnostics 
 
#ifdef _DEBUG 
void CMy2_1View::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CMy2_1View::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CMy2_1Doc* CMy2_1View::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy2_1Doc))); 
	return (CMy2_1Doc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMy2_1View message handlers 
 
void CMy2_1View::OnTimer(UINT nIDEvent)  
{ 
	// TODO: Add your message handler code here and/or call default 
	//结束,返回 
	if(jieshu==1) 
		return; 
	//显示个数为0的方格 
	leizero(); 
	//计时 
	if(secondstart>0) 
		secondstart++; 
	if(secondstart==20) 
	{ 
		secondstart=1; 
		second++;	 
		//重画时间 
    	CRect rect3; 
		rect3.left=325; 
		rect3.right=375; 
		rect3.top=10; 
		rect3.bottom=40; 
		InvalidateRect(&rect3);	 
	} 
	CView::OnTimer(nIDEvent); 
} 
 
void CMy2_1View::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default	 
    //获取指针pdc 
     CDC *pDC=GetDC(); 
	 CDC Dc; 
 	 if(Dc.CreateCompatibleDC(pDC)==FALSE) 
 		  AfxMessageBox("Can't create DC");	 
	 //显示按下按钮 
	 if(point.x>180&&point.x<210&&point.y>10&&point.y<40) 
	 { 
    	Dc.SelectObject(m_anniu[3]); 
    	pDC->BitBlt(180,10,160,160,&Dc,0,0,SRCCOPY);		 
	 } 
    if((point.x>=10)&&(point.x<=385)&&(point.y>=50)&&(point.y<=290)) 
	{			 
		if(jieshu==1) 
			return; 
 
		 //显示张口按钮 
		Dc.SelectObject(m_anniu[1]); 
		pDC->BitBlt(180,10,160,160,&Dc,0,0,SRCCOPY);		   
	 
		secondstart=1; 
		int a=(point.x-10)/15; 
		int b=(point.y-50)/15; 
		if(lei[a][b].weitu==0||lei[a][b].weitu==3) 
        { 
		    if(lei[a][b].shumu==-1) 
			{ 
				jieshu=1;  
				KillTimer(1); 
				Invalidate(); 
			} 
            else  
			{ 
				lei[a][b].weitu=1; 
				CRect rect; 
				rect.left=a*15+10; 
				rect.right=a*15+25; 
				rect.top=b*15+50; 
				rect.bottom=b*15+65; 
				InvalidateRect(&rect); 
			}	 
		} 
		 
	} 
 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void CMy2_1View::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
	//结束,返回 
	if(jieshu==1) 
		return; 
	if((point.x>=10)&&(point.x<=385)&&(point.y>=50)&&(point.y<=290)) 
	{		 
		int a=(point.x-10)/15; 
		int b=(point.y-50)/15; 
 
		if(lei[a][b].weitu==0||lei[a][b].weitu==3)	 
		{ 
			lei[a][b].weitu=2;			 
			leftnum--;	 
		 
		} 
	    else 
			if(lei[a][b].weitu==2) 
			{ 
				lei[a][b].weitu=3; 
				leftnum++; 
			} 
        //重画剩下雷数 
		CRect rect2; 
		rect2.left=20; 
		rect2.right=70; 
		rect2.top=10; 
		rect2.bottom=40; 
		InvalidateRect(&rect2);			 
		//重画打击格子 
		CRect rect; 
    	rect.left=a*15+10; 
		rect.right=a*15+25; 
		rect.top=b*15+50; 
		rect.bottom=b*15+65; 
		InvalidateRect(&rect);		 
	} 
	CView::OnRButtonDown(nFlags, point); 
} 
 
  
int CMy2_1View::OnCreate(LPCREATESTRUCT lpCreateStruct)  
{ 
	if (CView::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	// TODO: Add your specialized creation code here 
//	SetTimer(1,50,NULL); 
	return 0; 
} 
//扫描,如果是已经被按下且雷数为0,显示它周围的八个格,并重画 
void CMy2_1View::leizero() 
{ 
    for(int i=0;i<m_RowCount;i++) 
		for(int j=0;j<m_ColCount;j++) 
			if(lei[i][j].shumu==0&&lei[i][j].weitu==1) 
			{ 
				for(int n=i-1;n<i+2;n++) 
					for(int m=j-1;m<j+2;m++) 
						if(n>=0&&n<25&&m>=0&&m<m_ColCount) 
							if(lei[n][m].shumu!=-1&&lei[n][m].weitu==0) 
							{ 
								lei[n][m].weitu=1; 
								CRect rect; 
								rect.left=n*15+10; 
								rect.right=n*15+25; 
								rect.top=m*15+50; 
								rect.bottom=m*15+65; 
								InvalidateRect(&rect);		 
							} 
			} 
} 
 
void CMy2_1View::OnStart()  
{ 
	SetTimer(1,50,NULL); 
	// TODO: Add your command handler code here 
	second=0;//计时 
	secondstart=0;//1时开始计时 
//	num=0; 
    leftnum=leinum;//剩余雷数 
	jieshu=0;//jieshu=1时停止 
	int aa=0; 
	//初始化0 
	for(int i=0;i<m_RowCount;i++) 
	{ 
		for(int j=0;j<m_ColCount;j++) 
		{ 
			lei[i][j].shumu=0; 
			lei[i][j].weitu=0; 
		} 
	} 
	//设置40个雷 
	do 
	{ 
		int k=rand()%m_RowCount; 
		int l=rand()%m_ColCount; 
		if(lei[k][l].shumu!=-1) 
		{ 
			lei[k][l].shumu=-1;  
		    aa++;  
		} 
	  
	}while(aa!=leinum);    
    //给方格赋值 
	for(int a=0;a<m_RowCount;a++) 
		for(int b=0;b<m_ColCount;b++) 
			if(lei[a][b].shumu==0) 
			{ 
				for(int c=a-1;c<a+2;c++) 
					for(int d=b-1;d<b+2;d++) 
						if(c>=0&&c<m_RowCount&&d>=0&&d<m_ColCount) 
							if(lei[c][d].shumu==-1) 
								lei[a][b].shumu++;		 
			} 
	Invalidate();  
 
} 
 
void CMy2_1View::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	// TODO: Add your message handler code here and/or call default 
    CDC *pDC=GetDC(); 
 	CDC Dc; 
 	if(Dc.CreateCompatibleDC(pDC)==FALSE) 
 	  AfxMessageBox("Can't create DC");	 
	//显示按钮 
	Dc.SelectObject(m_anniu[0]); 
	pDC->BitBlt(180,10,160,160,&Dc,0,0,SRCCOPY); 
	 
	if(jieshu==1) 
	{ 
		Dc.SelectObject(m_anniu[2]); 
		pDC->BitBlt(180,10,160,160,&Dc,0,0,SRCCOPY); 
	}	 
 
	if(point.x>180&&point.x<210&&point.y>10&&point.y<40) 
	    OnStart(); 
 
	CView::OnLButtonUp(nFlags, point); 
}