目前正在学习MFC,并决定制作游戏Gomoku。这是我到目前为止掌握的密码。
**mainframe.h**
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
protected:
DECLARE_DYNAMIC(CMainFrame)
public:
public:
public:
public:
virtual ~CMainFrame();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
DECLARE_MESSAGE_MAP()
void DrawBoard(CDC* pDC);
int m_nNextChar;
int board[15][15];
static const int EMPTY = 0, WHITE = 1, BLACK = 2;
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnPaint();
};
**mainframe.cpp**
#include "stdafx.h"
#include "01.win32tomfc.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMainFrame
IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_PAINT()
END_MESSAGE_MAP()
int diameter = 23;
int size = 40;
int xCod;
int yCod;
int xCodx;
int yCody;
// CMainFrame ¹¹Ôì/Îö¹¹
CMainFrame::CMainFrame()
{
m_nNextChar = BLACK;
Create(NULL, _T("Generic Sample Application"));
CRect rect(0, 0, 700, 700);
CalcWindowRect(&rect);
SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
}
CMainFrame::~CMainFrame()
{
}
// CMainFrame Õï¶Ï
#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
CFrameWnd::AssertValid();
}
void CMainFrame::Dump(CDumpContext& dc) const
{
CFrameWnd::Dump(dc);
}
#endif //_DEBUG
// CMainFrame ÏûÏ¢´¦Àí³ÌÐò
void CMainFrame::DrawBoard(CDC * pDC)
{
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = pDC->SelectObject(&pen);
for (int i = 1; i <= 16; i++) {
pDC->MoveTo(40 * i, 40);
pDC->LineTo(40 * i, 640);
pDC->MoveTo(40, 40 * i);
pDC->LineTo(640, 40 * i);
}
pDC->SelectObject(pOldPen);
}
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(BLACK_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if (m_nNextChar != BLACK )
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
}
}
m_nNextChar = WHITE;
CFrameWnd::OnLButtonDown(nFlags, point);
}
void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(WHITE_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if (m_nNextChar != WHITE)
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
}
}
m_nNextChar = BLACK;
CFrameWnd::OnRButtonDown(nFlags, point);
}
void CMainFrame::OnPaint()
{
CPaintDC dc(this);
DrawBoard(&dc);
}我的代码在函数DrawBoard()中绘制了一个15X15网格,并分别在OnLButtonDown和OnRButtonDown中绘制了黑白部分。问题是,当我运行程序,然后单击绘制黑片,然后白片,白色的部分可以绘制在黑色的部分,反之亦然。因此,我想创建一个二维数组board15,以便在其绘制时存储一个片段,这样一个不同的片段就不能在当前的一个片段上绘制,这将是最好的(我在正确的轨道上)。我试过了,但我似乎想不出怎么做。我不太擅长编程,并意识到这可能很容易,但一些帮助将是非常感谢的。请解释一下我怎么做才是正确的。
这就是我试过的。
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(BLACK_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if ((m_nNextChar != BLACK) && (board[xCod][yCod] = WHITE) )
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
board[xCod][yCod] = BLACK;
}
}
void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
CClientDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
CPen* pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(WHITE_BRUSH);
xCod = (point.x + (size / 2)) / size;
xCod = (xCod * size) - diameter / 2;
yCod = (point.y + (size / 2)) / size;
yCod = (yCod * size) - diameter / 2;
xCodx = xCod + diameter;
yCody = yCod + diameter;
if (m_nNextChar != WHITE && (board[xCod][yCod] = BLACK))
return;
else {
if (xCod > 20 && yCod <= 640 && xCodx < 655 && yCody > 40) {
dc.Ellipse(xCod, yCod, xCodx, yCody);
board[xCod][yCod] = WHITE;
}
}
m_nNextChar = BLACK;
CFrameWnd::OnRButtonDown(nFlags, point);
}发布于 2017-12-03 03:54:18
你应该用OnPaint做所有的图纸。不要绘制其他函数,如OnLButtonDown。相反,从OnLButtonDown获取必要的信息并调用Invalidate,这将重新绘制窗口。
下面是一个例子。为了简单起见,我创建了一个结构info和一个二维数组data。data存储每个单元格的所有信息,即矩形和颜色。您必须初始化一次data,并根据data中的信息进行绘制。
#include <vector>
class CMainFrame : public CFrameWnd
{
...
struct info
{
CRect rect;
int color;
};
std::vector<std::vector<info>> data;
};
CMainFrame::CMainFrame()
{
...
data.resize(15);
for(int i = 0; i < data.size(); i++)
data[i].resize(15);
int xoffset = 20;
int yoffset = 20;
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
data[row][col].rect.SetRect(0, 0, size + 1, size + 1);
data[row][col].rect.MoveToXY(xoffset + row * size, yoffset + col * size);
}
}
}
void CMainFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
CFrameWnd::OnLButtonDown(nFlags, point);
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
if(data[row][col].color)
break;
if(data[row][col].rect.PtInRect(point))
{
data[row][col].color = WHITE;
break;
}
}
}
Invalidate(FALSE);
}
void CMainFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
CFrameWnd::OnRButtonDown(nFlags, point);
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
if(data[row][col].color)
break;
if(data[row][col].rect.PtInRect(point))
{
data[row][col].color = BLACK;
break;
}
}
}
Invalidate(FALSE);
}
void CMainFrame::OnPaint()
{
CPaintDC dc(this);
CPen pen(PS_SOLID, 1, RGB(0, 0, 0));
dc.SelectObject(&pen);
CBrush white, black;
white.CreateSolidBrush(RGB(255, 255, 255));
black.CreateSolidBrush(RGB(0, 0, 0));
for(int row = 0; row < 15; row++)
{
for(int col = 0; col < 15; col++)
{
dc.Rectangle(data[row][col].rect);
if(data[row][col].color)
{
CBrush *oldbrush;
if(data[row][col].color == WHITE)
oldbrush = dc.SelectObject(&white);
else
oldbrush = dc.SelectObject(&black);
dc.Ellipse(data[row][col].rect);
dc.SelectObject(oldbrush);
}
}
}
}https://stackoverflow.com/questions/47610276
复制相似问题