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. PostNcDestroy problems

PostNcDestroy problems

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 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.
  • A Offline
    A Offline
    andyg 101
    wrote on last edited by
    #1

    Hi there, I am having trouble with my dialogues using the cross X in the corner of my dialogue doesn't seem to call PostNcDestroy where I perform clean up of the object. I was expecting the WM_CLOSE message to be generated which calls the default DestroyWindow. Can anyone explain why it is not being called? Andy

    D J 2 Replies Last reply
    0
    • A andyg 101

      Hi there, I am having trouble with my dialogues using the cross X in the corner of my dialogue doesn't seem to call PostNcDestroy where I perform clean up of the object. I was expecting the WM_CLOSE message to be generated which calls the default DestroyWindow. Can anyone explain why it is not being called? Andy

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

      Interesting. I have a modeless dialog I use in a little app that simply shows a progress bar while the app is running. When I click the 'X', it indeed calls the dialog's PostNcDestroy() function.

      1 Reply Last reply
      0
      • A andyg 101

        Hi there, I am having trouble with my dialogues using the cross X in the corner of my dialogue doesn't seem to call PostNcDestroy where I perform clean up of the object. I was expecting the WM_CLOSE message to be generated which calls the default DestroyWindow. Can anyone explain why it is not being called? Andy

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #3

        I don't think it is called in modal dialogs. John

        A 1 Reply Last reply
        0
        • J John M Drescher

          I don't think it is called in modal dialogs. John

          A Offline
          A Offline
          andyg 101
          wrote on last edited by
          #4

          I don't understand it, I stepped through some of the example code in Nishants tutorial and closing the dialogue with the X definitely calls PostNcDestroy, however it doesn't in mine, despite the fact that clicking the X sends the WM_CLOSE message, which I think should call destroy window. I'm confused, the only difference I can see in my code is that I call ShowWindow in the constructor of my dialogue, and not in the external code. ?? Andy :-( Still confused

          J 1 Reply Last reply
          0
          • A andyg 101

            I don't understand it, I stepped through some of the example code in Nishants tutorial and closing the dialogue with the X definitely calls PostNcDestroy, however it doesn't in mine, despite the fact that clicking the X sends the WM_CLOSE message, which I think should call destroy window. I'm confused, the only difference I can see in my code is that I call ShowWindow in the constructor of my dialogue, and not in the external code. ?? Andy :-( Still confused

            J Offline
            J Offline
            John M Drescher
            wrote on last edited by
            #5

            Is your dialog modeless then? John

            A 1 Reply Last reply
            0
            • J John M Drescher

              Is your dialog modeless then? John

              A Offline
              A Offline
              andyg 101
              wrote on last edited by
              #6

              It's definetly modeless, I create a new dialogue like this

              if(!m_pDlgComms) // check to see if the dialogue exists
              {
              m_pDlgComms = new CCommsDlg(this);

              etc which is then constructed like this

              CCommsDlg::CCommsDlg(CWnd* pParent /*=NULL*/)
              : CDialog(CCommsDlg::IDD, pParent)
              {
              //{{AFX_DATA_INIT(CCommsDlg)
              m_sCommsInput = _T("");
              m_sCommsOutput = _T("");
              //}}AFX_DATA_INIT
              if (Create(CCommsDlg::IDD, pParent))
              {
              m_pParent = pParent;
              ShowWindow(SW_SHOW);
              }
              }

              and here is the PostNcDestroy which is called whenever I force a destroy window using a button for example

              void CCommsDlg::PostNcDestroy()
              {
              // TODO: Add your specialized code here and/or call the base class
              CDialog::PostNcDestroy();
              if(m_pParent)
              {

              	((CBuggycontrolappView\*)m\_pParent)->m\_pDlgComms			= NULL;
              	((CBuggycontrolappView\*)m\_pParent)->m\_nCommsToggleState = 0;
              }
              delete this;
              

              }

              but not when I use the cross. Even though there is a WM_CLOSE message Andy

              J T 2 Replies Last reply
              0
              • A andyg 101

                It's definetly modeless, I create a new dialogue like this

                if(!m_pDlgComms) // check to see if the dialogue exists
                {
                m_pDlgComms = new CCommsDlg(this);

                etc which is then constructed like this

                CCommsDlg::CCommsDlg(CWnd* pParent /*=NULL*/)
                : CDialog(CCommsDlg::IDD, pParent)
                {
                //{{AFX_DATA_INIT(CCommsDlg)
                m_sCommsInput = _T("");
                m_sCommsOutput = _T("");
                //}}AFX_DATA_INIT
                if (Create(CCommsDlg::IDD, pParent))
                {
                m_pParent = pParent;
                ShowWindow(SW_SHOW);
                }
                }

                and here is the PostNcDestroy which is called whenever I force a destroy window using a button for example

                void CCommsDlg::PostNcDestroy()
                {
                // TODO: Add your specialized code here and/or call the base class
                CDialog::PostNcDestroy();
                if(m_pParent)
                {

                	((CBuggycontrolappView\*)m\_pParent)->m\_pDlgComms			= NULL;
                	((CBuggycontrolappView\*)m\_pParent)->m\_nCommsToggleState = 0;
                }
                delete this;
                

                }

                but not when I use the cross. Even though there is a WM_CLOSE message Andy

                J Offline
                J Offline
                John M Drescher
                wrote on last edited by
                #7

                Ok, I understand your problem but I have not seen this in the past. Do you have code for OnCancel() ? Destroying the window with the X button should call OnCancel(). John

                1 Reply Last reply
                0
                • A andyg 101

                  It's definetly modeless, I create a new dialogue like this

                  if(!m_pDlgComms) // check to see if the dialogue exists
                  {
                  m_pDlgComms = new CCommsDlg(this);

                  etc which is then constructed like this

                  CCommsDlg::CCommsDlg(CWnd* pParent /*=NULL*/)
                  : CDialog(CCommsDlg::IDD, pParent)
                  {
                  //{{AFX_DATA_INIT(CCommsDlg)
                  m_sCommsInput = _T("");
                  m_sCommsOutput = _T("");
                  //}}AFX_DATA_INIT
                  if (Create(CCommsDlg::IDD, pParent))
                  {
                  m_pParent = pParent;
                  ShowWindow(SW_SHOW);
                  }
                  }

                  and here is the PostNcDestroy which is called whenever I force a destroy window using a button for example

                  void CCommsDlg::PostNcDestroy()
                  {
                  // TODO: Add your specialized code here and/or call the base class
                  CDialog::PostNcDestroy();
                  if(m_pParent)
                  {

                  	((CBuggycontrolappView\*)m\_pParent)->m\_pDlgComms			= NULL;
                  	((CBuggycontrolappView\*)m\_pParent)->m\_nCommsToggleState = 0;
                  }
                  delete this;
                  

                  }

                  but not when I use the cross. Even though there is a WM_CLOSE message Andy

                  T Offline
                  T Offline
                  Toni78
                  wrote on last edited by
                  #8

                  When WM_CLOSE is sent, you need to send call DestroyWindow() For example:

                  CCommsDlg::OnClose()
                  {
                  DestroyWindow();
                  }

                  This is how you close a modeless dialog. When you call DestroyWindow() then of course you will get to PostNcDestroy(). // Afterall I realized that even my comment lines have bugs

                  A 1 Reply Last reply
                  0
                  • T Toni78

                    When WM_CLOSE is sent, you need to send call DestroyWindow() For example:

                    CCommsDlg::OnClose()
                    {
                    DestroyWindow();
                    }

                    This is how you close a modeless dialog. When you call DestroyWindow() then of course you will get to PostNcDestroy(). // Afterall I realized that even my comment lines have bugs

                    A Offline
                    A Offline
                    andyg 101
                    wrote on last edited by
                    #9

                    Hello again guys I've solved the problem I think. I put in the handler for WM_CLOSE, and the following code

                    void CCommsDlg::OnClose()
                    {
                    CDialog::OnClose();

                    DestroyWindow();
                    

                    }

                    This now works fine, doing exactly what I want. Previously the code I had used was

                    void CCommsDlg::OnClose()
                    {
                    DestroyWindow();

                    CDialog::OnClose();
                    

                    }

                    which gave me the exception. I'm still not sure why though no doubt one of you guys can inform me and I'm still puzzled by the fact that the code given in Nishants example in the dialogues section makes no call to OnClose at all and still calls PostNcDestroy. However now my code works I'm a happy man :-) though it'd be better if I understood why! Cheers Andy

                    T 1 Reply Last reply
                    0
                    • A andyg 101

                      Hello again guys I've solved the problem I think. I put in the handler for WM_CLOSE, and the following code

                      void CCommsDlg::OnClose()
                      {
                      CDialog::OnClose();

                      DestroyWindow();
                      

                      }

                      This now works fine, doing exactly what I want. Previously the code I had used was

                      void CCommsDlg::OnClose()
                      {
                      DestroyWindow();

                      CDialog::OnClose();
                      

                      }

                      which gave me the exception. I'm still not sure why though no doubt one of you guys can inform me and I'm still puzzled by the fact that the code given in Nishants example in the dialogues section makes no call to OnClose at all and still calls PostNcDestroy. However now my code works I'm a happy man :-) though it'd be better if I understood why! Cheers Andy

                      T Offline
                      T Offline
                      Toni78
                      wrote on last edited by
                      #10

                      As Nishant explains in his article a modal dialog makes a call to DestroyWindow() and as a result to PostNcDestroy(). But in your example you are building a modeless dialog and Windows is very specific about this case. You need to call DestroyWindow() to close a modeless dialog. Why? I don't know. This decision was made by windows. I will do a little bit of investigating though to find out. However, in this case OnClose() doesn't generate an WM_DESTROY message afterwards. And by the way this part of your code DestroyWindow(); CDialog::OnClose(); generates an exception because you are sending a WM_CLOSE message to a window that doesn't exist. // Afterall I realized that even my comment lines have bugs

                      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