How can I post a user message from a Dialog box to the CDocument class?
-
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 mapON_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 executedAfxGetMainWnd()->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 -
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 mapON_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 executedAfxGetMainWnd()->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? ThanksHi, 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
-
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
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
#endifbut again it wouldn't satisfy the compiler. Regards :) ,
-
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 mapON_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 executedAfxGetMainWnd()->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 -
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 DelauneThanks 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,
-
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
#endifbut again it wouldn't satisfy the compiler. Regards :) ,
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
-
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
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
#endifRegards, :)