0

how can i draw square net ( like chess) in SDI MFC ? and how to determine the position for putting some more shape in specific position ? i have to use (Moveto) and (Lineto) and draw them 1 by 1 ? or using bitmap ? or easier way ? i tried in this way but it's not really smart. thank you.

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);
Emad mohseny
  • 13
  • 1
  • 6
  • Draw into a memory DC and then blit it to the screen. – Andrew Truckle Jun 22 '16 at 14:07
  • can you give me more detail ? – Emad mohseny Jun 23 '16 at 11:00
  • Have a look here: https://msdn.microsoft.com/en-gb/library/windows/hardware/cc308997(v=vs.90).aspx. You use the **CMemDC** class. This is good if you want to reduce flickering. – Andrew Truckle Jun 23 '16 at 12:08
  • @AndrewTruckle: All supported versions of Windows implement double-buffering already, as is. There hardly ever is a need to render to a memory device context. – IInspectable Jun 23 '16 at 12:14
  • @IInspectable I was not aware that MFC did these days. That is why I used the CMemDC and blip to screen automatically. – Andrew Truckle Jun 23 '16 at 12:16
  • 1
    @AndrewTruckle: This has nothing to do with MFC. MFC merely provides wrappers around standard Windows controls and windows. Those are double-buffered by default in the Desktop Window Manager (DWM). – IInspectable Jun 23 '16 at 12:26
  • @IInspectable But I have been taught that at times you may still do your painting in the background and then render it. Maybe not needed for this scenario. Thanks for clarification though. – Andrew Truckle Jun 23 '16 at 12:33

1 Answers1

1

You can draw solid rectangles by calling CDC::FillSolidRect. If your rectangles should contain a more complex pattern, use CDC::FillRect instead.

You can render a checkered board using the following pseudo-code:

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);
    }
}
IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • @Emadsharifmohseny: Since that is what you asked for, I don't see why you are surprised. Those are the ingredients for a checkered chess board. If that is not fancy enough, I also linked to an API that can fill rectangles with arbitrarily complex patterns. – IInspectable Jun 23 '16 at 11:09
  • @IInspectable I think he is asking how to construct the "Net" itself of "Squares". – Andrew Truckle Jun 23 '16 at 12:10
  • i am trying to make net , not just hatches – Emad mohseny Jun 23 '16 at 12:19
  • i think you didnt catch my meaning , btw thanks for help – Emad mohseny Jun 23 '16 at 14:47
  • @Emadsharifmohseny: I can only answer the question that was asked. If you want something *"like chess"*, and the answer that's offering something *"like chess"* doesn't address your question, then you have meant to ask a different question than you did. If you are looking for a grid, the `MoveTo` and `LineTo` are the calls to use. – IInspectable Jun 23 '16 at 14:53