How to keep real-time WM_PAINT in Win32 applications?

thebluetropics 1,046 Reputation points
2022-09-23T02:28:48.337+00:00

I have some problem with my application, I want to WM_PAINT is called everytime, that is, "real-time".

Here is my message loop code:

   while (true) {  
               if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {  
                   if (msg.message == WM_QUIT)  
                       break;  
                     
                   TranslateMessage(&msg);  
                   DispatchMessageW(&msg);  
               }  
     
               InvalidateRect(state.hwnd, NULL, FALSE); // this line should execute everytime, no matter what.  
           }  

This works perfectly, the WM_PAINT is called everytime. However, when I try to drag the window, the window stops receiving WM_PAINT!
It fully stops, until the mouse button is up. I don't want this behavior.

Any workaround to this?

Regards, @thebluetropics

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,440 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,553 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,861 Reputation points
    2022-09-23T08:27:20.9+00:00

    If it is in Direct2D, you just call InvalidateRect inside WM_PAINT;
    It will be refreshed at the screen refresh rate (60 FPS on my PC), even when you move the window :

    244097-direct2d-invalidaterect.gif

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 15,526 Reputation points Microsoft Vendor
    2022-09-23T06:12:21.753+00:00

    Hello @thebluetropics ,

    In InvalidateRect document

    The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function.

    The system sends a WM_PAINT message to a window whenever its update region is not empty and there are no other messages in the application queue for that window.

    If the bErase parameter is TRUE for any part of the update region, the background is erased in the entire region, not just in the specified part.

    InvalidateRect(state.hwnd, NULL, FALSE);  
     UpdateWindow(state.hwnd);  
    

    If you want to refresh the invalid area immediately, you can call the UpdateWindow after calling the InvalidateRect. If any part of the client area is invalid, the UpdateWindow will cause Windows to call the window process with the WM_PAINT message (if the entire client area is valid, the window process is not called). This WM_PAINT message does not enter the message queue and directly call the window process by Windows. After the window is refreshed, it will be exited immediately. Windows will control the statement to the statement after the UpdateWindow call in the program.

    Thank you.
    Junjie


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.