C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-30-2006, 07:04 PM   #1
Mad
 
OnionKnight's Avatar
 
Join Date: Jan 2005
Location: Umeå, Sweden
Posts: 555
Can't create child windows

I am trying to make one main window and inside that one I want to have one window that renders OpenGL stuff and another one that is an edit control. So I made this:
Code:
const char wndclass[] = "OpenGL Program";
const char oglclass[] = "OpenGL Renderer";
WNDPROC oldconproc;
HWND window;

int WINAPI WinMain (HINSTANCE inst, HINSTANCE pinst, char* args, int show)
{
	WNDCLASSEX wc;
	WNDCLASSEX oc;
	MSG msg;

	ZeroMemory(&wc, sizeof(wc));
	wc.cbSize        = sizeof(wc);
	wc.lpfnWndProc   = wndproc;
	wc.hInstance     = inst;
	wc.hbrBackground = COLOR_WINDOW;
	wc.lpszClassName = wndclass;

	ZeroMemory(&oc, sizeof(oc));
	oc.cbSize        = sizeof(oc);
	oc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
	oc.lpfnWndProc   = oglproc;
	oc.hInstance     = inst;
	oc.hbrBackground = COLOR_WINDOW;
	oc.lpszClassName = oglclass;

	if (!RegisterClassEx(&wc) || !RegisterClassEx(&oc)) {
		Error();
		return 1;
	}
	window = CreateWindowEx(WS_EX_CLIENTEDGE, wndclass, "OpenGL", WS_OVERLAPPEDWINDOW,
	                        CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, inst, NULL);
	if (window == NULL) {
		Error();
		return 1;
	}
	ShowWindow(window, show);
	UpdateWindow(window);

	while (GetMessage(&msg, NULL, 0, 0) > 0) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}
This part of the code runs fine, but when it gets to WM_CREATE for the window I just made it throws me an error because it couldn't create neither of the two child windows with the error message "Cannot create a top-level child window.".

The wndproc function, cut off to show the important parts:
Code:
LRESULT CALLBACK wndproc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	static HWND ogl;
	static HWND con;
	static int fonth;

	switch (msg) {
		case WM_CREATE:
		{
			HFONT font, oldfont;
			HDC condc;
			TEXTMETRIC tm;

			font = CreateFont(-MulDiv(12, GetDeviceCaps(GetDC(hwnd), LOGPIXELSY), 72), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("MS Shell Dlg"));
			/*Here comes the trouble makers*/
			ogl = CreateWindowEx(0, oglclass, TEXT(""), WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0, 0, 0, 0, window, NULL, GetModuleHandle(NULL), NULL);
			con = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""), WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL, 0, 0, 0, 0, window, NULL, GetModuleHandle(NULL), NULL);
			if (font == NULL || ogl == NULL || con == NULL) { /*End of the line*/
				Error();
				PostQuitMessage(1);
				break;
			}
			oldconproc = SetWindowLongPtr(con, GWLP_WNDPROC, (LONG_PTR) conproc);
			SendMessage(con, WM_SETFONT, (WPARAM) font, MAKELPARAM(FALSE, 0));
			condc = GetDC(con);
			oldfont = SelectObject(condc, font);
			GetTextMetrics(condc, &tm);
			SelectObject(condc, oldfont);
			ReleaseDC(con, condc);
			fonth = tm.tmHeight;
			break;
		}
'ogl' is the child window that is to draw the OpenGL stuff and 'con', for console, is the edit control.
Looking up the error message doesn't help me solve my problem, "As part of its user interface, an application has tried to create a child window at an inappropriate level.", what are these "levels" to begin with?
OnionKnight is offline   Reply With Quote
Old 11-30-2006, 08:05 PM   #2
erstwhile
 
Join Date: Jan 2002
Posts: 2,223
The variable window isn't a valid window handle until CreateWindowEx returns (recall that a WM_CREATE message is issued during system processing of that api function) so the child windows essentially have a NULL handle as a parent. A top-level window is a non-child window, like your main window.

Just use the HWND parameter passed to the window procedure in the WM_CREATE handler as the parent handle when creating the child windows .
__________________
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
Ken Fitlike is offline   Reply With Quote
Old 11-30-2006, 08:11 PM   #3
Mad
 
OnionKnight's Avatar
 
Join Date: Jan 2005
Location: Umeå, Sweden
Posts: 555
You're right, I had forgotten about that, even though I copypasted stuff from another program which used hwnd instead. :|
Now it works fine, except that the OpenGL window isn't showing anything. EDIT: Forgot WS_VISIBLE. Now everything's like it should.

Last edited by OnionKnight; 11-30-2006 at 08:15 PM.
OnionKnight is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How can you make a parent process wait for a child? I'm gettin a seg fault. mr_coffee C Programming 3 10-15-2008 09:24 AM
Removing/Hiding Child Windows Hysteresis Windows Programming 4 07-14-2006 11:50 AM
Child window inside child window Magos Windows Programming 13 04-20-2004 06:51 AM
child windows face_master Windows Programming 14 03-01-2002 07:08 AM


All times are GMT -6. The time now is 07:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22