MFC::使用结构传递数据

MFC :: passing data using structure

本文关键字:数据 MFC 结构      更新时间:2023-10-16

所以我有这个MFC对话框程序。对话框已经写好了,但现在我很难在对话框之间传递数据。我在从CWinApp派生的类中设置了以下结构_dlgDataHandler,并为指向该类型的指针创建了一个"new"语句。

//。。。。。。。SRK.h文件

class CSRK_App : public CWinApp
    {
public:
    CFSB_App();
     // added the following data structure for data passing withing the program
    typedef struct _dlgDataHandler {
      char RepetitionRadio[24];
          // another member
          // yet another member and so on as necessary
    } *dlgDataHandlerPtr;
      // extern dlgDataHandlerPtr dlgDataHandler;
// Overrides
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CSRK_App)
     public:
     virtual BOOL InitInstance();
     //}}AFX_VIRTUAL
// Implementation
    //{{AFX_MSG(CSRK_App)
    // NOTE - the ClassWizard will add and remove member functions here.
        //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
 }; 

//。。。。。。。SRK.cpp指向这个块中创建的新数据处理程序的指针,大约是的2/3

// CSRK_App initialization
BOOL CSRK_App::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
//  of your final executable, you should remove from the following
//  the specific initialization routines you do not need.
//SetRegistryKey(_T("Local AppWizard-Generated Aplications"));
#ifdef _AFXDLL
Enable3dControls();         // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic();   // Call this when linking to MFC statically
#endif
//CSRK_Dlg dlg;
CDialogMain dlg("SRK - Beta");    // added 12/27 **
m_pMainWnd = &dlg;
//const char* m_pszHelpFilePath = NULL;
//free((void*)m_pszHelpFilePath);
//m_pszHelpFilePath=_tcsdup(_T("c:SRKHelp.rtf"));
// the following line added to allocate memory for the structure
    dlgDataHandlerPtr dlgDataHandler = new _dlgDataHandler;
dlg.SetWizardMode();          // added 12/27 **
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
//  application, rather than start the application's message pump.
return FALSE;
}

在dialog.cpp文件中,有五个,我需要能够从AFX变量"m_"中获取数据,并将它们加载到这个dataHandler结构(或类似的另一个)中,以便它们可以在其他对话框和程序的部分中使用,特别是当所有对话框数据收集完成时,我的实际代码。有人说要使用AfxGetApp(),这样我就可以处理当前实例,但我不明白他们在说什么。是的,我在很多论坛上都读到过,但我就是不明白。我进一步意识到这可能不是最好的方法。我正在努力利用现有时间学习MFC/OOP,但目前,我只是想掌握基本过程,因为一旦我了解了如何收集和传递简单数据,我可以稍后对其进行调整。

我还不明白调用AfxGetApp()将如何帮助我了解CSRK_App的成员。它继承了CWinApps公共成员,但AfxGetapp()看不到CSRK_App有什么。。。能吗?

首先,解释您收到的AfxGetApp建议。使用"new"和指针会有一些额外的手势,但这基本上是为保存数据的结构使用全局变量。这不是做你想做的事情的最佳方式。有很多陷阱。

AfxGetApp()是一个MFC调用,它返回一个指向从CWinApp派生的主应用程序类的指针。如果您想使用返回的指针,您需要将其转换为CSRK_App*指针,其中包含:

CSRK_App* pApp = static_cast <CSRK_App*> ( AfxGetApp());

然后您可以使用pApp->dlgDataHandlerPtr->。。。以访问您需要的变量。

现在,针对陷阱。其他人可能会提出"new"和指针有用的原因,但与在CSRK_App类中只使用局部变量dlgDataHandler相比,我认为这种方法没有任何优势。这将简化代码。

下一个问题是,您的所有数据在结构中都是公共的。任何可以调用AfxGetApp的对话框类都可以读取或写入该结构中的任何数据。您无法控制访问。

此外,您的所有对话框类现在都必须包含SRK_App.h,以便它们了解结构,并可以访问该应用程序类中的所有其他变量。

一种更干净、面向对象的方法是在一个单独的.h文件中声明数据的结构(类),该文件可以包含在对话框类中。然后,将指向该数据的指针/引用传递到对话框类的构造函数中。对话框类将不需要知道任何关于应用程序类的信息。

对于更高级别的隔离,可以编写对话框类,以便它们在调用.DoModal()之前只获得传入的dlgDataHandler类的副本,然后在DoModal调用返回IDOK后,App类可以控制对话框中的哪些数据更新到dlgDataHandler类中。这种方法的优点是,无论对话框类是如何编程的,用户都可以在不修改任何数据的情况下"取消"对话框。