Handling WM_CLOSE and WM_DESTROY

This is a discussion on Handling WM_CLOSE and WM_DESTROY within the Windows Programming forums, part of the Platform Specific Boards category; Some examples show these being handled like this: Code: case WM_CLOSE: DestroyWindow(hWnd); return 0; case WM_DESTROY: PostQuitMessage(WM_QUIT); return 0; Charles ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    439

    Handling WM_CLOSE and WM_DESTROY

    Some examples show these being handled like this:

    Code:
    case WM_CLOSE:
    	DestroyWindow(hWnd);
    	return 0;
    
    case WM_DESTROY:
    	PostQuitMessage(WM_QUIT);
    	return 0;
    Charles Petzold Shows it like this:

    Code:
    case WM_DESTROY:
    	PostQuitMessage(0);
    	return 0;
    not handling the WM_CLOSE message, only the WM_DESTROY.

    According to "Programming Windows", the WM_CLOSE is passed to the default window procedure,
    DefWindowProc, which results in the WM_DESTROY message being sent.

    Is there a reason to use the former example?

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    No. The only difference is the 1st sends 12 and the second sends 0.

    The param of PostQuitMessage() is only used if your message pump (Peek/GetMessage) actually looks at the wParam.

    IIRC technically you are required to return an int to the OS, zero for success or an application defined error code, so sending 12 (WM_QUIT == 0x0012) is not correct (if you actually use the wParam of the WM_QUIT msg in the msg pump).
    "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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The WM_CLOSE message (which is also sent by the X on the window frame) is intended to give you a chance to save settings etc. before exiting.

    Also note that if you return TRUE or 1, you can prevent the application from closing.

    Code:
    case WM_CLOSE :
      SaveSettings();
      PlayExitNoise();
      if (MessageBox(hWindMain,"Do you want to quit", "Exit", MB_YESNO | MB_ICONWARNING) == IDOK)
        { DestroyWindow(hWindMain);
          return 0; }
      else
        return 1;
    The WM_DESTROY message is the last thing on the way out the door, so that's where you PostQuitMessage()...

    For most programs PostQuitMessage(0) is perfectly adequate.
    Last edited by CommonTater; 09-27-2011 at 12:57 AM.

  4. #4
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    Code:
    case WM_DESTROY:
    	PostQuitMessage(WM_QUIT);
    	return 0;
    Not that it's technically wrong, but I guess tutorial writers go against the grain of 0 just so they can see who's copying the code.

    Quote Originally Posted by novacain View Post
    so sending 12 (WM_QUIT == 0x0012)
    That's 18. I'd go look at your contract, maybe they're not really paying you all that for MFC after all
    Last edited by adeyblue; 09-27-2011 at 05:05 PM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    439
    Ok, thanks.

    When reversing the "close window" process, by returning a '1' in response to a WM_CLOSE message,
    does the '1' itself have any signifigance or does it just have to be not zero?

    It works well, although it took me a little while to figure out that IDOK needs to be IDYES:

    Code:
    if (MessageBox(hWindMain,"Do you want to quit", "Exit", MB_YESNO | MB_ICONWARNING) == IDOK)

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sorry about IDYES mistake... did that from memory and I shouldn't. The return value is a signal for the default window process to stop processing the message... basically it amounts to TRUE and FALSE as used in DialogProc returns. I would imagine anything but a 0 would halt the process...

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Quote Originally Posted by adeyblue View Post
    That's 18. I'd go look at your contract, maybe they're not really paying you all that for MFC after all
    Typos, I test code for them, not forum posts.

    I investigated an incident where 2 3km long trains fitted with my prototype data recorders collided head on. It cost the company over Au$100 million in damage and lost production (it would have been about Au$30 mill less if the crane driver had not dropped one of the locos when putting them back on track).

    Turned out the cause was a typo, someone put a 2 (instead of a 3) in a track based transponder (that told the loco's Automated Train Protection system where it was on the track).
    "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

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    439
    Quote Originally Posted by CommonTater View Post
    Sorry about IDYES mistake... did that from memory and I shouldn't. The return value is a signal for the default window process to stop processing the message... basically it amounts to TRUE and FALSE as used in DialogProc returns. I would imagine anything but a 0 would halt the process...
    Oh, no problem.

    I use the MB_OK type message box so often for debugging, that I kept looking at the IDOK
    and seeing nothing wrong.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by megafiddle View Post
    Oh, no problem.

    I use the MB_OK type message box so often for debugging, that I kept looking at the IDOK
    and seeing nothing wrong.
    Same here... I wonder if it's possible to wear it out?

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    439

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Wm_close Wm_destroy Wm_quit
    By h_howee in forum Windows Programming
    Replies: 2
    Last Post: 09-24-2006, 02:11 PM
  3. Wm_close
    By franks in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2003, 03:30 PM
  4. Wm_close & Wm_destroy
    By Kelvin in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2002, 03:12 AM
  5. Disabling WM_Close
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 04-26-2002, 06:30 AM

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