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. How can I post a user message from a Dialog box to the CDocument class?

How can I post a user message from a Dialog box to the CDocument class?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++css
7 Posts 3 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.
  • E Offline
    E Offline
    Electronic75
    wrote on last edited by
    #1

    Hi, I have a MFC Doc/View SDI application that in CDocument it opens a dialog box I need this dialog box to send and get data to the CDocument without halting the thread so I created a Mode less dialog box (CDialog m_dlgCalibrate),Now my problem is How can I send a message from this dialog box to CDocument class. In CDocument class that I create this calibration dialog box I've assigned its parent the CView associated with CDocument(I have only one view) This is the code

    void CMyDoc::OnCommandsCalibrate()
    {
    bool res;
    POSITION pos = GetFirstViewPosition();

    res = m_dlgCalibrate.Create(IDD_CALIBRATE, GetNextView(pos));
    if(!res)
    AfxMessageBox(_T("Error Creating Dialog Box"));
    m_dlgCalibrate.ShowWindow(SW_SHOW);
    }

    So when in this calibration dialog box I executed PostMessage(WM_CAL_COMMAND); I expected it is routed to CMyView I have put this in CMyView Message map ON_MESSAGE(WM_CAL_COMMAND, CMyView::OnNewCalCommand) I also have defined handler function as: afx_msg LRESULT OnNewCalCommand(WPARAM, LPARAM); but this message won't reach to CMyView let alone CMyDoc but I could grab it in CMainFrame class using similar code(I only executed AfxGetMainWnd()->PostMessage(WM_CAL_COMMAND) in dialog box in this case). Is there anyway so I can post this message to CMyDoc directly or at least to CMyView? Thanks

    B L 2 Replies Last reply
    0
    • E Electronic75

      Hi, I have a MFC Doc/View SDI application that in CDocument it opens a dialog box I need this dialog box to send and get data to the CDocument without halting the thread so I created a Mode less dialog box (CDialog m_dlgCalibrate),Now my problem is How can I send a message from this dialog box to CDocument class. In CDocument class that I create this calibration dialog box I've assigned its parent the CView associated with CDocument(I have only one view) This is the code

      void CMyDoc::OnCommandsCalibrate()
      {
      bool res;
      POSITION pos = GetFirstViewPosition();

      res = m_dlgCalibrate.Create(IDD_CALIBRATE, GetNextView(pos));
      if(!res)
      AfxMessageBox(_T("Error Creating Dialog Box"));
      m_dlgCalibrate.ShowWindow(SW_SHOW);
      }

      So when in this calibration dialog box I executed PostMessage(WM_CAL_COMMAND); I expected it is routed to CMyView I have put this in CMyView Message map ON_MESSAGE(WM_CAL_COMMAND, CMyView::OnNewCalCommand) I also have defined handler function as: afx_msg LRESULT OnNewCalCommand(WPARAM, LPARAM); but this message won't reach to CMyView let alone CMyDoc but I could grab it in CMainFrame class using similar code(I only executed AfxGetMainWnd()->PostMessage(WM_CAL_COMMAND) in dialog box in this case). Is there anyway so I can post this message to CMyDoc directly or at least to CMyView? Thanks

      B Offline
      B Offline
      Bram van Kampen
      wrote on last edited by
      #2

      Hi, Is there any chance of giving the DialogBox a reference to your CMyDoc and then executing m_pMyDoc->OnCalibrate() directly in your CMyDialog::OnOK(). You are trying to use the Framework Message Routing Mechanism which was designed to implement MFC. I'm not sure how it routes WM_USER+x Messages. From your experiment it occurs to me that the message ends up in the BitBucket somewhere along the way. Hope this is helpfull, Regards :)

      Bram van Kampen

      E 1 Reply Last reply
      0
      • B Bram van Kampen

        Hi, Is there any chance of giving the DialogBox a reference to your CMyDoc and then executing m_pMyDoc->OnCalibrate() directly in your CMyDialog::OnOK(). You are trying to use the Framework Message Routing Mechanism which was designed to implement MFC. I'm not sure how it routes WM_USER+x Messages. From your experiment it occurs to me that the message ends up in the BitBucket somewhere along the way. Hope this is helpfull, Regards :)

        Bram van Kampen

        E Offline
        E Offline
        Electronic75
        wrote on last edited by
        #3

        Hi, I tried but I couldn't because it requires that I have a member variable of dialog box in CDocument class, so I have to include CDialog header file in CDocument header file in that case I also need to define a member variable that points to CDocument so I also need to include CDocument header file in CDialog header file and this causes the compiler to cry loudly and gives hundreds of errors I even tried to prevent compiler form reading a header file recursively. like this:

        #ifndef __CDOCUMENT_H_
        #define __CDOCUMENT_H_
        .....//CDocument Header file
        #endif

        but again it wouldn't satisfy the compiler. Regards :) ,

        B 1 Reply Last reply
        0
        • E Electronic75

          Hi, I have a MFC Doc/View SDI application that in CDocument it opens a dialog box I need this dialog box to send and get data to the CDocument without halting the thread so I created a Mode less dialog box (CDialog m_dlgCalibrate),Now my problem is How can I send a message from this dialog box to CDocument class. In CDocument class that I create this calibration dialog box I've assigned its parent the CView associated with CDocument(I have only one view) This is the code

          void CMyDoc::OnCommandsCalibrate()
          {
          bool res;
          POSITION pos = GetFirstViewPosition();

          res = m_dlgCalibrate.Create(IDD_CALIBRATE, GetNextView(pos));
          if(!res)
          AfxMessageBox(_T("Error Creating Dialog Box"));
          m_dlgCalibrate.ShowWindow(SW_SHOW);
          }

          So when in this calibration dialog box I executed PostMessage(WM_CAL_COMMAND); I expected it is routed to CMyView I have put this in CMyView Message map ON_MESSAGE(WM_CAL_COMMAND, CMyView::OnNewCalCommand) I also have defined handler function as: afx_msg LRESULT OnNewCalCommand(WPARAM, LPARAM); but this message won't reach to CMyView let alone CMyDoc but I could grab it in CMainFrame class using similar code(I only executed AfxGetMainWnd()->PostMessage(WM_CAL_COMMAND) in dialog box in this case). Is there anyway so I can post this message to CMyDoc directly or at least to CMyView? Thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Whats wrong with doing this from your Dialog:CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd); CDocument * pDoc = pFrame->GetActiveDocument(); if(NULL != pDoc) { pDoc->SomeFunction(); }
          Best Wishes, -David Delaune

          E 1 Reply Last reply
          0
          • L Lost User

            Whats wrong with doing this from your Dialog:CFrameWnd * pFrame = (CFrameWnd *)(AfxGetApp()->m_pMainWnd); CDocument * pDoc = pFrame->GetActiveDocument(); if(NULL != pDoc) { pDoc->SomeFunction(); }
            Best Wishes, -David Delaune

            E Offline
            E Offline
            Electronic75
            wrote on last edited by
            #5

            Thanks a lot David, That solved my current problem but for future and just for knowledge purpose is there anyway to post a message directly to CDocument class from a dialog box I mean is there a way to override default message parsing scheme of MFC and route a message to a specific location? I read something about message reflection but as far as I understood that's most useful when one wants to propagate a message to child descendants and in my case can't do the trick how about overriding CCmdTaget? Thanks Best wishes,

            1 Reply Last reply
            0
            • E Electronic75

              Hi, I tried but I couldn't because it requires that I have a member variable of dialog box in CDocument class, so I have to include CDialog header file in CDocument header file in that case I also need to define a member variable that points to CDocument so I also need to include CDocument header file in CDialog header file and this causes the compiler to cry loudly and gives hundreds of errors I even tried to prevent compiler form reading a header file recursively. like this:

              #ifndef __CDOCUMENT_H_
              #define __CDOCUMENT_H_
              .....//CDocument Header file
              #endif

              but again it wouldn't satisfy the compiler. Regards :) ,

              B Offline
              B Offline
              Bram van Kampen
              wrote on last edited by
              #6

              Electronic75 wrote:

              Hi, I tried but I couldn't because it requires that I have a member variable of dialog box in CDocument class, so I have to include CDialog header file in CDocument header file in that case I also need to define a member variable that points to CDocument so I also need to include CDocument header file in CDialog header file and this causes the compiler to cry loudly and gives hundreds of errors I even tried to prevent compiler form reading a header file recursively. like this:

              Well, read up on your cpp. Ofcource the compiler (and largely the Framework) will object if you start adding items willy nilly to CDocument. After you eventually convince the compiler that way, the Linker will complain more loudly! (and should do so!) The crucial issue is that you have derrived a class CMyDocument, from CDocument. Again, Read up on CPP, Place your Dlg Header in 'MyDocument.h" and you can call it from CMyDocument (but NOT from CDocument except by virtual function.) Regards, :)

              Bram van Kampen

              E 1 Reply Last reply
              0
              • B Bram van Kampen

                Electronic75 wrote:

                Hi, I tried but I couldn't because it requires that I have a member variable of dialog box in CDocument class, so I have to include CDialog header file in CDocument header file in that case I also need to define a member variable that points to CDocument so I also need to include CDocument header file in CDialog header file and this causes the compiler to cry loudly and gives hundreds of errors I even tried to prevent compiler form reading a header file recursively. like this:

                Well, read up on your cpp. Ofcource the compiler (and largely the Framework) will object if you start adding items willy nilly to CDocument. After you eventually convince the compiler that way, the Linker will complain more loudly! (and should do so!) The crucial issue is that you have derrived a class CMyDocument, from CDocument. Again, Read up on CPP, Place your Dlg Header in 'MyDocument.h" and you can call it from CMyDocument (but NOT from CDocument except by virtual function.) Regards, :)

                Bram van Kampen

                E Offline
                E Offline
                Electronic75
                wrote on last edited by
                #7

                Hi, My mistake from CDocument and CDialog I meant a CDocument/CDialog derived class i.e. CMyDocument/CMyDialog :-O and the code was like this,

                #ifndef C_MY_DOC_H_
                #define C_MY_DOC_H_
                ...//CMyDoc.H body
                #endif

                Regards, :)

                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