Solved

DestroyWindow()

Posted on 1998-09-09
6
346 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.
0
Comment
Question by:Zainal062797
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1414293
This should not  be happening.  I suspect you are specifying a handle to the parent (application) window, not the child.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1414294
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
 

Author Comment

by:Zainal062797
ID: 1414295
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Accepted Solution

by:
danny_pav earned 50 total points
ID: 1414296
Because of the PostQuitMessage in your FireAndRescueWndProc
0
 

Author Comment

by:Zainal062797
ID: 1414297
Okay, then what I am suppose to do to in order to only kill the child window?

Thnaks.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1414298
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question