从 App 类访问 Dlg 类中的变量
我正在使用 MFC 为 GUI 编写一个基于对话框的 C++ 应用程序。创建项目后,Visual Studio 还创建了两个类 ProjectNameApp
和 ProjectNameDlg
。将菜单添加到我的应用程序时,菜单项处理程序(用于菜单按钮)将作为方法添加到 ProjectNameApp
类中。在这些处理程序之一中,我想访问 ProjectNameDlg
类的变量,更具体地说,是 CComboBox
。但这当然是不可能的。所以我有两个问题:
有没有办法从 Dlg 类访问
CComboBox
变量?如果没有,如何将菜单处理程序移动到 Dlg 类以直接使用
CComboBox
变量?
另外,我的应用程序必须基于对话框,并且必须有一个菜单。
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:
is there a way to acces that
CComboBox
variable from the Dlg class?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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最后一次处理这些问题是在 VS-2008 中,但如果内存可用,CDialog 对象可能会在 CProjectNameApp::InitInstance() 的堆栈上分配。那里可能有一些代码看起来有点像:
您可以做的一件事是添加一个指向对话框的指针作为 ProgramNameApp 类的成员。因此,在 ProgramNameApp.h 中添加一个数据元素,例如:
然后将 CProjectNameApp::InitInstance() 中的代码更改为:
当然,您必须留意
dlg
的任何其他用途并更改他们相应地。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:
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:
Then change the code in CProjectNameApp::InitInstance() to be:
Naturally you'd have to be on the lookout for any other uses of
dlg
and change them accordingly.