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