I've been injured my eyes trying to find the logical bug (since it is
not syntax or any other error that could VC report), so I'm asking you
if you could help me to find the bug.
Program is retyped from book and unfortunately and don't have the
original source code to compare it with.
Program should create main window and in it the other four child
windows and do some display in each of them except the main window.
But when I try to run the program, it only display the main window and
the client area, without child windows in it!
Just to say one more time, VC does not report any error or warning, so
it must be the logical bug in code.
Here's the source (I apologize for text length in mail):
#include <windows.h><br />
#include <math.h><br />
<br />
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);<br />
<br />
int cyChar;<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, <br />
PSTR szCmdLine, int iCmdShow)<br />
{<br />
static TCHAR szAppName[] = TEXT("Multi1");<br />
HWND hwnd;<br />
MSG msg;<br />
WNDCLASS wc;<br />
<br />
wc.style = CS_HREDRAW | CS_VREDRAW;<br />
wc.lpfnWndProc = WndProc;<br />
wc.cbClsExtra = 0;<br />
wc.cbWndExtra = 0;<br />
wc.hInstance = hInstance;<br />
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);<br />
wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br />
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
wc.lpszMenuName = NULL;<br />
wc.lpszClassName = szAppName;<br />
<br />
if (!RegisterClass(&wc))<br />
{<br />
MessageBox(NULL, TEXT("This Program Requires Windows NT!"),<br />
szAppName, MB_ICONERROR);<br />
return 0;<br />
}<br />
<br />
hwnd = CreateWindow(szAppName, TEXT("Multitasking Demo"),<br />
WS_OVERLAPPEDWINDOW,<br />
CW_USEDEFAULT, CW_USEDEFAULT,<br />
CW_USEDEFAULT, CW_USEDEFAULT,<br />
NULL, NULL, hInstance, NULL);<br />
<br />
ShowWindow(hwnd, iCmdShow);<br />
UpdateWindow(hwnd);<br />
<br />
while (GetMessage(&msg, NULL, 0, 0))<br />
{<br />
TranslateMessage(&msg);<br />
DispatchMessage(&msg);<br />
}<br />
return msg.wParam;<br />
}<br />
<br />
int CheckBottom(HWND hwnd, int cyClient, int iLine)<br />
{<br />
if (iLine * cyChar + cyChar > cyClient)<br />
{<br />
InvalidateRect(hwnd, NULL, TRUE);<br />
UpdateWindow(hwnd);<br />
iLine = 0;<br />
}<br />
return iLine;<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int iNum, iLine, cyClient;<br />
HDC hdc;<br />
TCHAR szBuffer[16];<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
if (iNum < 0)<br />
iNum = 0;<br />
<br />
iLine = CheckBottom(hwnd, cyClient, iLine);<br />
hdc = GetDC(hwnd);<br />
<br />
TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
wsprintf(szBuffer, TEXT("%d"), iNum++));<br />
<br />
ReleaseDC(hwnd, hdc);<br />
iLine++;<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int iNum = 1, iLine, cyClient;<br />
HDC hdc;<br />
int i, iSqrt;<br />
TCHAR szBuffer[16];<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
do<br />
{<br />
if (++iNum < 0)<br />
iNum = 0;<br />
<br />
iSqrt = (int) sqrt(iNum);<br />
for (i = 2; i <= iSqrt; i++)<br />
if (iNum % i == 0)<br />
break;<br />
}<br />
while (i <= iSqrt);<br />
<br />
iLine = CheckBottom(hwnd, cyClient, iLine);<br />
hdc = GetDC(hwnd);<br />
<br />
TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
wsprintf(szBuffer, TEXT("%d"), iNum));<br />
<br />
ReleaseDC(hwnd, hdc);<br />
iLine++;<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int iNum = 0, iNext = 1, iLine, cyClient;<br />
HDC hdc;<br />
int iTemp;<br />
TCHAR szBuffer[16];<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
if (iNum < 0)<br />
{<br />
iNum = 0;<br />
iNext = 1;<br />
}<br />
<br />
iLine = CheckBottom(hwnd, cyClient, iLine);<br />
hdc = GetDC(hwnd);<br />
<br />
TextOut(hdc, 0, iLine * cyChar, szBuffer,<br />
wsprintf(szBuffer, TEXT("%d"), iNum));<br />
<br />
ReleaseDC(hwnd, hdc);<br />
iTemp = iNum;<br />
iNum = iNext;<br />
iNext += iTemp;<br />
iLine++;<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static int cxClient, cyClient;<br />
HDC hdc;<br />
int iDiameter;<br />
<br />
switch (message)<br />
{<br />
case WM_SIZE:<br />
cxClient = LOWORD(lParam);<br />
cyClient = HIWORD(lParam);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
InvalidateRect(hwnd, NULL, TRUE);<br />
UpdateWindow(hwnd);<br />
<br />
iDiameter = rand() % (max(1, min(cxClient, cyClient)));<br />
hdc = GetDC(hwnd);<br />
<br />
Ellipse(hdc, (cxClient - iDiameter) / 2,<br />
(cyClient - iDiameter) / 2,<br />
(cxClient + iDiameter) / 2,<br />
(cyClient + iDiameter) / 2);<br />
<br />
ReleaseDC(hwnd, hdc);<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}<br />
<br />
<br />
LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
static HWND hwndChild[4];<br />
static TCHAR* szChildClass[] = { TEXT("Child1"), TEXT("Child2"),<br />
<br />
TEXT("Child3"),<br />
TEXT("Child4") };<br />
static WNDPROC ChildProc[] = { WndProc1, WndProc2, WndProc3, WndProc4 };<br />
HINSTANCE hInstance;<br />
int i, cxClient, cyClient;<br />
WNDCLASS wc;<br />
<br />
switch (message)<br />
{<br />
case WM_CREATE:<br />
hInstance = (HINSTANCE) GetWindowLong(hwnd, GWL_HINSTANCE);<br />
<br />
wc.style = CS_HREDRAW | CS_VREDRAW;<br />
wc.cbClsExtra = 0;<br />
wc.cbWndExtra = 0;<br />
wc.hInstance = hInstance;<br />
wc.hIcon = NULL;<br />
wc.hCursor = LoadCursor(NULL, IDC_ARROW);<br />
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);<br />
wc.lpszMenuName = NULL;<br />
<br />
for (i = 0; i < 4; i++)<br />
{<br />
wc.lpfnWndProc = ChildProc[i];<br />
wc.lpszClassName = szChildClass[i];<br />
<br />
RegisterClass(&wc);<br />
<br />
hwndChild[i] = CreateWindow(szChildClass[i], NULL,<br />
<br />
WS_CHILDWINDOW |<br />
WS_BORDER | WS_VISIBLE,<br />
<br />
0, 0, 0, 0,<br />
<br />
hwnd, (HMENU) i,<br />
hInstance, NULL);<br />
}<br />
<br />
cyChar = HIWORD(GetDialogBaseUnits());<br />
SetTimer(hwnd, 1, 10, NULL);<br />
return 0;<br />
<br />
case WM_SIZE:<br />
cxClient = LOWORD(lParam);<br />
cyClient = HIWORD(lParam);<br />
<br />
for (i = 0; i < 4; i++);<br />
MoveWindow(hwndChild[i], (i % 2) * cxClient / 2,<br />
<br />
(i > 1) * cyClient / 2,<br />
<br />
cxClient / 2, cyClient / 2,<br />
TRUE);<br />
return 0;<br />
<br />
case WM_TIMER:<br />
for (i = 0; i < 4; i++)<br />
SendMessage(hwndChild[i], WM_TIMER, wParam, lParam);<br />
return 0;<br />
<br />
case WM_CHAR:<br />
if (wParam == '\x1B')<br />
DestroyWindow(hwnd);<br />
return 0;<br />
<br />
case WM_DESTROY:<br />
KillTimer(hwnd, 1);<br />
PostQuitMessage(0);<br />
return 0;<br />
}<br />
return DefWindowProc(hwnd, message, wParam, lParam);<br />
}
Help in any form is welcome
Thanks
|