1

I have a MDI appliation in MFC to modify. I want to check the value of a flag which is a member variable of MainFrm from a lower level class. But I don't want to access it using '((CMainFrame*) AfxGetMainWnd ())->IsFlagOn()' kind of function because for that i have to give the mainfrm.h in a lower level class. I somehow feel this will create some circular reference later, after reading this Why are circular references considered harmful? what are the other ways to get the flag value from mainfrm class.Please guide !

note: here class hierarchy is mainfrm->CTestExplorerFrame->CTestExplorerView->CTestExplorerTreeCtrl I want to check from the lowest level about a flag that is only accessed by mainfrm

Community
  • 1
  • 1
Codename_DJ
  • 553
  • 1
  • 11
  • 40

2 Answers2

2

AfxGetMainWnd() returns a CWnd* that you can use to communicate with the mainframe via the Windows message system. Define a custom message and send this message to the CWnd*

#define UWM_MYREQUEST (WM_APP + 2)

int datatoget;
CWnd* pMainframe = AfxGetMainWnd();
pMainframe->SendMessage(UWM_MYREQUEST, (WPARAM)&datatoget, 0);

The mainframe needs code like this to receive and handle the custom message:

ON_MESSAGE(UWM_MYREQUEST, OnMyRequest)

LRESULT CMainFrame::OnMyRequest(WPARAM wparam, LPARAM lparam)
{
 int* ptoget = (int*)wparam;
 *ptoget = m_datarequested;
  return 0;
}
ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
  • This is exactly what I wanted . I thought about sending custom message but I totally forgot that we can also get value through it.Thanks ScottMcP-MVP – Codename_DJ Aug 06 '13 at 07:15
  • 1
    It is up to you what solution you take but the approach using a windows message is not really very efficient, quite slow compared to a direct call and you need to define an own message and message handler for each data member you want to read. In addition it is not type safe and you are using Windows specific code :) – Clemens Aug 06 '13 at 08:19
  • Ya, that's totally true @Clemens. I will obviously think about this point. Thanks. – Codename_DJ Aug 06 '13 at 08:48
0

I would declare an (pure virtual) interface class where you have a pure virtual call to get the value of the flag you are interested in at CTestExplorerTreeCtrl. Then the MainFrame implements this interface class and passes a pointer to CTestExplorerTreeCtrl. This way you can avoid any references to the MainFrame class.

Clemens
  • 1,744
  • 11
  • 20