When WM_PAINT is not generated by invalidaterect, that is, by maximizing, minimizing, and so forth, or by moving (sometimes only generating WM_ERASEBKGND messages) The system sends WM_ERASEBKGND messages before sending WM_PAINT messages.
If the WM_ERASEBKGND message is processed when the False,beginpaint token is returned pt.ferase is true, the True,beginpaint token pt.ferase is False if the WM_ERASEBKGND is processed.
When WM_PAINT is generated by InvalidateRect, the WM_PAINT message is sent first (asynchronously), if InvalidateRect berase for True,beginpaint Check to the update area needs to delete the background, send a wm_ to the window The ERASEBKGND message, if the WM_ERASEBKGND message is processed, returns the False,beginpaint token pt.ferase True if True is returned when processing WM_ERASEBKGND, BeginPaint flag Pt.ferase is false.
If Pt.ferase is marked as true, indicates that the application should handle the background, but the application does not necessarily need to be processed, pt.ferase just as a token.
Added: DefWindowProc (hWnd, message, WParam, LParam) handles WM_ERASEBKGND messages by default with the following brush to clear the background
Wcex.hbrbackground = (hbrush) (color_window+1);
WM_ERASEBKGND returns True and returns false as a specification, generally with no difference, but when used, the subsequent processing is judged based on the function return value. It is therefore best to return data as required.
Finally, we will provide some test code, so that we can better understand the above content.
WM_ERASEBKGNDTest.cpp:Defines the entry point for the application.
//
#include "stdafx.h"
#include "Wm_erasebkgndtest.h"
#define MAX_LOADSTRING 100
Global Variables:
HINSTANCE HInst; Current instance
TCHAR sztitle[max_loadstring]; The title bar text
TCHAR szwindowclass[max_loadstring]; The main window class name
Forward declarations of functions included in this code module:
ATOM MyRegisterClass (hinstance hinstance);
BOOL InitInstance (hinstance, int);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About (HWND, UINT, WPARAM, LPARAM);
int Apientry _tWinMain (hinstance hinstance,
HInstance hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
Unreferenced_parameter (hprevinstance);
Unreferenced_parameter (lpCmdLine);
Todo:place code here.
MSG msg;
Haccel hacceltable;
Initialize Global Strings
LoadString (HInstance, Ids_app_title, SzTitle, max_loadstring);
LoadString (HInstance, Idc_wm_erasebkgndtest, Szwindowclass, max_loadstring);
MyRegisterClass (HINSTANCE);
Perform Application Initialization:
if (! InitInstance (HINSTANCE, nCmdShow))
{
return FALSE;
}
hacceltable = Loadaccelerators (hinstance, Makeintresource (idc_wm_erasebkgndtest));
Main message loop:
while (GetMessage (&msg, NULL, 0, 0))
{
if (! TranslateAccelerator (Msg.hwnd, hacceltable, &msg))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
}
return (int) Msg.wparam;
}
//
Function:myregisterclass ()
//
Purpose:registers the window class.
//
COMMENTS:
//
This function and its usage is only necessary if the want this Code
To being compatible with WIN32 systems prior to the ' RegisterClassEx '
function that is added to Windows 95. It is important to call the This function
So, the application would get ' well formed ' small icons associated
With it.
//
ATOM MyRegisterClass (hinstance hinstance)
{
Wndclassex Wcex;
wcex.cbsize = sizeof (wndclassex);
Wcex.style = Cs_hredraw | Cs_vredraw;
Wcex.lpfnwndproc = WndProc;
Wcex.cbclsextra = 0;
Wcex.cbwndextra = 0;
Wcex.hinstance = hinstance;
Wcex.hicon = LoadIcon (hinstance, Makeintresource (idi_wm_erasebkgndtest));
Wcex.hcursor = LoadCursor (NULL, Idc_arrow);
Wcex.hbrbackground = (hbrush) (color_window+1);
Wcex.hbrbackground = NULL;
Wcex.lpszmenuname = Makeintresource (idc_wm_erasebkgndtest);
Wcex.lpszclassname = Szwindowclass;
WCEX.HICONSM = LoadIcon (Wcex.hinstance, Makeintresource (Idi_small));
return RegisterClassEx (&WCEX);
}
//
Function:initinstance (HINSTANCE, int.)
//
Purpose:saves instance handle and creates main window
//
COMMENTS:
//
In this function, we save the instance handle in a global variable and
Create and display the main program window.
//
BOOL InitInstance (hinstance hinstance, int ncmdshow)
{
HWND hwnd;
HInst = hinstance; Store instance handle in our global variable
HWnd = CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow,
Cw_usedefault, 0, cw_usedefault, 0, NULL, NULL, HINSTANCE, NULL);
if (!hwnd)
{
return FALSE;
}
ShowWindow (HWnd, ncmdshow);
UpdateWindow (HWND);
return TRUE;
}
//
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)
{
int Wmid, wmevent;
Paintstruct PS;
HDC hdc;
switch (message)
{
case wm_command:
wmId = LoWord ( WParam);
wmevent = HiWord (WParam);
//Parse The menu selections:
switch (wmid)
{
case IDM_ About:
dialogbox (HInst, Makeintresource (Idd_aboutbox), hWnd, about);
break;
case idm_exit:
destroywindow (hWnd);
break;
default:
return defwindowproc (hWnd, message, WParam, lParam);
}
break;
case wm_lbuttondown:
rect RECT;
getclientrect (HWnd, &rect);
invalidaterect (hWnd, &rect, TRUE);
messagebeep (2);
break;
Case WM_ERASEBKGND:
OutputDebugString (_t ("Enter wm_erasebkgnd\n"));
return 1;
Break
Case WM_PAINT:
OutputDebugString (_t ("Enter wm_paint\n"));
HDC = BeginPaint (hWnd, &ps);
if (ps.ferase = = TRUE)
OutputDebugString (_t ("ps.ferase = = true\n\n"));
Todo:add any drawing code here ...
EndPaint (HWnd, &ps);
Break
Case Wm_destroy:
PostQuitMessage (0);
Break
Default
if (WM_ERASEBKGND = = message)
OutputDebugString (_t ("Enter wm_erasebkgnd\n"));
Return DefWindowProc (hWnd, message, WParam, LParam);
}
DefWindowProc (hWnd, message, WParam, LParam);
return 0;
}
Message handler for about box.
INT_PTR CALLBACK About (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM)
{
Unreferenced_parameter (LParam);
Switch (message)
{
Case WM_INITDIALOG:
Return (INT_PTR) TRUE;
Case WM_COMMAND:
if (LoWord (wParam) = = IDOK | | LoWord (wParam) = = IDCANCEL)
{
EndDialog (Hdlg, LoWord (WParam));
Return (INT_PTR) TRUE;
}
Break
}
Return (INT_PTR) FALSE;
}
http://www.articleswriting.net/article/3946748807/
WM_PAINT and WM_ERASEBKGND