Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC OnClose() behaviour

MFC OnClose() behaviour

Scheduled Pinned Locked Moved C / C++ / MFC
c++debugginghelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J B 0
    wrote on last edited by
    #1

    Hello guys, I'm wondering if anyone could help me to better undestand the exact OnClose() behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()) to a parent dialog's function, which leads OnClose(), the function looks like:

    LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam)
    {
    	OnClose();
    	return 0;
    }
    

    I need to do some clean-up before the application is closed, so I override the OnClose() function:

    void ParentDlg::OnClose()
    {
    	DoSomeCleanUp();
    	CDialog::OnClose();	// call base class handler
    }
    

    MSDN says OnClose() that its default implementation calls DestroyWindow. So I call the base class handler CDialog::OnClose() at the end of overriden OnClose() and assumes that should call DestroyWindow(), which leads to ParentDlg::OnDestroy() I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes through OnExitApp(), OnClose(), then OnDestroy() never gets called after and the program will not exit. Why is the behaviour different when both call CDialog::OnClose() eventually? I later simply insert the code DestroyWindow(); to the OnClose() function and that made both call OnDestroy() and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ Thanks

    B D D 3 Replies Last reply
    0
    • J J B 0

      Hello guys, I'm wondering if anyone could help me to better undestand the exact OnClose() behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()) to a parent dialog's function, which leads OnClose(), the function looks like:

      LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam)
      {
      	OnClose();
      	return 0;
      }
      

      I need to do some clean-up before the application is closed, so I override the OnClose() function:

      void ParentDlg::OnClose()
      {
      	DoSomeCleanUp();
      	CDialog::OnClose();	// call base class handler
      }
      

      MSDN says OnClose() that its default implementation calls DestroyWindow. So I call the base class handler CDialog::OnClose() at the end of overriden OnClose() and assumes that should call DestroyWindow(), which leads to ParentDlg::OnDestroy() I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes through OnExitApp(), OnClose(), then OnDestroy() never gets called after and the program will not exit. Why is the behaviour different when both call CDialog::OnClose() eventually? I later simply insert the code DestroyWindow(); to the OnClose() function and that made both call OnDestroy() and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ Thanks

      B Offline
      B Offline
      Bob Stanneveld
      wrote on last edited by
      #2

      Hi, When you push the X button in an app, some messages are send to your app. One of those messages is the WM_DESTROY message, which doesn't get send when you push the Exit button in your dialog. One solution is (only when it is a modal dialog) to use the OnOK (or OnCancel) message handler. This will terminate the dialog and return the DoModal funtion will return a value. Hope this helps.

      A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

      1 Reply Last reply
      0
      • J J B 0

        Hello guys, I'm wondering if anyone could help me to better undestand the exact OnClose() behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()) to a parent dialog's function, which leads OnClose(), the function looks like:

        LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam)
        {
        	OnClose();
        	return 0;
        }
        

        I need to do some clean-up before the application is closed, so I override the OnClose() function:

        void ParentDlg::OnClose()
        {
        	DoSomeCleanUp();
        	CDialog::OnClose();	// call base class handler
        }
        

        MSDN says OnClose() that its default implementation calls DestroyWindow. So I call the base class handler CDialog::OnClose() at the end of overriden OnClose() and assumes that should call DestroyWindow(), which leads to ParentDlg::OnDestroy() I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes through OnExitApp(), OnClose(), then OnDestroy() never gets called after and the program will not exit. Why is the behaviour different when both call CDialog::OnClose() eventually? I later simply insert the code DestroyWindow(); to the OnClose() function and that made both call OnDestroy() and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ Thanks

        D Offline
        D Offline
        Diddy
        wrote on last edited by
        #3

        LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam){ OnClose(); return 0; } Is why. Change it to LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam){ SendMessage(WM_CLOSE); return 0; } When CDialog::OnClose gets called, it calls MFC's Default() method - which says hay what was the last message? Lets pass that to DefWindowProc for processing. If you just _call_ OnClose, the last message will be your custom message you are using to close the dialog - and DefWindowProc will do nowt. Better yet - do away with your custom message and just send the dialog a WM_CLOSE from the other dialog.

        1 Reply Last reply
        0
        • J J B 0

          Hello guys, I'm wondering if anyone could help me to better undestand the exact OnClose() behaviour. To start clearly, I have an dialog-based application that has a child dialog with a Exit button. When the button is clicked, the child dialog can therefore notify to close the application by sending a message (SendMessage()) to a parent dialog's function, which leads OnClose(), the function looks like:

          LRESULT ParentDlg::OnExitApp(WPARAM wParam, LPARAM lParam)
          {
          	OnClose();
          	return 0;
          }
          

          I need to do some clean-up before the application is closed, so I override the OnClose() function:

          void ParentDlg::OnClose()
          {
          	DoSomeCleanUp();
          	CDialog::OnClose();	// call base class handler
          }
          

          MSDN says OnClose() that its default implementation calls DestroyWindow. So I call the base class handler CDialog::OnClose() at the end of overriden OnClose() and assumes that should call DestroyWindow(), which leads to ParentDlg::OnDestroy() I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). However, if it is to close via the Exit button in the child dialog, which goes through OnExitApp(), OnClose(), then OnDestroy() never gets called after and the program will not exit. Why is the behaviour different when both call CDialog::OnClose() eventually? I later simply insert the code DestroyWindow(); to the OnClose() function and that made both call OnDestroy() and both now exit the program. However here that produces another different behaviour. The program return code is now code 0 (0x0). Can anyone explain? Sorry for this lenthy post :~ Thanks

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          J.B. wrote: I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). Not strange at all. This is the expected return value when the "X" is clicked. If this is a modal dialog, look at the EndDialog() function. It gets called whenever a modal dialog is dismissed. Alt+F4, Cancel, and the "X" all effectively call EndDialog(2). This value, in turn, gets passed back through DoModal(). Had you dismissed the dialog using the OK button, EndDialog(1) would have been called instead.


          "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

          J 1 Reply Last reply
          0
          • D David Crow

            J.B. wrote: I run in debug mode and found that, if the application is to close by clicking on the "X" of the application, fair enough, OnClose() is called and it goes to OnDestroy() and the program exits (with return code code 2 (0x2) -- strange??). Not strange at all. This is the expected return value when the "X" is clicked. If this is a modal dialog, look at the EndDialog() function. It gets called whenever a modal dialog is dismissed. Alt+F4, Cancel, and the "X" all effectively call EndDialog(2). This value, in turn, gets passed back through DoModal(). Had you dismissed the dialog using the OK button, EndDialog(1) would have been called instead.


            "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

            J Offline
            J Offline
            J B 0
            wrote on last edited by
            #5

            Thanks alot guys, it all makes sense now :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups