How to access Mainfrm member variable status without using ((CMainFrame*) AfxGetMainWnd ())->...?

StackOverflow https://stackoverflow.com/questions/18057395

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

有帮助吗?

解决方案

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;
}

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top