Click here to Skip to main content
15,395,897 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
My program use WINAPI(C++),and use GDI and childwindow to create GUI.
the program can running normally,and seems nothing wrong,but when I tried to move the window outside the screen quickly to test the program,it crashed,all the gui turns white,and childwindows are not working,also I can't move this window(I use no frame window and send WM_NCLBUTTONDOWN, HTCAPTION message to move the window).
Two images there.
Image-Normal
Image-Crash

What I have tried:

In the spy++ detect,I can know that the program send WM_PAINT message again when it crashed,but I not know why cause this program.
Posted
Updated 23-Aug-21 3:00am
v2
Comments
Gerry Schmitz 11-Aug-21 21:11pm
   
Then don't paint when the window is being moved (quickly). Or try asynchronous calls.
EnderMo233 11-Aug-21 21:45pm
   
is it mainly is about the program move outside the screen and the window redraw frequently?
How can I solve it?
thank you.
Rick York 12-Aug-21 0:06am
   
One possibility I have seen often is trying to draw to an invalid window or device context. Be careful to make sure they are all valid when you paint with them.
EnderMo233 12-Aug-21 0:12am
   
I will upload the problem image to my website,then I will Improve question.wait.
EnderMo233 12-Aug-21 0:23am
   
okay,I have added images to the question

1 solution

all the gui turns white I'd say that's rather a sign of leaking of display context handles
See this code, each BeginPaint must be closed with EndPaint:
C++
    switch (uMsg)
    {
...
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
            //paint here
            EndPaint(hwnd, &ps);
        }


If between BeginPaint and EndPaint some exception occurs, the EndPaint might be skipped.
Also each GetDC/GetWindowDC call must be closed with ReleaseDC. If you use MFC all of that is handled automatically. You can implement your own scope guards, a class that calls EndPaint in the constructor.
Using SEH frames is also an option, it part of WinAPI and works for both, C and C++, see more info here: Structured Exception Handling (C/C++) | Microsoft Docs[^]
   
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900