HINSTANCE g_hInst = NULL;HANDLE g_hStdout = NULL; //控制台CHAR szText[256] = { 0 }; //调试用#define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), 0, 0);int i = 1;//获得坐标int xPos = 0;int yPos = 0;void DrawBk(HDC hDC){int marc = 0; //控制棋盘所在位置for (int i = 0; i < 10; i++) //画九格横线{MoveToEx(hDC, marc + 50, marc + 50 + i * 48, NULL);LineTo(hDC, marc + 480, marc + 50 + i * 48);}for (int j = 0; j < 10; j++) //画九格直线{MoveToEx(hDC, marc + 50 + j * 48, marc + 50, NULL);LineTo(hDC, marc + 50 + j * 48, marc + 480);}//背景图处理HBITMAP hBitMap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BKMP1));HDC hBmap = CreateCompatibleDC(hDC);HGDIOBJ hOldBmp = SelectObject(hBmap, hBitMap);//绘图开始for (int i = 0; i < 9; i++){for (int j = 0; j < 9; j++){BitBlt(hDC, 50 + j * 48, 50 + i * 48, 48, 48, hBmap, 0, 0, MERGECOPY);}}SelectObject(hDC, hOldBmp);DeleteDC(hBmap);DeleteObject(hBitMap);}void OnLButton(HDC hDC){//PAINTSTRUCT ps = { 0 };//HDC hDC = BeginPaint(hWnd, &ps);int marc = 0; //控制棋盘所在位置if (xPos>50 && yPos >50){//背景图处理HBITMAP hBitMap = LoadBitmap(g_hInst, MAKEINTRESOURCE(IDB_BKMP2));HDC hBmap = CreateCompatibleDC(hDC);HGDIOBJ hOldBmp = SelectObject(hBmap, hBitMap);//绘图开始int xPoint = (xPos - 50) / 48;int yPoint = (yPos - 50) / 48;sprintf_s(szText, 256, "OnLButton,x=%d,y=%d\n", xPoint, yPoint);PrintLog(szText);BitBlt(hDC, 50 + xPoint * 48, 50 + yPoint * 48, 48, 48, hBmap, 0, 0, SRCCOPY);SelectObject(hDC, hOldBmp);DeleteDC(hBmap);DeleteObject(hBitMap);}}//OnPaintvoid OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam){PAINTSTRUCT ps = { 0 };HDC hDC = BeginPaint(hWnd, &ps);if (i == 1) //暂时屏蔽棋盘每次刷新的办法,后面可以用控件解决{DrawBk(hDC);i = 2;}OnLButton(hDC);EndPaint(hWnd, &ps);}
原文:http://www.cnblogs.com/nfking/p/5573147.html