按C键复制图片到剪贴板。按Z键悔棋。默认是左图圆棋子。很容易改成方形棋子。
https://files.cnblogs.com/files/blogs/714801/BrdTool.zip 19KB 含源码和.exe
有效的代码130行左右:
enum { W = 354, H = 392, S = 39 }; // 棋盘与棋子大小 struct CBmpInDC : public CDC { // 本想继承CDC和CBitmap,但CDC和CBitmap没有virtual public CGdiObject CBitmap m_bmp; HGDIOBJ m_oldbmp; CBmpInDC() : m_oldbmp(0) {} ~CBmpInDC() { if (m_hDC && m_oldbmp) SelectObject(m_oldbmp); } void Create() { CreateCompatibleDC(ScreenDC()); } void CreateBmp(int w, int h) { m_bmp.CreateCompatibleBitmap(ScreenDC(), w, h); m_oldbmp = SelectObject(m_bmp); } void AttachBmp(PVOID hbmp) { m_bmp.Attach(hbmp); m_oldbmp = SelectObject(m_bmp); } PVOID DetachBmp() { return m_bmp.Detach(); } // 之后别忘了CreateBmp或AttachBmp static CDC* ScreenDC() { return CDC::FromHandle(GetDC(0)); } }; CBmpInDC bkgnd, pic; CFont fnt; char pos_stack[256][10][9]; // position(局面)们 int nPos; #define pos pos_stack[nPos] CPoint sel(-1, 0); // 选中棋子的位置 int fen_atoi(int c) { switch (toupper(c)) { case 'K': return 0; case 'A': return 1; case 'B': return 2; case 'N': return 3; case 'R': return 4; case 'C': return 5; case 'P': return 6; default: abort(); } } BOOL CBrdToolDlg::OnInitDialog() { CDialog::OnInitDialog(); m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE); CBitmap bmp; bmp.LoadBitmap(IDB_BRD); bkgnd.Create(); bkgnd.AttachBmp(bmp.Detach()); pic.Create(); pic.CreateBmp(W, H); fnt.CreatePointFont(168, "黑体"); const char* s = "rnbakabnr/9/1c5c1/p1p1p1p1p/9/9/P1P1P1P1P/1C5C1/9/RNBAKABNR"; for (int y = 0, x = 0; *s && *s != ' '; ++s) { const char c = *s; if (c == '/') (x = 0, ++y); else if (c >= '1' && c <= '9') x += c - '0'; else pos[y][x++] = char((c >= 'a' ? 8 : 1) + fen_atoi(c)); } return TRUE; // return TRUE unless you set the focus to a control } void CBrdToolDlg::SetCaption() { char s[80]; sprintf(s, "黑白棋盘 c=复制图片 z=悔棋 %d", nPos); SetWindowText(s); } void CBrdToolDlg::OnLButtonDown(UINT nFlags, CPoint pt) { pt.x /= S; if (pt.x < 0 || pt.x >= 9) pt.x = -1; pt.y /= S; if (pt.y < 0 || pt.y >= 10) pt.y = pt.x = -1; if (sel.x < 0) { if (pos[pt.y][pt.x]) sel = pt; // 第一次选择棋子,空白处不能选 } else { if (pt.x < 0) sel = pt; // 点空步处取消选择 else if (sel != pt) // 不能吃自己 { memcpy(pos_stack[nPos + 1], pos_stack[nPos], sizeof(pos_stack[0])); ++nPos; pos[pt.y][pt.x] = pos[sel.y][sel.x]; pos[sel.y][sel.x] = 0; SetCaption(); } sel = CPoint(-1, 0); // 回到没有棋子被选中 } InvalidateRect(NULL, FALSE); UpdateWindow(); } void CBrdToolDlg::OnPaint() { BitBlt(pic, 0, 0, W, H, bkgnd, 0, 0, SRCCOPY); CPen pen(PS_INSIDEFRAME, 1, RGB(192, 192, 192)); PVOID oldbrush = 0, oldpen = pic.SelectObject(pen), oldfnt = pic.SelectObject(fnt); pic.SetBkMode(TRANSPARENT); for (int y = 0; y < 10; y++) for (int x = 0; x < 9; x++) { int zi = pos[y][x]; if (!zi) continue; if (zi < 8) { // 红棋 oldbrush = pic.SelectObject(GetStockObject(WHITE_BRUSH)); pic.SetTextColor(RGB(0, 0, 0)); } else { oldbrush = pic.SelectObject(GetStockObject(BLACK_BRUSH)); pic.SetTextColor(RGB(255, 255, 255)); } if (x == sel.x && y == sel.y) pic.SetTextColor(RGB(0, 168, 0)); static const char* Z = "\0\0\0帅\0仕\0相\0马\0车\0炮\0兵\0將\0士\0象\0馬\0車\0砲\0卒"; CRect rc(CPoint(1 + x * S, 1 + y * S), CSize(S, S)); rc.InflateRect(-3, -3); pic.Ellipse(rc); // pic.Rectangle(rc); pic.DrawText(&Z[zi * 3], rc, DT_SINGLELINE | DT_VCENTER | DT_CENTER); } pic.SelectObject(oldbrush); pic.SelectObject(oldfnt); pic.SelectObject(oldpen); CPaintDC dc(this); BitBlt(dc, 0, 0, W, H, pic, 0, 0, SRCCOPY); } BOOL CBrdToolDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_CHAR && pMsg->wParam == 'c') { OpenClipboard(); EmptyClipboard(); SetClipboardData(CF_BITMAP, pic.DetachBmp()); CloseClipboard(); pic.CreateBmp(W, H); } else if (pMsg->message == WM_CHAR && pMsg->wParam == 'z' && nPos) { --nPos; InvalidateRect(NULL, FALSE); UpdateWindow(); SetCaption(); } return CDialog::PreTranslateMessage(pMsg); } BOOL CBrdToolDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { static HCURSOR hand = LoadCursor(NULL, MAKEINTRESOURCE(32649)); // IDC_HAND SetCursor(hand); return TRUE; }
标签:sel,return,pt,SelectObject,pic,黑白,pos,工具,棋盘 From: https://www.cnblogs.com/funwithwords/p/16976902.html