In Wn32 programming, it is easy to cause high processor usage when responding to the WM_PAINT message. The cause of high processor load is that the window is always checking its own Update Region. If so, WM_PAINT will be issued until BeginPaint and EndPaint are called, and Update Region will not be empty. If BeginPaint is not added to the message function, WM_PAINT is continuously sent, resulting in an endless loop.
If you do not actively respond to WM_PAINT, the message enters DefWindowProc, which calls the two functions.
In MFC, the constructor of the CPaintDC (this) class will call BeginPaint, And the Destructor will call EndPaint. The OnPaint of the parent class may also appear. If you comment out the above, the cpu usage will remain high after running.
For example, in the following Win32 example, if we open the WM_PAINT processing part, the load will be too high. If you comment out the code, there is no problem. However, if you add BeginPaint and EndPaint to it, the CPU load will not be too high.
# Include <windows. h>
# Include <aygshell. h>
# Pragma comment (lib, "aygshell. lib ")
# Define IDC_MY_STATIC 100
# Define IDC_MY_BUTTON 101
# Define IDC_MY_EDIT 102
LRESULT WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
Int _ tWinMain (HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR lpsz1_line, int nShowCmd)
{
WNDCLASS wc = {0 };
HWND hWnd = NULL;
MSG msg = {0 };
DEBUGMSG (TRUE, (TEXT ("hInstance = % p, lpsz1_line = % s, nShowCmd = % d \ n"), hInstance, lpsz1_line, nShowCmd ));
// 1. register the window class (specify a function pointer)
Wc. style = CS_HREDRAW | CS_VREDRAW;
Wc. lpfnWndProc = WndProc;
Wc. cbClsExtra = 0;
Wc. cbWndExtra = 0;
Wc. hInstance = hInstance;
Wc. hIcon = NULL;
Wc. hCursor = NULL;
Wc. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH );
Wc. lpszMenuName = NULL;
Wc. lpszClassName = TEXT ("GWND ");
If (0 = RegisterClass (& wc ))
{
Return-1;
}
// 2. Create a window
HWnd = CreateWindow (TEXT ("GWND"), TEXT ("Palm"), WS_VISIBLE, CW_USEDEFAULT, NULL, NULL, hInstance, NULL );
If (! IsWindow (hWnd ))
{
Return-1;
}
// 3. display window
ShowWindow (hWnd, nShowCmd );
UpdateWindow (hWnd );
// 4. Message Loop
While (GetMessage (& msg, NULL, 0, 0 ))
{
TranslateMessage (& msg );
DispatchMessage (& msg );
}
Return msg. wParam;
}
LRESULT WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HINSTANCE hTemp = NULL;
SHINITDLGINFO shidi = {0 };
WORD wID = 0;
WORD wEvent = 0;
DEBUGMSG (TRUE, (TEXT ("hWnd = % p, uMsg = % 04X, wParam = % p, lParam = % p \ n"), hWnd, uMsg, wParam, lParam ));
Switch (uMsg)
{
Case WM_CREATE:
{
HTemp = GetModuleHandle (NULL );
DEBUGMSG (TRUE, (TEXT ("hTemp = % p \ n"), hTemp ));
CreateWindow (TEXT ("BUTTON"), TEXT ("OPEN"), WS_CHILD | WS_VISIBLE, 10,100, 30, 20, hWnd, (HMENU) IDC_MY_BUTTON, hTemp, NULL );
DEBUGMSG (TRUE, (TEXT ("WM_CREATE \ n ")));
Shidi. dwMask = SHIDIM_FLAGS;
Shidi. hDlg = hWnd;
Shidi. dwFlags = SHIDIF_CANCELBUTTON | SHIDIF_SIZEDLGFULLSCREEN;
SHInitDialog (& shidi );
}
Break;
// Case WM_PAINT:
//{
// DEBUGMSG (TRUE, (TEXT ("WM_PAINT \ n ")));
//}
// Break;
Case WM_COMMAND:
{
DEBUGMSG (TRUE, (TEXT ("WM_COMMAND \ n ")));
WID = LOWORD (wParam );
WEvent = HIWORD (wParam );
Switch (wID)
{
Case IDCANCEL:
{
DestroyWindow (hWnd );
}
Break;
Case IDC_MY_BUTTON:
{
MessageBox (hWnd, TEXT ("HI"), TEXT ("Tip"), MB_ OK );
}
Break;
}
}
Break;
Case WM_DESTROY:
{
DEBUGMSG (TRUE, (TEXT ("WM_DESTROY \ n ")));
PostQuitMessage (0 );
}
Break;
Case WM_KEYDOWN:
{
DEBUGMSG (TRUE, (TEXT ("WM_KEYDOWN \ n ")));
}
Break;
Case WM_KEYUP:
{
DEBUGMSG (TRUE, (TEXT ("WM_KEYUP \ n ")));
}
Break;
Case WM_CHAR:
{
DEBUGMSG (TRUE, (TEXT ("WM_CHAR \ n ")));
}
Break;
Case WM_LBUTTONDOWN:
{
DEBUGMSG (TRUE, (TEXT ("WM_LBUTTONDOWN \ n ")));
}
Break;
Case WM_LBUTTONUP:
{
DEBUGMSG (TRUE, (TEXT ("WM_LBUTTONUP \ n ")));
}
Break;
Case WM_INITDIALOG:
{
DEBUGMSG (TRUE, (TEXT ("WM_INITDIALOG \ n ")));
}
Break;
Case WM_SYSKEYDOWN:
{
DEBUGMSG (TRUE, (TEXT ("WM_SYSKEYDOWN \ n ")));
}
Break;
Default:
Return DefWindowProc (hWnd, uMsg, wParam, lParam );
}
Return 0;
}