WM_PAINT

I have a small snippet of code in my WndProc that has a WM_PAINT with nothing in it except return(0). That causes my MessageBox not to appear unless I press ALT or F10 to get the MessageBox into an active state. It looks like the WM_PAINT message is called over and over preventing the MessageBox from appearing, even if no activity is going on. What is causing that?

I tried to post my code, but none of the tags '<>' or any of them are working, so I can't at the moment.

Sutton
The code tags are not working at the first post.
So copy the code as plaint text, submit it.
When you press the edit button and can mark your code and press the <> button.

Alternatively you can add the
[code]
&
[/code]
tags by hand.
Last edited on
OK. Thanks. See, the WM_PAINT code that is currently commented out. If uncommented, the About Box Message Box, never gets displayed until ALT or F10 is pressed. Why?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Test.cpp : Defines the entry point for the application.
//

#include "windows.h"
#include "commdlg.h"

#define IDM_ABOUT   1
#define IDM_EXIT    2
#define IDM_HELP    3
#define IDM_OPEN    4

WCHAR AppName[] = L"Analysis";
WCHAR AboutTitle[] = L"About Analysis";
WCHAR iconfilename[] = L"analysis.ico";

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);



int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
    WNDCLASS wndclass;
    HMENU hMenu, hMenuPopup;
    MSG msg;
    HWND hWnd;

    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = (HICON)LoadImage(NULL,iconfilename,IMAGE_ICON,0,0,LR_LOADFROMFILE|LR_DEFAULTSIZE|LR_SHARED);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wndclass.lpszMenuName = AppName;
    wndclass.lpszClassName = AppName;

    if (!RegisterClass(&wndclass))
    {
        MessageBox(NULL,L"Call to RegisterClass failed!",AppName,NULL);
        return(1);
    }

    hWnd = CreateWindow(AppName, AppName, WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 700, 400, NULL, NULL, hInstance, NULL);
    if (!hWnd)
    {
        MessageBox(NULL, L"Call to CreateWindow failed!", AppName, NULL);
        return(1);
    }

    hMenu = CreateMenu();
    hMenuPopup = CreateMenu();
    AppendMenu(hMenuPopup, MF_STRING, IDM_OPEN, L"Open");
    AppendMenu(hMenuPopup, MF_STRING, IDM_EXIT, L"Exit");
    AppendMenu(hMenu, MF_POPUP, UINT_PTR(hMenuPopup), L"File");

    hMenuPopup = CreateMenu();
    AppendMenu(hMenuPopup, MF_STRING, IDM_ABOUT, L"About Analysis");
    AppendMenu(hMenu, MF_POPUP, UINT_PTR(hMenuPopup), L"Help");

    SetMenu(hWnd, hMenu);

    ShowWindow(hWnd, iCmdShow);
    UpdateWindow(hWnd);

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}


//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    WCHAR text[80] = { 0 };

    switch (message)
    {
       case WM_COMMAND:
           // Parse the menu selections:

           switch (LOWORD(wParam))
           {
               case IDM_OPEN:
                   return(0);

               case IDM_EXIT:
                   SendMessage(hwnd, WM_CLOSE, 0, 0);
                   return(0);

               case IDM_ABOUT:
                   wsprintf(text, L"%s\n\u00a92021 Lookout Portable Security\nVersion 1.0", AppName);
                   MessageBox(hwnd,text,AboutTitle,MB_ICONINFORMATION|MB_OK);
                   return(0);
            }
            break;

        // case WM_PAINT:
            // return(0);

        case WM_DESTROY:
            PostQuitMessage(0);
            break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}
You never clear the need to repaint the window in WM_PAINT, the client area isn't validated, so Windows keeps sending WM_PAINT messages to your message queue.
https://www.codeproject.com/Articles/66250/BeginPaint-EndPaint-or-GetDC-ReleaseDC

Change your WM_PAINT case to something like this:
112
113
114
115
116
117
118
case WM_PAINT:
   HDC         hdc;
   PAINTSTRUCT ps;
   hdc = BeginPaint(hwnd, &ps);

   EndPaint(hwnd, &ps);
   return S_OK;

OR you can retain the case as an empty placeholder if you want to add code later and for now let DefWindowProc handle the message.
112
113
case WM_PAINT:
   break;

Commenting out the entire WM_PAINT case (or removing it entirely) sends all the paint messages to be processed by DefWindowProc, validating the client area.
Last edited on
Many thanks for this. That answers my question.
Topic archived. No new replies allowed.