Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 390
  • Last Modified:

DestroyWindow()

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.
0
Zainal062797
Asked:
Zainal062797
  • 3
  • 2
1 Solution
 
nietodCommented:
This should not  be happening.  I suspect you are specifying a handle to the parent (application) window, not the child.
0
 
nietodCommented:
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.)
0
 
Zainal062797Author 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) ;
     }

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
danny_pavCommented:
Because of the PostQuitMessage in your FireAndRescueWndProc
0
 
Zainal062797Author Commented:
Okay, then what I am suppose to do to in order to only kill the child window?

Thnaks.
0
 
nietodCommented:
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.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now