SDI MFC中绘制方网

drawing square net in SDI MFC

如何在SDI MFC中画方格网(象棋)? 以及如何确定在特定位置放置更多形状的位置? 我必须使用 (Moveto) 和 (Lineto) 并将它们 1 乘 1 地画出来? 或使用位图?或更简单的方法? 我以这种方式尝试过,但它并不是很聪明。 谢谢。

COLORREF blueline = RGB(255, 0, 0);
    pen1.CreatePen(PS_SOLID, 3, blueline);
    pDC->SelectObject(&pen1);
    pDC->MoveTo(0,80);
    pDC->LineTo(1024, 80);
    pDC->SelectObject(&pen1);

您可以通过调用 CDC::FillSolidRect. If your rectangles should contain a more complex pattern, use CDC::FillRect 来绘制实心矩形。

您可以使用以下伪代码渲染棋盘:

for (int x = 0; x < 8; ++x) {
    for (int y = 0; y < 8; ++y ) {
        // Calculate square position and size
        int x0 = x_origin + x * square_width;
        int x1 = x_origin + (x + 1) * square_width;
        int y0 = y_origin + y * square_height;
        int y1 = y_origin + (y + 1) * square_height;
        RECT r = {x0, y0, x1, y1};
        // Pick alternating color
        COLORREF color = (x + y) & 1 ? RGB(0, 0, 0) : RGB(255, 255, 255);
        // Render square
        pDC->FillSolidRect(&r, color);
    }
}