-
3 windows, cpu 100%
Hello Everyone
When I originally created three windows I did not realize that they were making my cpu run at 100% while my program was running. So I had been happily programming away but when I created even more windows with same child child class , my system is sticking almost freezing. So I tracked back through my versions to realize that the child windows must be the cause of the problem. I'm only creating the child windows once in the WM_CREATE part of winproc. I must be missing something obvious.Thanks for any replies.
phil
I'm using c
win2k
dev c++
Code:
/*------------------------------------------------------------
V3_6
------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildWndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT CALLBACK ChildChildWndProc (HWND, UINT, WPARAM, LPARAM) ;
HWND hwndChildChild; // handle for child of child window
HWND hwndChild; // handle for child window
TCHAR szChildClass[] = TEXT("Main_Child");
TCHAR szChildChildClass[] = TEXT("Child_Child");
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
BOOL bRet; // returm of getmessage
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
// Update winclass for main_child window .START
// Then register class .START
wndclass.lpfnWndProc = ChildWndProc;
wndclass.hIcon = NULL;
wndclass.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH) ;
wndclass.lpszClassName = szChildClass;
RegisterClass(&wndclass);
// Update winclass for main_child window .FINISH
// Then register class .FINISH
// Update winclass for child_child window .START
// Then register class .START
wndclass.lpfnWndProc = ChildChildWndProc;
wndclass.hIcon = NULL;
wndclass.hbrBackground = (HBRUSH) GetStockObject (GRAY_BRUSH) ;
wndclass.lpszClassName = szChildChildClass;
RegisterClass(&wndclass);
// Update winclass for child_child window .FINISH
// Then register class .FINISH
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Win Program"), // window caption
WS_OVERLAPPEDWINDOW |
WS_CLIPCHILDREN , // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while ((bRet = GetMessage (&msg, NULL, 0, 0))!=0)
{
if (bRet == -1)
{
MessageBox (NULL, TEXT ("Get message, error!"),
szAppName, MB_ICONERROR) ;
// handle the error and possibly exit
}
else
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int result = 5;
int iLength;
TCHAR szBuffer[100];
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
// Create child window .START
hwndChild = CreateWindow(szChildClass, NULL,
WS_CHILDWINDOW | WS_VSCROLL |
WS_CLIPSIBLINGS ,
40,40,360,420,
hwnd, (HMENU) 1001,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
ShowWindow (hwndChild, SW_SHOW);
hwndChildChild = CreateWindow(szChildChildClass, NULL,
WS_CHILDWINDOW | WS_VISIBLE |
WS_CLIPSIBLINGS ,
0,0,20,20,
hwndChild, (HMENU) 1002,
(HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE),
NULL);
// Create child window .FINISH
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_SIZE:
return 0;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
return 0;
case WM_SIZE:
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
LRESULT CALLBACK ChildChildWndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
return 0;
case WM_PAINT:
return 0;
case WM_SIZE:
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
-
in your while statment add Sleep(1);
-
Your process is spinning on WM_PAINT because the update region is never cleared for either of the child windows.
The WM_PAINT Message (second to last paragraph)
You can use this as your "template" WM_PAINT handler:
Code:
case WM_PAINT:
ValidateRect(hwnd, 0); /* clear/validate update region */
return 0;
gg
-
Thank you for the 2 replies, I tried the sleep( ) first, in the while statement and that stopped the cpu running at 100%. Why does this work?
Then I tried ValidateRect() in WM_PAINT. When I put ValidateRect() in both child winprocs that also worked.
Now I understand. :)
Code:
case WM_PAINT
return 0; // will loop for ever
Thanks
phil