【VC开源代码栏目提醒】:网学会员--在 VC开源代码编辑为广大网友搜集整理了:CellsView.cpp绩等信息,祝愿广大网友取得需要的信息,参考学习。
// CellsView.cpp : implementation of the CCellsView class
//
#include "stdafx.h"
#include "Cells.h"
#include "CellsDoc.h"
#include "CellsView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCellsView
IMPLEMENT_DYNCREATE(CCellsView, CView)
BEGIN_MESSAGE_MAP(CCellsView, CView)
//{{AFX_MSG_MAP(CCellsView)
ON_WM_TIMER()
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(ID_LIFE, OnLife)
ON_COMMAND(ID_RAND, OnRand)
ON_COMMAND(ID_DEAD, OnDead)
ON_COMMAND(ID_DEC, OnDec)
ON_COMMAND(ID_ADD, OnAdd)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCellsView construction/destruction
CCellsView::CCellsView()
{
// TODO: add construction code here
t=500;
}
CCellsView::~CCellsView()
{
}
BOOL CCellsView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCellsView drawing
void CCellsView::OnDraw(CDC* pDC)
{
CCellsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CCellsView diagnostics
#ifdef _DEBUG
void CCellsView::AssertValid() const
{
CView::AssertValid();
}
void CCellsView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCellsDoc* CCellsView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCellsDoc)));
return (CCellsDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCellsView message handlers
void CCellsView::OnInitialUpdate()
{
CView::OnInitialUpdate();
Initial(1);
SetTimer(0,t,NULL);
}
void CCellsView::Initial(int ID)
{
srand((unsigned)time(NULL));
for(int i=0;i<350;i++)
for(int j=0;j<350;j++)
{
if(ID==1)
Cells[i][j]=1;
if(ID==2)
Cells[i][j]=0;
if(ID==3)
Cells[i][j]=rand()%2;
Temp[i][j]=Cells[i][j];
}
}
void CCellsView::OnTimer(UINT nIDEvent)
{
Life();
CString msg;
static long k=0;
k++;
CDC dc;
CDC* pDC=GetDC();
CBitmap bitmap;
CBitmap* pOldBitmap;
CRect client;
pDC->GetClipBox(client); //检取无效区
if(dc.CreateCompatibleDC(pDC))//创建一个与pDC兼容的内存设备环境
{
//创建一与pDC兼容的位图,大小为整个客户区
if(bitmap.CreateCompatibleBitmap(pDC,349,349))
{
OnPrepareDC(&dc,NULL); //使dc与pDC具有同样的映射关系
pOldBitmap=dc.SelectObject(&bitmap);//将位图选入内存环境
dc.SelectClipRgn(NULL); //使dc的整个客户区都成无效区
dc.IntersectClipRect(client); //再“与上”检取的无效区,使内存环境与pDC检取的无效区相等
}
}
for(int i=1;i<349;i++)
for(int j=1;j<349;j++)
{
if(Cells[i][j]==0)
dc.SetPixel(i,j,RGB(0,0,0));
if(Cells[i][j]==1)
dc.SetPixel(i,j,RGB(255,255,0));
}
msg.Format("第%d代",k);
dc.SetTextColor(RGB(0,0,255));
dc.SetBkMode(TRANSPARENT);
dc.TextOut(5,5,msg);
pDC->BitBlt(0,0,349,349,&dc,1,1,SRCCOPY);
dc.SelectObject(pOldBitmap);
ReleaseDC(pDC);
CView::OnTimer(nIDEvent);
}
void CCellsView::Life()
{
int nCount=0;
for(int i=1;i<349;i++)
for(int j=1;j<349;j++)
{
nCount=Cells[i-1][j]+Cells[i][j-1]+Cells[i][j+1]+Cells[i+1][j];
switch(nCount)
{
case 0:
Temp[i][j]=0;
break;
case 1:
break;
case 2:
Temp[i][j]=1;
break;
case 3:
Temp[i][j]=0;
break;
case 4:
Temp[i][j]=0;
break;
default:
break;
}
}
for(i=0;i<350;i++)
for(int j=0;j<350;j++)
Cells[i][j]=Temp[i][j];
}
void CCellsView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
for(int i=point.x-10;i<point.x+10;i++)
for(int j=point.y-10;j<point.y+10;j++)
{
if(i<0)
i=0;
if(j<0)
j=0;
if(i>350)
i=350;
if(j>350)
j=350;
Cells[i][j]=0;
}
CView::OnLButtonDblClk(nFlags, point);
}
void CCellsView::OnLife()
{
Initial(1);
}
void CCellsView::OnRand()
{
Initial(3);
}
void CCellsView::OnDead()
{
Initial(2);
}
void CCellsView