2012-01-21 27 views
0

所以我有這個MFC對話框程序,我正在使用。對話框被寫入,但現在我很難從對話框向對話框傳遞數據。我有以下結構_dlgDataHandler建立在從CWinApp派生的類中,並且已經爲指向此類型的指針創建了一個「new」語句。MFC ::使用結構傳遞數據

//.......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一個指向該塊創建了一個新的DataHandler大約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; 
} 

在對話框的.cpp文件的方式,有五個,我需要能夠從AFX變量「M_」獲取數據並將其加載到該DataHandler的結構(或另一個喜歡它)以便它們可以用於其他對話框和程序的某些部分,特別是當所有對話框數據收集完成時我的實際代碼。有人說使用AfxGetApp(),這樣我就可以掌握當前的實例,但我不明白他們在說什麼。是的,我已經在很多論壇上看過它,我只是不明白。我進一步認識到這可能不是最好的方式。我想用我現有的時間學習MFC/OOP,但現在,我只是試圖掌握基本過程,因爲稍後我會理解如何收集和傳遞簡單的數據。

我進一步不明白如何調用AfxGetApp()將幫助我處理CSRK_App的成員。它繼承了CWinApps的公共成員,但AfxGetapp()無法看到CSRK_App有哪些...可以嗎?

回答

3

首先,解釋您收到的AfxGetApp建議。使用'new'和一個指針還有一些額外的手勢,但這基本上是使用一個全局變量來存儲數據。這不是做你正在嘗試做的最好的方式。有許多陷阱。

AfxGetApp()是一個MFC調用,它返回一個指向從CWinApp派生的主App類的指針。 如果你想使用返回的指針,你需要用強制轉換爲一個CSRK_App *指針:

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

然後你可以使用PAPP-> dlgDataHandlerPtr - > ...來訪問你需要的變量。

現在,爲陷阱。其他人可能會以「new」和指針有幫助爲理由,但我認爲這種方法沒有任何優勢,因爲與CSRK_App類中的局部變量dlgDataHandler相比,這種方法沒有什麼優勢。這將簡化代碼。

下一個問題是你的所有數據在一個結構中是公共的。任何可以調用AfxGetApp的對話框類都可以讀取或寫入該結構中的任何數據。您無法控制訪問權限。另外,所有的對話框類都必須包含SRK_App.h,以便他們知道結構,並且可以訪問該App類中的所有其他變量。

更清晰的面向對象的方法是在可以包含在對話框類中的單獨的.h文件中聲明數據的結構(類)。然後,您可以將對這些數據的指針/引用傳遞給對話類的構造函數。對話類不需要知道關於App類的任何信息。

對於更高級別的隔離,可以編寫對話框類,以便它們只在調用之前獲得傳入的dlgDataHandler類的副本。DoModal(),然後在DoModal調用返回IDOK後,App類可以控制來自對話框的哪些數據更新到dlgDataHandler類中。這種方法的優點是可以確保無論程序如何編程,用戶都可以在不修改任何數據的情況下「取消」對話框。

+0

謝謝您的輸入。我意識到這是一個糟糕的做法。我實際上試圖按照您所描述的方式在其自己的文件中使用另一個類。我最大的問題就是理解在哪裏放置類實例化的東西,以便它始終處於運行狀態。我似乎永遠無法保持對它的處理。就像當我從一個對話框移動到另一個對話框時,我失去了數據類的範圍,當我試圖回到它時,它保存了垃圾或其他東西。所以然後我嘗試了可疑的結構。對我來說最困難的事情是知道在MFC中所有的東西都應該放在哪裏 –