//game.h
class CChess
{
private:
int clientWidth,clientHeight; //定义客户区的高度和宽度
int numberRow,numberCol;//定义棋盘的行和列
RECT rect;//框架的位置数据定义 GDI RECT 而不是 MFC CRect
HWND m_hWnd;//用来识别各种不同资源的一个句柄
HDC dc;
HPEN pen;
public:
CChess();
~CChess();
void ChessBoard();
};
//game.cpp
#include "stdafx.h"
#include "game.h"
CChess::CChess()
{
clientWidth=clientHeight=0;
numberRow=numberCol=0;
}
void CChess::ChessBoard()
{
int i;
m_hWnd=FindWindow(NULL,"chess");
dc=GetDC(m_hWnd);//获取窗口的dc
pen=CreatePen(1,1,RGB(255,255,255));//创建画笔
SelectObject(dc,pen);//建立关联
GetClientRect(m_hWnd,&rect);//获取客户区的大小
clientWidth=rect.right-rect.left;
clientHeight=rect.bottom-rect.top;
numberRow=(clientWidth-100)/30; //1009
numberCol=(clientHeight-10)/30; //488
//int id,ih;获取窗口区的大小
//RECT rectTest;
//GetWindowRect(m_hWnd,&rectTest);
//id=rectTest.right-rectTest.left;//1025
//ih=rectTest.bottom-rectTest.top;//526
//int isd,ish;获取整个屏幕的尺寸
//isd=GetSystemMetrics(SM_CXSCREEN);//1366
//ish=GetSystemMetrics(SM_CYSCREEN);//768
//开始画图
for(i=1;i<=numberCol;i++) //15行
{
MoveToEx(dc,100,30*i,NULL);//移到线条的起点
LineTo(dc,clientWidth-98,30*i);
}
for(i=0;i<numberRow-2;i++)//28列
{
MoveToEx(dc,100+30*i,30,NULL);
LineTo(dc,100+30*i,clientHeight-37);
}
}
CChess::~CChess()
{
ReleaseDC(m_hWnd,dc);
}
昨天下午大致写了一下,今天有花一个小时的时间进行了优化,画布总于搞定。下面将进行如何获取坐标画棋子。