📄 mfc_smallpiecesview.cpp
字号:
// MFC_smallpiecesView.cpp : implementation of the CMFC_smallpiecesView class
//
#include "stdafx.h"
#include "MFC_smallpieces.h"
#include "MainFrm.h"
#include "MFC_smallpiecesDoc.h"
#include "MFC_smallpiecesView.h"
#include <math.h>
#include <time.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMFC_smallpiecesView
IMPLEMENT_DYNCREATE(CMFC_smallpiecesView, CView)
BEGIN_MESSAGE_MAP(CMFC_smallpiecesView, CView)
//{{AFX_MSG_MAP(CMFC_smallpiecesView)
ON_WM_PAINT()
ON_COMMAND(IDM_PAINT, OnMenuPaint)
ON_COMMAND(IDM_ANT, OnAnt)
ON_WM_TIMER()
ON_COMMAND(IDM_STOP, OnStop)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMFC_smallpiecesView construction/destruction
CMFC_smallpiecesView::CMFC_smallpiecesView(): m_bgBrush(RGB(3, 78, 105))
{
// TODO: add construction code here
}
CMFC_smallpiecesView::~CMFC_smallpiecesView()
{
}
BOOL CMFC_smallpiecesView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMFC_smallpiecesView drawing
void CMFC_smallpiecesView::OnDraw(CDC* pDC)
{
CMFC_smallpiecesDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMFC_smallpiecesView printing
BOOL CMFC_smallpiecesView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMFC_smallpiecesView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMFC_smallpiecesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMFC_smallpiecesView diagnostics
#ifdef _DEBUG
void CMFC_smallpiecesView::AssertValid() const
{
CView::AssertValid();
}
void CMFC_smallpiecesView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMFC_smallpiecesDoc* CMFC_smallpiecesView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFC_smallpiecesDoc)));
return (CMFC_smallpiecesDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMFC_smallpiecesView message handlers
void CMFC_smallpiecesView::OnInitialUpdate()
{
CView::OnInitialUpdate();
srand(time(NULL));
CDC *pDC = GetDC();
CRect rect;
GetClientRect(&rect);
// CMainFrame *pFrame = (CMainFrame *)GetParentFrame();
// pDC->LPtoDP(rect);
m_mapRect.left = 0; m_mapRect.top = 0;m_mapRect.right = 1000; m_mapRect.bottom = 1000;
// pFrame->m_wndToolBar.GetWindowRect(&rect);
// m_mapRect.bottom -= rect.Height();
// pFrame->m_wndStatusBar.GetWindowRect(&rect);
// m_mapRect.bottom -= rect.Height();
m_memDC.CreateCompatibleDC(pDC);
m_bitmap.CreateCompatibleBitmap(pDC, m_mapRect.Width(), m_mapRect.Height());
m_memDC.SelectObject(&m_bitmap);
m_memDC.Rectangle(m_mapRect);
// m_memDC.MoveTo(m_mapRect.TopLeft());
// m_memDC.LineTo(m_mapRect.BottomRight());
// m_memDC.MoveTo(m_mapRect.left, m_mapRect.bottom);
// m_memDC.LineTo(m_mapRect.right, m_mapRect.top);
int delta=m_mapRect.Height()/200;
for (int i=1;i<200;i++)
{
m_memDC.MoveTo(m_mapRect.left , m_mapRect.top+delta*i);
m_memDC.LineTo(m_mapRect.right , m_mapRect.top+delta*i);
}
delta = m_mapRect.Width()/100;
for (i=0; i<200; i++)
{
m_memDC.MoveTo(m_mapRect.left+delta*i,m_mapRect.top);
m_memDC.LineTo(m_mapRect.left+delta*i,m_mapRect.bottom);
}
/************************************************************************/
/* for (i=0;i<100;i++) //随机化环境的代码
{
for (int j=0;j<100;j++)
{
CRect rect(i*5,j*5,(i+1)*5,(j+1)*5);
CBrush brush1,brush2;
brush1.CreateSolidBrush(RGB(255,255,255));
brush2.CreateSolidBrush(RGB(255,0,0));
int temp = rand()%10;
if (temp<5)
{
m_memDC.FillRect(&rect,&brush1);
}
else
{
m_memDC.FillRect(&rect,&brush2);
}
}
} */
/************************************************************************/
ReleaseDC(pDC);
// TODO: Add your specialized code here and/or call the base class
x = 100;
y = 100;
m_direction = 1;
}
void CMFC_smallpiecesView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
dc.BitBlt(2, 2, m_mapRect.Width(), m_mapRect.Height(), &m_memDC, 0, 0, SRCCOPY);
// Do not call CView::OnPaint() for painting messages
}
void CMFC_smallpiecesView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
{
// TODO: Add your specialized code here and/or call the base class
CView::CalcWindowRect(lpClientRect, nAdjustType);
}
void CMFC_smallpiecesView::OnMenuPaint()
{
// TODO: Add your command handler code here
// 将m_bitmap的一个方格涂成灰色,这仅仅是一个试验代码
int x = rand()%100;
int y = rand()%100;
CRect rect(x*5,y*5,(x+1)*5,(y+1)*5);
CBrush brush1;
brush1.CreateSolidBrush(RGB(0,0,255));
m_memDC.FillRect(&rect,&brush1);
Invalidate();
}
void CMFC_smallpiecesView::OnAnt()
{
// TODO: Add your command handler code here
/************************************************************************/
/* CRect rect(x*5,y*5,(x+1)*5,(y+1)*5);
CBrush brush1;
brush1.CreateSolidBrush(RGB(0,0,255));
m_memDC.FillRect(&rect,&brush1); */
/************************************************************************/
m_nTimer = SetTimer(1,2000,0);
}
void CMFC_smallpiecesView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
for (int i = 0;i<1000;i++)
{
switch(m_direction)
{
case 1:
{
x--;
if (m_memDC.GetPixel(x*5+1,y*5+1)==RGB(255,255,255))
{
CRect rect1(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush1;
brush1.CreateSolidBrush(RGB(255,0,0));
m_memDC.FillRect(&rect1,&brush1);
m_direction = 4;
}
else
{
CRect rect2(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush2;
brush2.CreateSolidBrush(RGB(255,255,255));
m_memDC.FillRect(&rect2,&brush2);
m_direction = 2;
}
}
break;
case 2:
{
y--;
if (m_memDC.GetPixel(x*5+1,y*5+1)==RGB(255,255,255))
{
CRect rect1(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush1;
brush1.CreateSolidBrush(RGB(255,0,0));
m_memDC.FillRect(&rect1,&brush1);
m_direction = 1;
}
else
{
CRect rect2(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush2;
brush2.CreateSolidBrush(RGB(255,255,255));
m_memDC.FillRect(&rect2,&brush2);
m_direction = 3;
}
}
break;
case 3:
{
x++;
if (m_memDC.GetPixel(x*5+1,y*5+1)==RGB(255,255,255))
{
CRect rect1(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush1;
brush1.CreateSolidBrush(RGB(255,0,0));
m_memDC.FillRect(&rect1,&brush1);
m_direction = 2;
}
else
{
CRect rect2(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush2;
brush2.CreateSolidBrush(RGB(255,255,255));
m_memDC.FillRect(&rect2,&brush2);
m_direction = 4;
}
}
break;
case 4:
{
y++;
if (m_memDC.GetPixel(x*5+1,y*5+1)==RGB(255,255,255))
{
CRect rect1(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush1;
brush1.CreateSolidBrush(RGB(255,0,0));
m_memDC.FillRect(&rect1,&brush1);
m_direction = 3;
}
else
{
CRect rect2(x*5, y*5, (x+1)*5, (y+1)*5);
CBrush brush2;
brush2.CreateSolidBrush(RGB(255,255,255));
m_memDC.FillRect(&rect2,&brush2);
m_direction = 1;
}
}
break;
}
}
Invalidate();
CView::OnTimer(nIDEvent);
}
void CMFC_smallpiecesView::OnStop()
{
// TODO: Add your command handler code here
KillTimer(m_nTimer);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -