We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

DestroyWindow()

Zainal062797
Zainal062797 asked
on
Medium Priority
468 Views
Last Modified: 2013-12-03
According to the documentaions, DestroyWindow() is suppose to kill the window I spicify by its handle. However, I am getting a different result. When using DestroyWindow() to destroy a child window, the function terminates the application as well (the parent). I don't know why this is happening? Is this a bug?! Or am I missing something? Please advise?

Thank you.
Comment
Watch Question

Commented:
This should not  be happening.  I suspect you are specifying a handle to the parent (application) window, not the child.

Commented:
How are you getting the handle?  Have you been able to confirm that it is indeed the right handle?

Another posibility is that the child window's window procedure has code that terminates the application or closes the parent when it closed.  A similar possibility is that the parent window's window procedure has code that closes the window or terminates the application when the child window is closed.  

If these windows belong to an application written by someone else, these are possibilities.  If you wrote the application in question, then this is not likely (or you would know what is going on and wouldn't have asked.)

Author

Commented:
This is the code I am using. I wrote it all. It is so simple (for testing). Try to complie it and see for yourself.
===============================

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

LRESULT CALLBACK FireAndRescueWndProc (HWND hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
     switch (iMsg)
          {

          case WM_DESTROY :

               PostQuitMessage (0) ;

               return(0) ;
            }


     return DefWindowProc (hWnd, iMsg, wParam, lParam) ;

}  // end of  FireAndRescueWndProc()




BOOL RegisterFireAndRescueClass()
 {
     WNDCLASSEX   wndclass ;


     wndclass.cbSize        = sizeof (wndclass) ;
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = FireAndRescueWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = NULL;  
     wndclass.hIcon         = NULL ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (DKGRAY_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = "FireAndRescue" ;
     wndclass.hIconSm       = NULL ;

     return (RegisterClassEx (&wndclass)) ;

}  // end of  RegisterFireAndRescueClass()




int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
     {
     static char szAppName[] = "HelloWin" ;
     HWND        hwnd ;
     MSG         msg ;
     WNDCLASSEX  wndclass ;
      

     wndclass.cbSize        = sizeof (wndclass) ;
     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 ;
     wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;

     RegisterClassEx (&wndclass) ;
      RegisterFireAndRescueClass() ;

     hwnd = CreateWindow (szAppName,         // window class name
                        "The Hello Program",     // window caption
                    WS_OVERLAPPEDWINDOW,     // window style
                    CW_USEDEFAULT,           // initial x position
                    CW_USEDEFAULT,           // initial y position
                    CW_USEDEFAULT,           // initial x size
                    CW_USEDEFAULT,           // initial y size
                    NULL,                    // parent window handle
                    NULL,                    // window menu handle
                    hInstance,               // program instance handle
                        NULL) ;                         // creation parameters

     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;





     while (GetMessage (&msg, NULL, 0, 0))
          {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
          }
     return msg.wParam ;
     }





LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
     {
      static HINSTANCE            hInstance ;
     HDC                              hdc ;
     PAINTSTRUCT                  ps ;
     RECT                              rect ;
      static HWND                  hFireAndRescueWnd ;
      static HWND                  hButton ;



     switch (iMsg)
          {
          case WM_CREATE :
                  hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
               // PlaySound ("hellowin.wav", NULL, SND_FILENAME | SND_ASYNC) ;
                  
                  // create the fire and rescue window
                  hFireAndRescueWnd = CreateWindow ("FireAndRescue", "",
                                                            WS_CHILD | WS_BORDER | WS_VISIBLE,
                                                            20, 20, 100, 100,
                                                            (hwnd), NULL, hInstance, NULL) ;

                  hButton = CreateWindow ("BUTTON", "Kill Window",
                                                            WS_CHILD | WS_BORDER | WS_VISIBLE,
                                                            200, 20, 100, 100,
                                                            (hwnd), 1, hInstance, NULL) ;

               return 0 ;


          case WM_PAINT :
                 hdc = BeginPaint (hwnd, &ps) ;

               GetClientRect (hwnd, &rect) ;

               DrawText (hdc, "Hello, Windows 95!", -1, &rect,
                               DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

                EndPaint (hwnd, &ps) ;

               return 0 ;


          case WM_COMMAND :
                  switch (LOWORD (wParam))
                  {
                        case 1 :

                              MessageBeep(0) ;
                              DestroyWindow (hFireAndRescueWnd) ;

                              return(0) ;
                  }

                  return(0) ;


          case WM_DESTROY :
               PostQuitMessage (0) ;
               return 0 ;
          }

     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
     }

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Okay, then what I am suppose to do to in order to only kill the child window?

Thnaks.

Commented:
The child window procedure should not call PostQuitMessage() in the WM_DESTROY handler.  Only the parent window should do that (in most cases).  Calling PostQuiteMessage() will cause your application to termiante.  You want to do that only when the parent is closed, not he child.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.