Drawing in a window when resizing leaves Unpainted border

The problem I have seems trivial, but I cannot find a way to solve it. Here. I have a window with graphics.

For simplicity, we can say that this is a solid green rectangle that fills the entire client area of ​​the window. I want this rectangle to be redrawn and fill the whole window every time the window changes its size. I originally did this. I sent a WM_PAINT message from the WM_SIZE handler .

This works, but if I quickly move the mouse, I see a little unpainted (white) area around the green rectangle (in fact, only one or two sides, next to the one where the mouse is). My understanding of the problem is that the system thread that processes the user input (mouse) is faster than my WM_PAINT message handler. This means that by the time you start drawing the updated rectangle (its size is taken from WM_SIZE) the mouse is actually moving a little, and the system draws a new window frame that is different from what I'm trying to fill with green. This creates blank areas next to borders that move when resized.

When I stop resizing, the green color eventually fills the entire window, but when resizing, a little flicker occurs, occurring close to the borders, which is annoying. To solve the problem, I tried the following.

bool finishedPainting;
RECT windowRect;

case WM_PAINT :
    // .....  painting here

  finishedPainting = TRUE;
  break;

case WM_SIZE :
    // .... some actions

    // posting WM_PAINT
  InvalidateRect(hWnd, NULL, FALSE);
  PostMessage(hWnd, WM_PAINT, 0, 0);
  break;

case WM_SIZING :
    // this supposedly should prevent the system from passing
    // new window size to WM_SIZE
  if (!finishedPainting) memcpy((void*)lParam, &windowRect, sizeof(windowRect));
  else {
      // remember current window size for later use
    memcpy(&windowRect, (void*)lParam, sizeof(windowRect));
    finishedPainting = FALSE;
  }
  return TRUE;

He does not work. As a small change, I also tried this.

bool  finishedPainting;
POINT cursorPos;

case WM_PAINT :
    // .....  painting here

  finishedPainting = TRUE;
  break;

case WM_SIZE :
  if (!finishedPainting) SetCursorPos(cursorPos.x, cursorPos.y);
  else {
    finishedPainting = FALSE;
    GetCursorPos(&cursorPos);

      // .... some actions

    InvalidateRect(hWnd, NULL, FALSE);
    PostMessage(hWnd, WM_PAINT, 0, 0);
  }
  break;

This also does not work. As I understand it, the solution to the problem is to somehow slow down the mouse so that it moves to the next position on the screen (dragging the corner or side of the window with it) only after the picture is finished.

Any ideas how to achieve this? Or maybe there is something fundamentally wrong with the way I see the problem and the solution lies elsewhere?

// ================================================= ==================================================== =

Update

I did some experiments, and here is what I found

1) WM_SIZING - WM_NCPAINT - WM_SIZE - WM_PAINT. . , WM_SIZE WM_SIZING WM_NCPAINT

2) ( ). , , WM_SIZE, , WM_SIZING, , WM_NCPAINT WM_PAINT. , .

3) , , . , WM_NCPAINT WM_PAINT. , . ( WM_NCPAINT), WM_PAINT . , , . , , , , . , . , . . , . Windows : A) WM_NCPAINT , B) ( ), C) WM_PAINT . B - Windows , , , , , WM_PAINT.

, - . , , , Windows . WM_NCPAINT WM_PAINT, , , , , ( , ).

+2
3

WM_PAINT . , :: InvalidateRect Windows , WM_PAINT.

+4

Windows . , (.. ), .

WM_PAINT, , WM_ERASEBKGND , .

, , InvalidateRect RedrawWindow RDW_UPDATENOW.

: . WM_SIZING , / . WM_NCPAINT, , . , , , .

+2

WM_PAINT WM_SIZE. , , , RECT WM_SIZE, MoveWindow, , MoveWindow WM_PAINT .

, .

// in the WinMain function
if (GetMessage(&msg,NULL,NULL,0))
{
    TranslateMessage(&msg,NULL,NULL);
    DispatchMessage(&msg,NULL,NULL);
}
else
{
     // fill your window with the color in here
}

Or, of course, you can simply set the background color of your window to green, instead of doing the whole picture yourself:

// Before RegisterClass or RegisterClassEx
// wincl is a WNDCLASS or WNDCLASSEX
wincl.hbrBackground = CreateSolidBrush(RGB(50, 238, 50));
+1
source

All Articles