如何使用MFC在项目的所有对话框中访问变量? [英] how a variable can be access in all dialogs of a project using MFC?

查看:113
本文介绍了如何使用MFC在项目的所有对话框中访问变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个文件打开对话框,它包含一个编辑控件,其变量为"path",其中包含文件名.我想要的是在其他对话框中使用此变量的值,但是它给出了错误,即"path"是未声明的标识符.

  class  CAboutDlg: public  CDialog
{
公共:
    CAboutDlg();
    静态 CString imgname; 




在同一堂课中,我像这样使用它

 CString image = CAboutDlg :: imgname;
 CString szFilename(image); 



并通过此代码传递路径的值

 path = dlg.GetPathName();
UpdateData(FALSE);
CAboutDlg :: imgname = path; 



但是它仍然给出错误,在上面的代码中,CAboutDlg和imgname是未声明的标识符,我在其中传递路径值.我做了同样的事情,现在我从网站上学到了什么问题?:confused:

解决方案

您有几种选择可以做到这一点.
最简单但最丑陋的就是将您的路径声明为全局变量.
更好的方法是使用类似类的程序来包装程序的某些选项.然后,该类将被实例化为一个单例,程序的每个部分都可以访问该单例(有关更多信息,请单击Google的单例模式".).可以从该程序的特定部分完全访问CAboutDlg类.在MDI/SDI应用程序中,代码向导将CAboutDlg的声明(和实现)放入App .cpp文件中.您可能需要将类声明移到头文件(或新的头文件)中,并确保将其包含在要从中访问它的文件中.

虽然看起来有点丑陋.像Cedric建议的那样,创建一个设置类并传递它的实例.


一个简单的问题.答案实际上取决于应用程序的设置方式(如果这是基于对话框的应用程序),就像调用CEditControl :: GetWindowText()一样容易,您的EditControl显然需要一个控制变量才能使用GetWindowText()方法. ,只要您需要文字.

您始终可以创建一个全局变量,并通过创建事件处理程序来存储更改后的字符串.


这是MDI或SDI应用吗?在这个对话框不是主对话框/框架的情况下,我假设它是由调用函数创建和实例化的,并且仅存在于该函数的范围之内.因此,您需要在获得良好信息的同时获得信息.

在以下方面:

YourFunc()
{
yourDlgClass FileDLG;

if(FileDLG.doModal()== ID_OK)
{
YourGlobalString = FileDLG.imgname;
}
}


i have made a file open dialog, it contains an edit control whose variable is "path" that contains the file name. what i want is to use this variable''s value in other dialogs but it gives the error that "path" is an undeclard identifier.

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();
    static CString imgname;




in the same class, i used it like this

CString image=CAboutDlg::imgname;
 CString szFilename(image);



and passing value of path by this code

path=dlg.GetPathName();
UpdateData(FALSE);
CAboutDlg::imgname=path;



but it still gives error that CAboutDlg and imgname are undeclared identifier in the above code in which i m passing value of path. i did the same which i learned from the site now what''s wrong with that?:confused:

解决方案

You have several options for doing that.
The easiest but the ugliest would be to declare your path as a global variable.
A much better approach would be to have something like a class wrapping some options of your program. This class would then be instanciated as a singleton which would be accessible by every part of your program (google for "singleton pattern" for more information).


Sounds like your problem is that you cannot access the CAboutDlg class at all from that specific part of the program. In MDI/SDI applications the code wizard puts declaration (and implementation) of the CAboutDlg in the App .cpp file. You might need to move the class declaration to your header file (or a new header file) and make sure you include that in the file you want to access it from.

Seems a bit ugly though. Make a settings class and pass along an instance of it instead like Cedric suggested.


Simple enough question. The answer really depends on how the app is set up if this is a dialog based app this is as easy as calling the CEditControl::GetWindowText() ,your EditControl would obviously need a control variable to be able to use the GetWindowText() method, whenever you need the text.

You could always create a global variable and store the string when ever this is changed by creating an event handler.


Is this an MDI or SDI app? In the case that this is dialog is not the main dialog/frame I am assuming it is created and instantiated by a calling function and only exists within the scope of that function. So you need to get the information while the getting is good so to speak.

In the vein of:

YourFunc()
{
yourDlgClass FileDLG;

if(FileDLG.doModal() == ID_OK)
{
YourGlobalString = FileDLG.imgname;
}
}


这篇关于如何使用MFC在项目的所有对话框中访问变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆