Creating a child window in a parent window

This is a discussion on Creating a child window in a parent window within the Windows Programming forums, part of the Platform Specific Boards category; HI, I'm new to writing code for Win32. I don't really know much about it. I'm trying to create a ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Creating a child window in a parent window

    HI,

    I'm new to writing code for Win32. I don't really know much about it.

    I'm trying to create a program that can hold a child window.
    -The child window would be inside the parent window. It kind of sounds kind of simple, but I can't figure out how to code it.

    Here' my code
    Code:
    #include <windows.h>
    #include <mmsystem.h>
    
    LRESULT CALLBACK HelloWndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    					PSTR szCMLine, int iCmdShow){
    	static TCHAR szAppName[] = TEXT ("HelloApplication");
    	HWND	hwnd;
    	HWND    childhwnd;
    	MSG		msg;
    	WNDCLASS wndclass;
    
    	wndclass.style		= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc = HelloWndProc;
    	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 95/98/NT"),
    					szAppName, MB_ICONERROR);
    		return 0;
    	}
    
    	hwnd = CreateWindow(szAppName,		
    						TEXT("Hello World for Windows"), 
    						WS_OVERLAPPEDWINDOW,	
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						NULL,				
    						NULL,				
    						hInstance,			
    						NULL);				
    
    	ShowWindow(hwnd, iCmdShow);
    	UpdateWindow(hwnd);
    
    	childhwnd = CreateWindow(szAppName,		
    						TEXT("Hello World for Windows"), 
    						WS_OVERLAPPEDWINDOW,	
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						NULL,				
    						NULL,				
    						hInstance,			
    						NULL);				
    
    	ShowWindow(childhwnd, iCmdShow);
    	UpdateWindow(childhwnd);
    
    	while (GetMessage(&msg, NULL, 0, 0)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    
    
    LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    	HDC		hdc;
    	PAINTSTRUCT ps;
    	RECT	rect;
    
    	switch (message){
    		case WM_CREATE:
    			PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
    			return 0;
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    
    			GetClientRect(hwnd, &rect);
    
    			DrawText(hdc, TEXT("Hello, Windows"), -1, &rect,
    					DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    
    			EndPaint(hwnd, &ps);
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Look at the docs, http://msdn2.microsoft.com/en-us/library/ms632679.aspx

    The 4th last parameter of CreateWindow() is the parent handle (HWND), you've specified NULL when you create the child, it should be 'hwnd' (the parent).

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    Here's my new code, but still can't create the child window.

    Code:
    #include <windows.h>
    #include <mmsystem.h>
    #define NUM    1000
    #define TWOPI  (2 * 3.14159)
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    LRESULT CALLBACK HelloWndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    					PSTR szCMLine, int iCmdShow)
    {
    	static TCHAR szAppName[] = TEXT ("HelloApplication");
    	static TCHAR szAppName2[] = TEXT ("SineApplication");
    	HWND	hwnd;
    	HWND	hwnd2;
    	MSG		msg; 
    	WNDCLASS wndclass;
    
    	wndclass.style		= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc = HelloWndProc;
    	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;
    
    	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 95/98/NT"),
    					szAppName, MB_ICONERROR);
    		return 0;
    	}
    
    	hwnd = CreateWindow(szAppName,		
    						TEXT("Hello World for Windows"), 
    						WS_OVERLAPPEDWINDOW,	
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						NULL,				
    						NULL,				
    						hInstance,			
    						NULL);				
    
    	ShowWindow(hwnd, iCmdShow);
    	UpdateWindow(hwnd);
    
    	hwnd2 = CreateWindow(szAppName2,		
    						TEXT("Hello World"), 
    						WS_OVERLAPPEDWINDOW|WS_CHILD | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,	
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						hwnd,		//parent		
    						NULL,				
    						hInstance,			
    						NULL);				
    
    	ShowWindow(hwnd2, iCmdShow);
    	UpdateWindow(hwnd2);
    
    	while (GetMessage(&msg, NULL, 0, 0)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    
    
    LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    	HDC		hdc;
    	PAINTSTRUCT ps;
    	RECT	rect;
    
    	switch (message){
    		case WM_CREATE:
    			PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
    			return 0;
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    
    			GetClientRect(hwnd, &rect);
    
    			DrawText(hdc, TEXT("Hello, Windows"), -1, &rect,
    					DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    
    			EndPaint(hwnd, &ps);
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         HDC		hdc;
    	PAINTSTRUCT ps;
    	RECT	rect;
    
    	switch (message){
    		case WM_CREATE:
    			PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
    			return 0;
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    
    			GetClientRect(hwnd, &rect);
    
    			DrawText(hdc, TEXT("Hello, Windows"), -1, &rect,
    					DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    
    			EndPaint(hwnd, &ps);
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    Last edited by vopo; 10-04-2007 at 02:13 AM.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    What do you mean "can't create the child window"?

    I suggest add some error checking with GetLastError() and look-up the GetLastError() values on MSDN.

  5. #5
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    307
    Hi vopo:

    There are basically two reasons to have child window in a parent:
    a) Working MDI, see winprog example.
    b) Working with tabs, see PropertyPage.

    Sometimes users add a inner child window to hide a bunch of controls...that's rarelly...
    So...what is your goal?
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  6. #6
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Your putting two datas in the same variable.

    Code:
    wndclass.style		= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc = HelloWndProc;
    	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;
    
    	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;
    your mdi child should be separate

    Code:
    HWND CreateNewMDIChild(HWND hMDIClient)
    {
    	MDICREATESTRUCT mcs;
    	HWND hChild;
    
    	mcs.szTitle = "[Untitled]";
    	mcs.szClass = g_szChildClassName;
    	mcs.hOwner  = GetModuleHandle(NULL);
    	mcs.x = mcs.cx = CW_USEDEFAULT;
    	mcs.y = mcs.cy = CW_USEDEFAULT;
    	mcs.style = MDIS_ALLCHILDSTYLES;
    
    	hChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
    	if(!hChild)
    	{
    		MessageBox(hMDIClient, "MDI Child creation failed.", "Oh Oh...",
    			MB_ICONEXCLAMATION | MB_OK);
    	}
    	return hChild;
    }

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You need to call RegisterClass for each class you register, not just once.

    Also, your code template seems a bit outdated. Windows 95/98/NT?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    If you use the WS_CHILD then the HMENU param must be the ID number of the child.

    ie
    #define IDD_MYCHILD 40001

    //in the HMENU param of CreateWindow
    (HMENU)IDD_MYCHILD
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    I finally figured it out

    Thanks for the help

    Here's my code:
    Code:
    #include <windows.h>
    #include <mmsystem.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    LRESULT CALLBACK HelloWndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    					PSTR szCMLine, int iCmdShow)
    {
    	static TCHAR szAppName[] = TEXT ("HelloApplication");
    	static TCHAR szAppName2[] = TEXT ("SineApplication");
    	HWND hwnd,hwnd2;
    	MSG	msg; 
    	WNDCLASS wndclass, mywndclass;
    
    	wndclass.style		= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc = HelloWndProc;
    	wndclass.cbClsExtra	= 0;
    	wndclass.cbWndExtra = sizeof(long);
    	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;
    
    	mywndclass.style		= CS_HREDRAW | CS_VREDRAW;
    	mywndclass.lpfnWndProc = WndProc;
    	mywndclass.cbClsExtra	= 0;
    	mywndclass.cbWndExtra = sizeof(long);
    	mywndclass.hInstance	= hInstance;
    	mywndclass.hIcon		= LoadIcon (NULL, IDI_APPLICATION);
    	mywndclass.hCursor	= LoadCursor (NULL, IDC_ARROW);
    	mywndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    	mywndclass.lpszMenuName = NULL;
    	mywndclass.lpszClassName = szAppName2;
    
    	if (!RegisterClass (&wndclass))
    	{
    		MessageBox (NULL, TEXT ("This program requires Windows 95/98/NT"),
    					szAppName, MB_ICONERROR);
    		return 0;
    	}
    
    	if (!RegisterClass (&mywndclass))
    	{
    		MessageBox (NULL, TEXT ("This program requires Windows 95/98/NT"),
    					szAppName2, MB_ICONERROR);
    		return 0;
    	}
    
    	hwnd = CreateWindow(szAppName,		
    						TEXT("Hello World for Windows"), 
    						WS_OVERLAPPEDWINDOW,	
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						NULL,				
    						NULL,				
    						hInstance,			
    						NULL);				
    
    	ShowWindow(hwnd, iCmdShow);
    	UpdateWindow(hwnd);
    
    	hwnd2 = CreateWindow(szAppName2,		
    						TEXT("Hello World"), 
    						WS_CHILD | WS_VISIBLE | WS_CAPTION 
    						| WS_SYSMENU | WS_THICKFRAME 
    						| WS_MINIMIZEBOX | WS_MAXIMIZEBOX,	
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						300,		
    						300,		
    						hwnd,				
    						NULL,				
    						hInstance,			
    						NULL);				
    
    	ShowWindow(hwnd2, iCmdShow);
    	UpdateWindow(hwnd2);
    
    	while (GetMessage(&msg, NULL, 0, 0)){
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return msg.wParam;
    }
    
    
    
    LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC		hdc;
    	PAINTSTRUCT ps;
    	RECT	rect;
    
    	switch (message)
    	{
    		case WM_CREATE:
    			PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
    			return 0;
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    
    			GetClientRect(hwnd, &rect);
    
    			//DrawText(hdc, TEXT("Hello, Windows"), -1, &rect,
    			//		DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    
    			EndPaint(hwnd, &ps);
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    	}
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HDC		hdc;
    	PAINTSTRUCT ps;
    	RECT	rect;
    
    	switch (message){
    		case WM_CREATE:
    			PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
    			return 0;
    
    		case WM_PAINT:
    			hdc = BeginPaint(hwnd, &ps);
    
    			GetClientRect(hwnd, &rect);
    
    			DrawText(hdc, TEXT("Hello, Windows"), -1, &rect,
    					DT_SINGLELINE | DT_CENTER | DT_VCENTER);
    
    			EndPaint(hwnd, &ps);
    			return 0;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    	}
    
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Update parent window from child?
    By ac251404 in forum Windows Programming
    Replies: 1
    Last Post: 07-25-2006, 07:31 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Posting a message to a child window
    By axr0284 in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2005, 08:35 AM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM

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