hi!
I wrote this code that test multiple windows. I actually need this for another program, but I'm trying to figure out how creating windows work. so in this code I created two windows, and in one of them I create another two. but when I run it, it keeps doing funny things. like when I do this -
MoveWindow(hwndChild[0], cxClient/2, cyClient/2, cxClient/2, cyClient/2, TRUE);
it actually shows a window the size I want it. and although I created two windows that have two windows in them, only one of them shows.
all the windows are do almost the same thing (it's not really that long), except wndproc1 that in addition, creates 2 windows within it.
the reason why I'm playing with this, is because in another program I did, a window I created, creates another window within itself, but for some reason, those windows are not created. if anyone understands why that happens, or/and where can I find more about it, I'd love to know.
thanks alot :)
#include<windows.h>
#include<math.h>
#define TWO_PI (2.0 * 3.14159)
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LRESULT APIENTRY WndProc1 (HWND, UINT, WPARAM, LPARAM);
LRESULT APIENTRY WndProc2 (HWND, UINT, WPARAM, LPARAM);
LRESULT APIENTRY WndProc3 (HWND, UINT, WPARAM, LPARAM);
LRESULT APIENTRY WndProc4 (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
[INDENT]static TCHAR szAppName[] = TEXT ("SineWave") ;[/INDENT][INDENT]HWND hwnd ;[/INDENT][INDENT]MSG msg ;[/INDENT][INDENT]WNDCLASS wndclass ;[/INDENT][INDENT]wndclass.style = CS_HREDRAW | CS_VREDRAW ;[/INDENT][INDENT]wndclass.lpfnWndProc = WndProc ;[/INDENT][INDENT]wndclass.cbClsExtra = 0 ;[/INDENT][INDENT]wndclass.cbWndExtra = 0 ;[/INDENT][INDENT]wndclass.hInstance = hInstance ;[/INDENT][INDENT]wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;[/INDENT][INDENT]wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;[/INDENT][INDENT]wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;[/INDENT][INDENT]wndclass.lpszMenuName = NULL ;[/INDENT][INDENT]wndclass.lpszClassName = szAppName ;[/INDENT][INDENT]if (!RegisterClass (&wndclass))[/INDENT][INDENT]{[INDENT]MessageBox (NULL, TEXT ("Program requires Windows NT!"), [/INDENT][INDENT]szAppName, MB_ICONERROR) ;[/INDENT][INDENT]return 0 ;[/INDENT]}
hwnd = CreateWindow (szAppName, TEXT ("win in win"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{[INDENT]TranslateMessage (&msg) ;[/INDENT][INDENT]DispatchMessage (&msg) ;[/INDENT]}
return msg.wParam ;
[/INDENT]}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{[INDENT]static HWND hwndChild[4] ;[/INDENT][INDENT]static TCHAR * szChildClass[] = { TEXT ("App1"), TEXT ("App2") };[/INDENT][INDENT]static WNDPROC ChildProc[] = { WndProc1, WndProc2} ; [/INDENT][INDENT]static HINSTANCE hInstance, hInst ;[/INDENT][INDENT]static int i, cxClient, cyClient ;[/INDENT][INDENT]WNDCLASS wndclass ;[/INDENT]
[INDENT]switch (message)[/INDENT][INDENT]{[INDENT]case WM_CREATE:[INDENT]hInst = ((LPCREATESTRUCT) lParam)->hInstance ;[/INDENT][INDENT]hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;[/INDENT][INDENT]wndclass.style = CS_HREDRAW | CS_VREDRAW ;[/INDENT][INDENT]wndclass.cbClsExtra = 0 ;[/INDENT][INDENT]wndclass.cbWndExtra = 0 ;[/INDENT][INDENT]wndclass.hInstance = hInstance ;[/INDENT][INDENT]wndclass.hIcon = NULL ;[/INDENT][INDENT]wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;[/INDENT][INDENT]wndclass.hbrBackground = (HBRUSH) GetStockObject (COLOR_BACKGROUND);[/INDENT][INDENT]wndclass.lpszMenuName = NULL ;[/INDENT][INDENT]for (i = 0 ; i < 2 ; i++)[/INDENT][INDENT]{[INDENT]wndclass.lpfnWndProc = ChildProc[i] ;[/INDENT][INDENT]wndclass.lpszClassName = szChildClass[i] ;[/INDENT][INDENT]RegisterClass (&wndclass) ;[/INDENT][INDENT]hwndChild[i] = CreateWindow (szChildClass[i], NULL,[/INDENT][INDENT]WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,[/INDENT][INDENT]0, 0, 0, 0, [/INDENT][INDENT]hwnd, (HMENU) i, hInstance, NULL) ;[/INDENT][INDENT]hwndChild[i+2] = CreateWindow (szChildClass[i], NULL,[/INDENT][INDENT]WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,[/INDENT][INDENT]0, 0, 0, 0, [/INDENT][INDENT]hwnd, (HMENU) i, hInstance, NULL) ;[/INDENT]}
[/INDENT]case WM_SIZE:[INDENT]cxClient = LOWORD (lParam) ;[/INDENT][INDENT]cyClient = HIWORD (lParam) ;[/INDENT][INDENT]MoveWindow(hwndChild[2], 0, 0, cxClient/2, cyClient/2, TRUE);[/INDENT][INDENT]MoveWindow(hwndChild[0], cxClient/2, cyClient/2, cxClient/2, cyClient/2, TRUE);[/INDENT][INDENT]MoveWindow(hwndChild[1], 0, cyClient/2, cxClient/2, cyClient/2 , TRUE);[/INDENT][INDENT]MoveWindow(hwndChild[3], cxClient/2, 0,cxClient/2 , cyClient/2, TRUE);[/INDENT]return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
[/INDENT]}
return DefWindowProc (hwnd, message, wParam, lParam) ;
[/INDENT]}
LRESULT APIENTRY WndProc1 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)[INDENT]{[/INDENT][INDENT]static HWND hwndChild[2] ;[/INDENT][INDENT]static TCHAR * szChildClass[] = { TEXT ("App3"), TEXT ("App4") };[/INDENT][INDENT]static WNDPROC ChildProc[] = { WndProc3, WndProc4} ; [/INDENT][INDENT]static HINSTANCE hInstance, hInst ;[/INDENT][INDENT]WNDCLASS wndclass ;[/INDENT][INDENT]static HRGN hRgnClip ;[/INDENT][INDENT]static int cxClient, cyClient, i ;[/INDENT][INDENT]double fAngle, fRadius ;[/INDENT][INDENT]HCURSOR hCursor ;[/INDENT][INDENT]HDC hdc ;[/INDENT][INDENT]HRGN hRgnTemp[6] ;[/INDENT][INDENT]PAINTSTRUCT ps ;[/INDENT][INDENT]switch (message)[/INDENT][INDENT]{[INDENT]case WM_CREATE:[INDENT]hInst = ((LPCREATESTRUCT) lParam)->hInstance ;[/INDENT][INDENT]hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;[/INDENT][INDENT]wndclass.style = CS_HREDRAW | CS_VREDRAW ;[/INDENT][INDENT]wndclass.cbClsExtra = 0 ;[/INDENT][INDENT]wndclass.cbWndExtra = 0 ;[/INDENT][INDENT]wndclass.hInstance = hInstance ;[/INDENT][INDENT]wndclass.hIcon = NULL ;[/INDENT][INDENT]wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;[/INDENT][INDENT]wndclass.hbrBackground = (HBRUSH) GetStockObject (COLOR_BACKGROUND);[/INDENT][INDENT]wndclass.lpszMenuName = NULL ;[/INDENT][INDENT]for (i = 0 ; i < 2 ; i++)[/INDENT][INDENT]{[INDENT]wndclass.lpfnWndProc = ChildProc[i] ;[/INDENT][INDENT]wndclass.lpszClassName = szChildClass[i] ;[/INDENT][INDENT]RegisterClass (&wndclass) ;[/INDENT][INDENT]hwndChild[i] = CreateWindow (szChildClass[i], NULL,[/INDENT][INDENT]WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE,[/INDENT][INDENT]1, 10, 50, 50, [/INDENT][INDENT]hwnd, (HMENU) i, hInstance, NULL) ;[/INDENT]}
[/INDENT]break;
case WM_SIZE:[INDENT]cxClient = LOWORD (lParam) ;[/INDENT][INDENT]cyClient = HIWORD (lParam) ;[/INDENT][INDENT]ShowCursor (TRUE) ;[/INDENT]
[INDENT]if (hRgnClip)[INDENT]DeleteObject (hRgnClip) ;[/INDENT]hRgnTemp[0] = CreateEllipticRgn (0, cyClient / 3,
cxClient / 2, 2 * cyClient / 3) ;
hRgnTemp[1] = CreateEllipticRgn (cxClient / 2, cyClient / 3,
cxClient, 2 * cyClient / 3) ;
hRgnTemp[2] = CreateEllipticRgn (cxClient / 3, 0,
2 * cxClient / 3, cyClient / 2) ;
hRgnTemp[3] = CreateEllipticRgn (cxClient / 3, cyClient / 2,
2 * cxClient / 3, cyClient) ;
hRgnTemp[4] = CreateRectRgn (0, 0, 1, 1) ;
hRgnTemp[5] = CreateRectRgn (0, 0, 1, 1) ;
hRgnClip = CreateRectRgn (0, 0, 1, 1) ;
CombineRgn (hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR) ;
CombineRgn (hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR) ;
CombineRgn (hRgnClip, hRgnTemp[4], hRgnTemp[5], RGN_XOR) ;
for (i = 0 ; i < 6 ; i++)[INDENT]DeleteObject (hRgnTemp[i]) ;[/INDENT][INDENT]MoveWindow(hwndChild[1], 0, 0, cxClient/2, cyClient/2, TRUE);[/INDENT][INDENT]MoveWindow(hwndChild[0], cxClient/2, cyClient/2, cxClient/2, cyClient/2, TRUE);[/INDENT]return 0 ;
[/INDENT]case WM_PAINT:[INDENT]hdc = BeginPaint (hwnd, &ps) ;[/INDENT]
[INDENT]SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;[/INDENT][INDENT]SelectClipRgn (hdc, hRgnClip) ;[/INDENT]
[INDENT]fRadius = _hypot (cxClient / 2.0, cyClient / 2.0) ;[/INDENT]
[INDENT]for (fAngle = 0.0 ; fAngle < TWO_PI ; fAngle += TWO_PI / 360)[/INDENT][INDENT]{[INDENT]MoveToEx (hdc, 0, 0, NULL) ;[/INDENT][INDENT]LineTo (hdc, (int) ( fRadius * cos (fAngle) + 0.5),[/INDENT][INDENT](int) (-fRadius * sin (fAngle) + 0.5)) ;[/INDENT]}
EndPaint (hwnd, &ps) ;
return 0 ;
[/INDENT]case WM_DESTROY:[INDENT]return 0;[/INDENT]
[/INDENT]}
return DefWindowProc (hwnd, message, wParam, lParam) ;
[/INDENT]}
LRESULT APIENTRY WndProc2 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{[INDENT]static HRGN hRgnClip ;[/INDENT][INDENT]static int cxClient, cyClient ;[/INDENT][INDENT]double fAngle, fRadius ;[/INDENT][INDENT]HCURSOR hCursor ;[/INDENT][INDENT]HDC hdc ;[/INDENT][INDENT]HRGN hRgnTemp[6] ;[/INDENT][INDENT]int i ;[/INDENT][INDENT]PAINTSTRUCT ps ;[/INDENT][INDENT]switch (message)[/INDENT][INDENT]{[INDENT]case WM_CREATE:[/INDENT][INDENT]break;[/INDENT][INDENT]case WM_SIZE:[/INDENT][INDENT]cxClient = LOWORD (lParam) ;[/INDENT][INDENT]cyClient = HIWORD (lParam) ;[/INDENT][INDENT]ShowCursor (TRUE) ;[/INDENT]
[INDENT]if (hRgnClip)[INDENT]DeleteObject (hRgnClip) ;[/INDENT]hRgnTemp[0] = CreateEllipticRgn (0, cyClient / 3,
cxClient / 2, 2 * cyClient / 3) ;
hRgnTemp[1] = CreateEllipticRgn (cxClient / 2, cyClient / 3,
cxClient, 2 * cyClient / 3) ;
hRgnTemp[2] = CreateEllipticRgn (cxClient / 3, 0,
2 * cxClient / 3, cyClient / 2) ;
hRgnTemp[3] = CreateEllipticRgn (cxClient / 3, cyClient / 2,
2 * cxClient / 3, cyClient) ;
hRgnTemp[4] = CreateRectRgn (0, 0, 1, 1) ;
hRgnTemp[5] = CreateRectRgn (0, 0, 1, 1) ;
hRgnClip = CreateRectRgn (0, 0, 1, 1) ;
CombineRgn (hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR) ;
CombineRgn (hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR) ;
CombineRgn (hRgnClip, hRgnTemp[4], hRgnTemp[5], RGN_XOR) ;
for (i = 0 ; i < 6 ; i++)[INDENT]DeleteObject (hRgnTemp[i]) ;[/INDENT]return 0 ;
[/INDENT]case WM_PAINT:[INDENT]hdc = BeginPaint (hwnd, &ps) ;[/INDENT]
[INDENT]SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;[/INDENT][INDENT]SelectClipRgn (hdc, hRgnClip) ;[/INDENT]
[INDENT]fRadius = _hypot (cxClient / 2.0, cyClient / 2.0) ;[/INDENT]
[INDENT]for (fAngle = 0.0 ; fAngle < TWO_PI ; fAngle += TWO_PI / 360)[/INDENT][INDENT]{[INDENT]MoveToEx (hdc, 0, 0, NULL) ;[/INDENT][INDENT]LineTo (hdc, (int) ( fRadius * cos (fAngle) + 0.5),[/INDENT][INDENT](int) (-fRadius * sin (fAngle) + 0.5)) ;[/INDENT]}
EndPaint (hwnd, &ps) ;
return 0 ;
[/INDENT]case WM_DESTROY:[INDENT]KillTimer( hwnd, 1);[/INDENT][INDENT]return 0;[/INDENT]}
return DefWindowProc (hwnd, message, wParam, lParam) ;
[/INDENT]}
LRESULT APIENTRY WndProc3 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{[INDENT]static HRGN hRgnClip ;[/INDENT][INDENT]static int cxClient, cyClient ;[/INDENT][INDENT]double fAngle, fRadius ;[/INDENT][INDENT]HCURSOR hCursor ;[/INDENT][INDENT]HDC hdc ;[/INDENT][INDENT]HRGN hRgnTemp[6] ;[/INDENT][INDENT]int i ;[/INDENT][INDENT]PAINTSTRUCT ps ;[/INDENT][INDENT]switch (message)[/INDENT][INDENT]{[/INDENT][INDENT]case WM_CREATE:[INDENT]break;[/INDENT]
case WM_SIZE:[INDENT]cxClient = LOWORD (lParam) ;[/INDENT][INDENT]cyClient = HIWORD (lParam) ;[/INDENT][INDENT]ShowCursor (TRUE) ;[/INDENT][INDENT]if (hRgnClip)[INDENT]DeleteObject (hRgnClip) ;[/INDENT]hRgnTemp[0] = CreateEllipticRgn (0, cyClient / 3,
cxClient / 2, 2 * cyClient / 3) ;
hRgnTemp[1] = CreateEllipticRgn (cxClient / 2, cyClient / 3,
cxClient, 2 * cyClient / 3) ;
hRgnTemp[2] = CreateEllipticRgn (cxClient / 3, 0,
2 * cxClient / 3, cyClient / 2) ;
hRgnTemp[3] = CreateEllipticRgn (cxClient / 3, cyClient / 2,
2 * cxClient / 3, cyClient) ;
hRgnTemp[4] = CreateRectRgn (0, 0, 1, 1) ;
hRgnTemp[5] = CreateRectRgn (0, 0, 1, 1) ;
hRgnClip = CreateRectRgn (0, 0, 1, 1) ;
CombineRgn (hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR) ;
CombineRgn (hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR) ;
CombineRgn (hRgnClip, hRgnTemp[4], hRgnTemp[5], RGN_XOR) ;
for (i = 0 ; i < 6 ; i++)[INDENT]DeleteObject (hRgnTemp[i]) ;[/INDENT]return 0 ;
[/INDENT]case WM_PAINT:[INDENT]hdc = BeginPaint (hwnd, &ps) ;[/INDENT][INDENT]SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;[/INDENT][INDENT]SelectClipRgn (hdc, hRgnClip) ;[/INDENT]
[INDENT]fRadius = _hypot (cxClient / 2.0, cyClient / 2.0) ;[/INDENT]
[INDENT]for (fAngle = 0.0 ; fAngle < TWO_PI ; fAngle += TWO_PI / 360)[/INDENT][INDENT]{[INDENT]MoveToEx (hdc, 0, 0, NULL) ;[/INDENT][INDENT]LineTo (hdc, (int) ( fRadius * cos (fAngle) + 0.5),[/INDENT][INDENT](int) (-fRadius * sin (fAngle) + 0.5)) ;[/INDENT]}
EndPaint (hwnd, &ps) ;
return 0 ;
[/INDENT]case WM_DESTROY:
KillTimer( hwnd, 1);
return 0;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
[/INDENT]}
LRESULT APIENTRY WndProc4 (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{[INDENT]static HRGN hRgnClip ;[/INDENT][INDENT]static int cxClient, cyClient ;[/INDENT][INDENT]double fAngle, fRadius ;[/INDENT][INDENT]HCURSOR hCursor ;[/INDENT][INDENT]HDC hdc ;[/INDENT][INDENT]HRGN hRgnTemp[6] ;[/INDENT][INDENT]int i ;[/INDENT][INDENT]PAINTSTRUCT ps ;[/INDENT][INDENT]switch (message)[/INDENT][INDENT]{[INDENT]case WM_CREATE:[INDENT]break;[/INDENT]
case WM_SIZE:[INDENT]cxClient = LOWORD (lParam) ;[/INDENT][INDENT]cyClient = HIWORD (lParam) ;[/INDENT][INDENT]ShowCursor (TRUE) ;[/INDENT]
[INDENT]if (hRgnClip)[INDENT]DeleteObject (hRgnClip) ;[/INDENT]hRgnTemp[0] = CreateEllipticRgn (0, cyClient / 3,
cxClient / 2, 2 * cyClient / 3) ;
hRgnTemp[1] = CreateEllipticRgn (cxClient / 2, cyClient / 3,
cxClient, 2 * cyClient / 3) ;
hRgnTemp[2] = CreateEllipticRgn (cxClient / 3, 0,
2 * cxClient / 3, cyClient / 2) ;
hRgnTemp[3] = CreateEllipticRgn (cxClient / 3, cyClient / 2,
2 * cxClient / 3, cyClient) ;
hRgnTemp[4] = CreateRectRgn (0, 0, 1, 1) ;
hRgnTemp[5] = CreateRectRgn (0, 0, 1, 1) ;
hRgnClip = CreateRectRgn (0, 0, 1, 1) ;
CombineRgn (hRgnTemp[4], hRgnTemp[0], hRgnTemp[1], RGN_OR) ;
CombineRgn (hRgnTemp[5], hRgnTemp[2], hRgnTemp[3], RGN_OR) ;
CombineRgn (hRgnClip, hRgnTemp[4], hRgnTemp[5], RGN_XOR) ;
for (i = 0 ; i < 6 ; i++)[INDENT]DeleteObject (hRgnTemp[i]) ;[/INDENT]return 0 ;
[/INDENT]case WM_PAINT:[INDENT]hdc = BeginPaint (hwnd, &ps) ;[/INDENT]
[INDENT]SetViewportOrgEx (hdc, cxClient / 2, cyClient / 2, NULL) ;[/INDENT][INDENT]SelectClipRgn (hdc, hRgnClip) ;[/INDENT]
[INDENT]fRadius = _hypot (cxClient / 2.0, cyClient / 2.0) ;[/INDENT]
[INDENT]for (fAngle = 0.0 ; fAngle < TWO_PI ; fAngle += TWO_PI / 360)[/INDENT][INDENT]{[INDENT]MoveToEx (hdc, 0, 0, NULL) ;[/INDENT][INDENT]LineTo (hdc, (int) ( fRadius * cos (fAngle) + 0.5),[/INDENT][INDENT](int) (-fRadius * sin (fAngle) + 0.5)) ;[/INDENT]}
EndPaint (hwnd, &ps) ;
return 0 ;
[/INDENT]case WM_DESTROY:
return 0;
[/INDENT]}
return DefWindowProc (hwnd, message, wParam, lParam) ;
[/INDENT]}