/* Main window process messages callback function */ LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) { PAINTSTRUCT ps; HDC hdc; static RECT rt, boardRect, xAxisRect, yAxisRect, figureRect, pathLenRect; static Point fig1, fig2; static Point path[MAX_PATH_QUEUE]; static HBRUSH hBlackBrush, hWhiteBrush, hRedBrush; static HPEN hPen; static ChessFigure *chessFigures; static int nChessFigures, currentChessFigure = 0 , pathLen = -1; int mX, mY; switch (message) { case WM_CREATE: /* Initializing chess figures */ nChessFigures = CreateChessFigures(&chessFigures); /* Initializing board data */ srand((unsigned)time(NULL)); fig1.x = (int)((double)rand() / RAND_MAX * BOARD_X); fig1.y = (int)((double)rand() / RAND_MAX * BOARD_Y); fig2.x = (int)((double)rand() / RAND_MAX * BOARD_X); fig2.y = (int)((double)rand() / RAND_MAX * BOARD_Y); pathLen = SearchTurns(path, MAX_PATH_QUEUE, &fig1, &fig2, &chessFigures[currentChessFigure]); /* Creating instruments */ hBlackBrush = CreateSolidBrush(RGB(0x00, 0x00, 0x00)); hWhiteBrush = CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF)); hRedBrush = CreateSolidBrush(RGB(0xFF, 0x00, 0x00)); hPen = CreatePen(PS_SOLID, 4, RGB(0xFF, 0x10, 0x10)); break; case WM_SIZE: break; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); /* Calculating render rectangles */ GetClientRect(hWnd, &rt); /* Board rectangle */ boardRect.left = rt.left + AXIS_WIDTH; boardRect.right = rt.right; boardRect.top = rt.top + CAPTION_HEIGHT; boardRect.bottom = rt.bottom - AXIS_HEIGHT; /* Correcting aspect */ if ((double)(boardRect.right - boardRect.left) / BOARD_X > (double)(boardRect.bottom - boardRect.top) / BOARD_Y) { boardRect.right = (long)(boardRect.left + (double)(boardRect.bottom - boardRect.top) / BOARD_Y * BOARD_X); } else { boardRect.bottom = (long)(boardRect.top + (double)(boardRect.right - boardRect.left) / BOARD_X * BOARD_Y); } /* 'X' axis rectangle */ xAxisRect.left = boardRect.left; xAxisRect.right = boardRect.right; xAxisRect.top = boardRect.bottom; xAxisRect.bottom = boardRect.bottom + AXIS_HEIGHT; /* 'Y' axis rectangle */ yAxisRect.left = rt.left; yAxisRect.right = rt.left + AXIS_WIDTH; yAxisRect.top = boardRect.top; yAxisRect.bottom = boardRect.bottom; /* Figure name rectangle */ figureRect.left = rt.left + AXIS_WIDTH; figureRect.right = rt.left + AXIS_WIDTH + FIGURE_WIDTH; figureRect.top = rt.top; figureRect.bottom = rt.top + CAPTION_HEIGHT; /* Path length string rectangle */ pathLenRect.left = rt.left + FIGURE_WIDTH + AXIS_WIDTH; pathLenRect.right = rt.left + FIGURE_WIDTH + AXIS_WIDTH + PATH_WIDTH; pathLenRect.top = rt.top; pathLenRect.bottom = rt.top + CAPTION_HEIGHT; /* Drawing figure caption */ DrawFigureCaption(hdc, &figureRect, chessFigures[currentChessFigure].name); /* Drawing path length caption */ DrawPathLenCaption(hdc, &pathLenRect, pathLen); /* Drawing chessboard */ DrawChessboard(hdc, &boardRect, hBlackBrush, hWhiteBrush); /* Drawing axes */ DrawXAxis(hdc, &xAxisRect); DrawYAxis(hdc, &yAxisRect); /* Drawing path */ DrawPath(hdc, &boardRect, &fig1, path, pathLen, hPen, hRedBrush); /* Drawing figures labels */ DrawFigureName(hdc, &boardRect, &fig1, "Fig.1"); DrawFigureName(hdc, &boardRect, &fig2, "Fig.2"); EndPaint(hWnd, &ps); break; case WM_CHAR: /* Quiting on escape */ if (wParam == 27) SendMessage(hWnd, WM_DESTROY, 0, 0); break; case WM_LBUTTONUP: mX = LOWORD(lParam); mY = HIWORD(lParam); /* Changing first figure position */ if (_isInRect(&boardRect, mX, mY)) { fig1.x = _getXRectPart(&boardRect, BOARD_X, mX); fig1.y = _getYRectPart(&boardRect, BOARD_Y, mY); pathLen = SearchTurns(path, MAX_PATH_QUEUE, &fig1, &fig2, &chessFigures[currentChessFigure]); InvalidateRect(hWnd, &rt, TRUE); } /* Changing figure type (next) */ else if (_isInRect(&figureRect, mX, mY)) { currentChessFigure = (currentChessFigure + 1) % nChessFigures; pathLen = SearchTurns(path, MAX_PATH_QUEUE, &fig1, &fig2, &chessFigures[currentChessFigure]); InvalidateRect(hWnd, &rt, TRUE); } break; case WM_RBUTTONUP: mX = LOWORD(lParam); mY = HIWORD(lParam); if (_isInRect(&boardRect, mX, mY)) { /* Changing second figure position */ fig2.x = _getXRectPart(&boardRect, BOARD_X, mX); fig2.y = _getYRectPart(&boardRect, BOARD_Y, mY); pathLen = SearchTurns(path, MAX_PATH_QUEUE, &fig1, &fig2, &chessFigures[currentChessFigure]); InvalidateRect(hWnd, &rt, TRUE); } /* Changing figure type (previous) */ else if (_isInRect(&figureRect, mX, mY)) { currentChessFigure = (currentChessFigure + nChessFigures - 1) % nChessFigures; pathLen = SearchTurns(path, MAX_PATH_QUEUE, &fig1, &fig2, &chessFigures[currentChessFigure]); InvalidateRect(hWnd, &rt, TRUE); } break; case WM_DESTROY: /* Destroying created chess figures */ DestroyChessFigures(chessFigures, nChessFigures); /* Destroying created instruments */ DeleteObject(hBlackBrush); DeleteObject(hWhiteBrush); DeleteObject(hRedBrush); DeleteObject(hPen); /* Quiting */ PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } /* End of 'WndProc' function */
void CChessBorad::OnPaint() { CPaintDC dc(this); //加载棋盘位图 CBitmap bmp; bmp.LoadBitmap(IDB_CHESSBOARD); int nBmpWidth,nBmpHeight; //获取位图信息 BITMAP bmpInfo; bmp.GetBitmap(&bmpInfo); nBmpWidth = bmpInfo.bmWidth; nBmpHeight = bmpInfo.bmHeight; //将位图绘制在整个窗口区域 CRect cltRC; GetClientRect(cltRC); CDC memDC; memDC.CreateCompatibleDC(&dc); memDC.SelectObject(&bmp); dc.StretchBlt(0,0,cltRC.Width(),cltRC.Height(),&memDC,0,0,nBmpWidth,nBmpHeight,SRCCOPY); memDC.DeleteDC(); bmp.DeleteObject(); DrawChessboard(); CDC* pDC = GetDC(); CBitmap BmpWhite,BmpBlack,BmpBK; memDC.CreateCompatibleDC(pDC); BmpBlack.LoadBitmap(IDB_BLACK); BmpWhite.LoadBitmap(IDB_WHITE); BmpBK.LoadBitmap(IDB_BLANK); BmpBlack.GetBitmap(&bmpInfo); nBmpWidth = bmpInfo.bmWidth; nBmpHeight = bmpInfo.bmHeight; if (m_IsWin) { CPen pen(PS_SOLID,2,RGB(255,0,0)); pDC->SelectObject(&pen); pDC->MoveTo(m_NodeList[m_Startpt.x][m_Startpt.y].m_Point); pDC->LineTo(m_NodeList[m_Endpt.x][m_Endpt.y].m_Point); } int nPosX,nPosY; nPosX = 10*m_fRateX; nPosY = 10*m_fRateY; if (m_bBackPlay) //当前是否为游戏回放 { POSITION pos = NULL; for(pos = m_BackPlayList.GetHeadPosition(); pos != NULL;) { NODE* pNode = (NODE*)m_BackPlayList.GetNext(pos); if (pNode->m_IsUsed==TRUE) { int nPosX,nPosY; nPosX = 10*m_fRateX; nPosY = 10*m_fRateY; if (pNode->m_Color == ncWHITE) { memDC.SelectObject(&BmpWhite); pDC->StretchBlt(pNode->m_Point.x-nPosX,pNode->m_Point.y-nPosY,nBmpWidth,nBmpHeight, &memDC,0,0,nBmpWidth,nBmpHeight,SRCCOPY); } else if (pNode->m_Color == ncBLACK) { memDC.SelectObject(&BmpBlack); pDC->StretchBlt(pNode->m_Point.x-nPosX,pNode->m_Point.y-nPosY,nBmpWidth,nBmpHeight, &memDC,0,0,nBmpWidth,nBmpHeight,SRCCOPY); } else if (pNode->m_Color == ncUNKOWN) { memDC.SelectObject(&BmpBK); pDC->StretchBlt(pNode->m_Point.x-nPosX,pNode->m_Point.y-nPosY,nBmpWidth,nBmpHeight, &memDC,0,0,nBmpWidth,nBmpHeight,SRCCOPY); //绘制棋盘的局部表格 //首先获取中心点坐标 int nCenterX = pNode->m_Point.x ; int nCenterY = pNode->m_Point.y; CPoint topPT(nCenterX,nCenterY-nPosY); CPoint bottomPT(nCenterX,nCenterY+nPosY + 5); CPen pen(PS_SOLID,1,RGB(0,0,0)); pDC->SelectObject(&pen); pDC->MoveTo(topPT); pDC->LineTo(bottomPT); CPoint leftPT(nCenterX-nPosX,nCenterY); CPoint rightPT(nCenterX+nPosX + 10 ,nCenterY); pDC->MoveTo(leftPT); pDC->LineTo(rightPT); } } } } else //在棋盘中绘制棋子 { for (int m=0; m< m_nRowCount+1; m++) { for (int n=0; n<m_nColCount+1; n++) { if (m_NodeList[m][n].m_Color == ncWHITE) { memDC.SelectObject(&BmpWhite); pDC->StretchBlt(m_NodeList[m][n].m_Point.x-nPosX,m_NodeList[m][n].m_Point.y-nPosY,nBmpWidth,nBmpHeight, &memDC,0,0,nBmpWidth,nBmpHeight,SRCCOPY); } else if (m_NodeList[m][n].m_Color == ncBLACK) { memDC.SelectObject(&BmpBlack); pDC->StretchBlt(m_NodeList[m][n].m_Point.x-nPosX,m_NodeList[m][n].m_Point.y-nPosY,nBmpWidth,nBmpHeight, &memDC,0,0,nBmpWidth,nBmpHeight,SRCCOPY); } } } } BmpWhite.DeleteObject(); BmpBlack.DeleteObject(); BmpBK.DeleteObject(); }