开发者

Access variable in Dlg class from App class

开发者 https://www.devze.com 2023-03-04 16:59 出处:网络
I am writing a Dialog Based C++ application with MFC for the GUI. After the creation of the project, Visual Studio also created two classes ProjectNameApp and ProjectNameDlg. When adding a Menu to my

I am writing a Dialog Based C++ application with MFC for the GUI. After the creation of the project, Visual Studio also created two classes ProjectNameApp and ProjectNameDlg. When adding a Menu to my application the menu item handlers (for the menu buttons) are added as methods to the ProjectNameApp class. In one of these handlers I want to access a variable of the ProjectNameDlg class, more specific, a CComboBox. But that, of course, is not possible. So I have two questions:

  1. is there a way to acces that CComboBox varia开发者_如何转开发ble from the Dlg class?

  2. if not, how can I move the Menu handlers to the Dlg class to directly use the CComboBox variable?

Also, my application has to be dialog based, and it has to have a menu.


The last I dealt with any of this was in VS-2008, but if memory serves the CDialog object is probably allocated on the stack in CProjectNameApp::InitInstance(). There is probably some code there that looks kind of like:

CProjectNameDlg dlg;
int nResponse = dlg.DoModal();

One thing you could do is to add a pointer to the dialog as a member of the ProgramNameApp class. So in ProgramNameApp.h add a data element like:

std::tr1::unique_ptr<CProjectNameDlg> m_pDlg;

Then change the code in CProjectNameApp::InitInstance() to be:

m_pDlg = std::tr1::unique_ptr<CProjectNameDlg>(new CProjectNameDlg());
int nResponse = m_pDlg->DoModal();

Naturally you'd have to be on the lookout for any other uses of dlg and change them accordingly.

0

精彩评论

暂无评论...
验证码 换一张
取 消