Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you call <code>RestoreWindow()</code> to draw the bitmap onscreen, you are wiping out your <code>memoryDC</code> variable that you used to draw the pixels with. The bitmap is still selected into the original <code>HDC</code> that you have now lost, and a bitmap cannot be selected into multiple <code>HDC</code>s at the same time (the MSDN documentation for <code>SelectObject()</code> says as much). So you are not actually drawing the bitmap onscreen at all.</p> <p>There is no need to call <code>CreateCompatibleDC()</code> or <code>SelectObject()</code> inside of <code>RestoreWindow()</code> because you already have the bitmap and memory <code>HDC</code> set up inside of <code>StoreWindow()</code>, so they use them as-is instead.</p> <p>Try this:</p> <pre><code>HDC memoryDC = NULL; HBITMAP memoryBitmap = NULL; int width = 0, height = 0; void CheckWidthHeight() { RECT clientRect; GetClientRect(hwnd, &amp;clientRect); width = clientRect.right - clientRect.left; height = clientRect.bottom - clientRect.top; } void StoreWindow() { HDC hDC = GetDC(hwnd); memoryDC = CreateCompatibleDC(hDC); memoryBitmap = CreateCompatibleBitmap(hDC, width, height); SelectObject(memoryDC, memoryBitmap); BitBlt(memoryDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY); ReleaseDC(hwnd, hDC); } void RestoreWindow() { PAINTSTRUCT ps; HDC hDC = BeginPaint(hwnd, &amp;ps); if (memoryDC) BitBlt(hDC, 0, 0, width, height, memoryDC, 0, 0, SRCCOPY); EndPaint(hwnd, &amp;ps); } ... case WM_SIZE: { CheckWidthHeight(); break; } case WM_LBUTTONDOWN: { if (!memoryDC) StoreWindow(); if (memoryDC) { SetPixel(memoryDC, LOWORD(lParam), HIWORD(lParam), RGB(0,0,0)); RECT rect; rect.left = LOWORD(lParam); rect.top = HIWORD(lParam); rect.right = rect.left + 1; rect.bottom = rect.top + 1; InvalidateRect(hwnd, &amp;rect, TRUE); } break; } case WM_PAINT: { RestoreWindow(); break; } ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload