windows does manage wm_paint differently. multiple wm_paint messages in a row are treated as one, the others are removed.
Main Topics
Browse All TopicsHow does it work?
I find if I put a breakpoint in the WM_PAINT case, the breakpoint is hit constantly.
This means a WM_PAINT message keeps being posted, even although there seems to be no reason for painting the screen? (the window doesn't change in appearance, even with the program stopped at the breakpoint in the WM_PAINT, so not all of these paint messages are required.
Is there any way to reduce the number of redundant paint messages being posted to the message loop?
Thanks,
-Sandra
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>Try to call OutputDebugString instead of a breakpoint, and see what happens.
Ok, I will.
> windows does manage wm_paint differently. multiple wm_paint messages in a row are treated as one, the others are removed.
That's interesting to know. I always learn so many things when I post in this forum. C++ programmers by nature have to be very close to the underlying computer and often understand thousands of valuable low-level technical details.
-Sandra
I added a OutputDebugString() in the WM_PAINT and printed an integer that I incremented every time. I timed it, in 5 seconds there were 640 WM_PAINT messages received by my application. Conservatively that's a paint message every 10 milliseconds. Painting is a fairly expensive operation in this control, I'd like to reduce that if I could. The monitor doesn't even refresh as fast as it's trying to repaint, I could show a movie at over 100 frames / sec if I could blt them that fast in the paint method.
-Sandra
> Any operations in your code that call "InvalidateRect()" or similar functions?
Nothing like that. Here's the entire code (it's a little messy):
#include "stdafx.h"
#include "TestLargeBG.h"
#define KEY_SCANCODE(lParam) (((lParam) & 0x00FF0000)>>16)
#define KEY_REPEAT(lParam) (int)((lParam) & 0xFFFF)
// Global Variables:
HINSTANCE hInst; // current instance
MSG msg;
HBITMAP bgImg;
HGDIOBJ hOldImg;
HDC hDC, hMemDC;
int width = 1000;
int height = 800;
int x = 0;
int y = 0;
int nPaints = 0;
char temp[100];
const TCHAR * szWindowClass = "TestLargeBG"; // the main window class name
// Forward declarations of functions included in this code module:
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HBITMAP ImageFromFile(HINSTANCE hInstance, LPCTSTR imgPath)
{
return (HBITMAP)LoadImage(hInstan
}
void Cleanup(HWND hWnd)
{
ReleaseDC(hWnd, hDC);
SelectObject(hMemDC, hOldImg);
DeleteDC(hMemDC);
}
inline void Paint()
{
BitBlt(hDC, 0, 0, width, height, hMemDC, x, y, SRCCOPY);
}
void KeyDown(WPARAM wParam, LPARAM lParam)
{
int i, repeat = KEY_REPEAT(lParam);
OutputDebugString("Key Down\n");
switch(wParam)
{
case 'w':
for(i=0; i < repeat; i++)
{
y -= 4;
Paint();
}
break;
case 'a':
for(i=0; i < repeat; i++)
{
x -= 4;
Paint();
}
break;
case 'd':
for(i=0; i < repeat; i++)
{
x += 4;
Paint();
}
break;
case 's':
for(i=0; i < repeat; i++)
{
y += 4;
Paint();
}
break;
case 27: // ESC
PostQuitMessage(0);
break;
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// Register the window class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
// Load the background image
bgImg = ImageFromFile(hInstance, "C:\\TestLargeBG.bmp");
if(bgImg == NULL)
return FALSE;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
hDC = GetDC(hWnd);
hMemDC = CreateCompatibleDC(hDC);
hOldImg = SelectObject(hMemDC, bgImg);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_KEYDOWN:
TranslateMessage(&msg);
break;
case WM_CHAR:
KeyDown(wParam, lParam);
break;
case WM_PAINT:
// Count the number of paint ops
itoa(nPaints++,temp,10);
temp[strlen(temp)] = '\n';
temp[strlen(temp)] = 0;
OutputDebugString((LPCSTR)
Paint();
break;
case WM_ERASEBKGND:
return (LRESULT)1; // Do nothing, but tell windows we handled this event
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_QUIT:
Cleanup(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
Handling WM_PAINT message we must always get window DC for painting and not to keep persistent DC as you do. Changing of the program by this way fixes the problem. Hopefully, you can continue from this point.
#include "stdafx.h"
//#include "TestLargeBG.h"
#include <stdlib.h>
#define KEY_SCANCODE(lParam) (((lParam) & 0x00FF0000)>>16)
#define KEY_REPEAT(lParam) (int)((lParam) & 0xFFFF)
// Global Variables:
HINSTANCE hInst; // current instance
MSG msg;
HBITMAP bgImg;
HGDIOBJ hOldImg;
//HDC hDC, hMemDC; // Don't use persistent HDC !!!
HDC hMemDC;
int width = 1000;
int height = 800;
int x = 0;
int y = 0;
int nPaints = 0;
char temp[100];
const TCHAR * szWindowClass = "TestLargeBG"; // the main window class name
// Forward declarations of functions included in this code module:
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HBITMAP ImageFromFile(HINSTANCE hInstance, LPCTSTR imgPath)
{
return (HBITMAP)LoadImage(hInstan
}
void Cleanup(HWND hWnd)
{
// ReleaseDC(hWnd, hDC);
SelectObject(hMemDC, hOldImg);
DeleteDC(hMemDC);
}
inline void Paint(HDC hDC)
{
BitBlt(hDC, 0, 0, width, height, hMemDC, x, y, SRCCOPY);
}
void KeyDown(WPARAM wParam, LPARAM lParam, HWND hWnd)
{
int i, repeat = KEY_REPEAT(lParam);
OutputDebugString("Key Down\n");
switch(wParam)
{
case 'w':
for(i=0; i < repeat; i++)
{
y -= 4;
HDC hdc = GetDC(hWnd);
Paint(hdc);
ReleaseDC(hWnd, hdc);
}
break;
case 'a':
for(i=0; i < repeat; i++)
{
x -= 4;
HDC hdc = GetDC(hWnd);
Paint(hdc);
ReleaseDC(hWnd, hdc);
}
UpdateWindow(hWnd);
break;
case 'd':
for(i=0; i < repeat; i++)
{
x += 4;
HDC hdc = GetDC(hWnd);
Paint(hdc);
ReleaseDC(hWnd, hdc);
}
UpdateWindow(hWnd);
break;
case 's':
for(i=0; i < repeat; i++)
{
y += 4;
HDC hdc = GetDC(hWnd);
Paint(hdc);
ReleaseDC(hWnd, hdc);
}
break;
UpdateWindow(hWnd);
case 27: // ESC
PostQuitMessage(0);
break;
}
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// Register the window class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
// Load the background image
bgImg = ImageFromFile(hInstance, "C:\\TestLargeBG.bmp");
if(bgImg == NULL)
return FALSE;
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
return FALSE;
HDC hDC = GetDC(hWnd);
hMemDC = CreateCompatibleDC(hDC);
hOldImg = SelectObject(hMemDC, bgImg);
ReleaseDC(hWnd, hDC);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_KEYDOWN:
TranslateMessage(&msg);
break;
case WM_CHAR:
KeyDown(wParam, lParam, hWnd);
break;
case WM_PAINT:
// Count the number of paint ops
itoa(nPaints++,temp,10);
temp[strlen(temp)] = '\n';
temp[strlen(temp)] = 0;
OutputDebugString((LPCSTR)
hdc = BeginPaint(hWnd, &ps);
Paint(hdc);
EndPaint(hWnd, &ps);
break;
case WM_ERASEBKGND:
return (LRESULT)1; // Do nothing, but tell windows we handled this event
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_QUIT:
Cleanup(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
After reading your second post about keyboard handling I want to suggest you to exclude repeat counting and simplify keyboard handling code:
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
...
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CHAR:
KeyDown(wParam, lParam, hWnd);
break;
case WM_PAINT:
...
void KeyDown(WPARAM wParam, LPARAM lParam, HWND hWnd)
{
OutputDebugString("Key Down\n");
switch(wParam)
{
case 'w':
{
y -= 4;
HDC hdc = GetDC(hWnd);
Paint(hdc);
ReleaseDC(hWnd, hdc);
}
break;
...
Oh, I thought that if you pass the CS_OWNDC that you have a dedicated DC and don't need to get it from the pool and return it each time you want to paint.
That was silly of me to assume that because it appeared to work that it was working, the two are often different animals.
Thanks, Alex it works perfectly now, and no erroneous paint messages are sent.
Why the UpdateWindow() call though? Paint() is sufficient isn't it?
You not only solve the problem, but you fixed my code for me! Well beyond what I was expecting, thanks Alex. A+.
-Sandra
Business Accounts
Answer for Membership
by: cwwkiePosted on 2005-08-27 at 17:40:45ID: 14769532
I know the wm_paint is at least sent when the window needs repainting. If you put a breakpoint on that, and your debugger get focussed on top of the program you are debugging, it must be repainted again after you continue. Try to call OutputDebugString instead of a breakpoint, and see what happens.