CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    9

    Closing childwindow close also mainwindow

    Hi all!

    I have created a Mainwindow with a lot of childwindows in it. The quantity of ChildNow is dynamic cause i want to close or add Childwindwos during the Runtime. Every Childwindow has no titelbar but it has a little button to close the childwindow. Now my problem is that if i close one of the existing Childwindws then the Mainwindow is also closing. How can i avoid it? Here a quick description how my code works:
    Code:
    HWND *Childwin = new HWND(100); // dynamic array for the children
    HWND mainhwnd; // mainwindow
    
    Winmain (...)
    {
    ...
    
    // create mainwindow
    mainhwnd = ....
    
    // create some childs
    for(i=0;i<10;i++)
    {
    Childwin[i] = ....
    }
    
    ...
    
    }
    
    
    Callback(...)
    {
    ...
    	WM_COMMAND
    	...
    		default
    		if a certain quit-Childbutton is pressed
    		DestroyWindow(Child[i]);
    
    	WM_DESTROY
    		PostQuitMessage(0);
    
    	WM_PAINT
    		BitBlt...
    		draw Child and Main...
    
    	WM_TIMER
    	...
    ...
    }
    I guess always if i want to close a childwindow a WM_COMMAND message is generated but also a WM_DESTROY message what also close the mainwindow, but how can i avoid this or anybody has a better concept to close childwindows inside mainwindow?

    My second question is that i want add childwindows during the runtime. On a certain event on my desktop a new childwindow should created and appear in the mainwindow as the other childrens. Does anybody has a starting clue how i can realize?

    Many thanks in advance, best regards rommi

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Closing childwindow close also mainwindow

    Set break points in the "callback", debug your App and see what happens when you "close" child window
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2009
    Posts
    9

    Re: Closing childwindow close also mainwindow

    Hi VictorN,
    thx for your replay. Here is my reduced code to describe the problem. If i close the childwindow the main window is also closeing:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <fstream>
    #include <time.h>
    #include <stdlib.h>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    #pragma comment(lib, "user32.lib")
    
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    // XXXXXX					Deklaration									XXXXXXXX
    // XXXXXX					-------------								XXXXXXXX
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg,	WPARAM wParam, LPARAM lParam );
    LPCSTR MainClassName = "TESTTEST";
    HINSTANCE hInstance;
    #define CHILDX 123
    HWND hWnd;													// Mainwindow
    HWND Child;													// Childwindows
    
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    // XXXXXX						CHILD - SUB								XXXXXXXX
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    void FerstellChilder()		
    {
    	Child = CreateWindowEx (WS_EX_COMPOSITED,MainClassName,"Child", WS_CHILD |
    	WS_OVERLAPPEDWINDOW |WS_VISIBLE ,0,0,500,53,hWnd, (HMENU) CHILDX,hInstance, NULL);
    }
    
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    // XXXXXX					-------------								XXXXXXXX
    // XXXXXX						MAIN									XXXXXXXX
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
    	//==================================================================//
    	//	Create Main-Window												//
    	//==================================================================//
    
        WNDCLASSEX wc;
        MSG wmsg;
    
        wc.cbSize        = sizeof(WNDCLASSEX);
        wc.style         = 0;
        wc.lpfnWndProc   = WndProc;
        wc.cbClsExtra    = 0;
        wc.cbWndExtra    = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = LoadIcon(GetModuleHandle(NULL), IDI_APPLICATION);
    	wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = CreateSolidBrush(RGB(0, 128, 0));
        wc.lpszMenuName  = MainClassName;
        wc.lpszClassName = MainClassName;
        wc.hIconSm       = LoadIcon(GetModuleHandle(NULL), IDI_APPLICATION);
    
        if(!RegisterClassEx(&wc)){return 0;}
    	hWnd = CreateWindowEx(WS_EX_COMPOSITED, MainClassName, "TEST", WS_OVERLAPPEDWINDOW | 
    	WS_VISIBLE,CW_USEDEFAULT, CW_USEDEFAULT, 698, 130, NULL, NULL, hInstance, NULL);
    	
    	//==================================================================//
    	//	Create Childwindow over function								//
    	//==================================================================//
    	FerstellChilder();
    	//==================================================================//
    	//	send messages													//
    	//==================================================================//
        while(GetMessage(&wmsg, NULL, 0, 0))
    	{
    		TranslateMessage(&wmsg); 
    		DispatchMessage(&wmsg);
    	}
        return wmsg.wParam;
    }
    
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    // XXXXXX					-------------								XXXXXXXX
    // XXXXXX						Callback								XXXXXXXX
    // ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
    
    LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    {
        PAINTSTRUCT ps;
    	switch( uMsg )
        {
    		//________________________________________________
    		case WM_COMMAND :
    			{
    				switch( LOWORD( wParam ) )
    				{
    					case CHILDX:
    						DestroyWindow(Child); //close Child
    					break;
    				}
    				break;
    			}
    		break;
    		//________________________________________________
            case WM_DESTROY :
    			{
    				PostQuitMessage(0);
    				return 0;
    			}
    		//________________________________________________
            case WM_PAINT :
    		{
    			BeginPaint(hWnd, &ps);	//paint mainwindow
    			EndPaint(hWnd, &ps);
    
    			BeginPaint(Child, &ps);	//paint childwindow
    			EndPaint(Child, &ps);
    			return 0;
    		}
    		//________________________________________________
    		default :{return( DefWindowProc( hWnd, uMsg, wParam, lParam ));}
        }
    return( 0L );
    }
    I create a breakpoint on the begin of the callback function and a braekpoint of the DestroyWindow function. On the breakpoint on the begin of the callback i get this local messages:

    hWnd 0x000d07ba {unused=??? } HWND__ *
    unused CXX0030: Fehler: Ausdruck kann nicht ausgewertet werden
    uMsg 36 unsigned int
    wParam 0 unsigned int
    lParam 1243124 long

    Im a newbie and at the moment i cant realy interpret what the this message means "unused CXX30: Error:.....". On the breakpoint of the DestroyWindow function i get not messages or warnings. May be this helps to describe what i mean. regards, rommi

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Closing childwindow close also mainwindow

    Well, your problem is you are using the same window class for the main and for the child window. It means that the same window procedure is used for these both.
    But in this procedure you call PostQuitMessage in the case of WM_DESTROY! So handling this message for the child window your procedure causes the thread to exit!

    To fix the problem use another window class/another procedure (which won't call PostQuitMessage) for the child window.
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2009
    Posts
    9

    Re: Closing childwindow close also mainwindow

    Hi VictorN,
    this is the solution! thanks a lot!
    best regards, rommi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured