具有结构成员变量的关联对话框控件
Associated dialog controls with a structure member variables
我有这个struct
定义:
typedef struct tagPublicTalkInfo
{
CString strAwayCong1;
CString strAwayCong2;
CString strAwayBrother1;
CString strAwayBrother2;
CString strChairman;
CString strCong;
CString strHosp;
CString strReader;
CString strBrother;
CString strTheme;
CString strWTConductor;
CString strInterpreter;
CString strMisc;
CString strWatchtowerStudyTheme;
CString strServiceTalkTheme;
CString strBibleVersesReader;
CString strVideoHost;
CString strVideoCohost;
CString strOpeningPrayer;
CString strClosingPrayer;
int iSongStart;
int iSongMiddle;
int iSongEnd;
int iThemeNumber;
} S_TALK_INFO;
此结构被传递到对话框中并像这样检索:
void CWeekendMeetingDlg::SetWeekendMeetingInfo(S_TALK_INFO &rsTalkInfo)
{
m_sWeekendMeetingInfo = rsTalkInfo;
}
S_TALK_INFO CWeekendMeetingDlg::GetWeekendMeetingInfo()
{
return m_sWeekendMeetingInfo;
}
目前我正在将对话框控件映射到不同的变量并传输到/从我的结构。例如:
m_strChairman = m_sWeekendMeetingInfo.strChairman;
m_sWeekendMeetingInfo.strChairman = m_strChairman;
是否可以将我的控件直接映射到结构成员 CString
变量,或者这会被视为不好的做法吗?
是的,正如 Adrian 所说,您不必局限于使用向导提供给您的变量。我用我的测试项目通过关于对话框来做到这一点。
struct data_type {
CString a_data;
CString b_data;
};
class CAboutDlg : public CDialogEx
{
data_type& dlg_data;
public:
CAboutDlg(data_type&);
//CString dummy;
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
protected:
DECLARE_MESSAGE_MAP()
public:
};
CAboutDlg::CAboutDlg(data_type& data) : CDialogEx(IDD_ABOUTBOX), dlg_data(data)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1_ABOUT, dlg_data.a_data);
DDX_Text(pDX, IDC_EDIT2_ABOUT, dlg_data.b_data);
}
// App command to run the dialog
void CMFCApplicationApp::OnAppAbout()
{
data_type data{ L"this", L"that" };
CAboutDlg aboutDlg(data);
aboutDlg.DoModal();
assert(false);
}
这里我传递了对外部数据的引用以直接对其进行处理。由于 DoDataExchange
机制,只有当用户按下 OK 时,数据才会被修改。
我使用向导添加了第一个 DDX_Text
处理程序,因此注释值 dummy
。我刚刚复制并粘贴了该处理程序以添加第二个。
我将断言放在 app 命令中,这样程序就会中断并可以检查数据。
我有这个struct
定义:
typedef struct tagPublicTalkInfo
{
CString strAwayCong1;
CString strAwayCong2;
CString strAwayBrother1;
CString strAwayBrother2;
CString strChairman;
CString strCong;
CString strHosp;
CString strReader;
CString strBrother;
CString strTheme;
CString strWTConductor;
CString strInterpreter;
CString strMisc;
CString strWatchtowerStudyTheme;
CString strServiceTalkTheme;
CString strBibleVersesReader;
CString strVideoHost;
CString strVideoCohost;
CString strOpeningPrayer;
CString strClosingPrayer;
int iSongStart;
int iSongMiddle;
int iSongEnd;
int iThemeNumber;
} S_TALK_INFO;
此结构被传递到对话框中并像这样检索:
void CWeekendMeetingDlg::SetWeekendMeetingInfo(S_TALK_INFO &rsTalkInfo)
{
m_sWeekendMeetingInfo = rsTalkInfo;
}
S_TALK_INFO CWeekendMeetingDlg::GetWeekendMeetingInfo()
{
return m_sWeekendMeetingInfo;
}
目前我正在将对话框控件映射到不同的变量并传输到/从我的结构。例如:
m_strChairman = m_sWeekendMeetingInfo.strChairman;
m_sWeekendMeetingInfo.strChairman = m_strChairman;
是否可以将我的控件直接映射到结构成员 CString
变量,或者这会被视为不好的做法吗?
是的,正如 Adrian 所说,您不必局限于使用向导提供给您的变量。我用我的测试项目通过关于对话框来做到这一点。
struct data_type {
CString a_data;
CString b_data;
};
class CAboutDlg : public CDialogEx
{
data_type& dlg_data;
public:
CAboutDlg(data_type&);
//CString dummy;
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
protected:
DECLARE_MESSAGE_MAP()
public:
};
CAboutDlg::CAboutDlg(data_type& data) : CDialogEx(IDD_ABOUTBOX), dlg_data(data)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1_ABOUT, dlg_data.a_data);
DDX_Text(pDX, IDC_EDIT2_ABOUT, dlg_data.b_data);
}
// App command to run the dialog
void CMFCApplicationApp::OnAppAbout()
{
data_type data{ L"this", L"that" };
CAboutDlg aboutDlg(data);
aboutDlg.DoModal();
assert(false);
}
这里我传递了对外部数据的引用以直接对其进行处理。由于 DoDataExchange
机制,只有当用户按下 OK 时,数据才会被修改。
我使用向导添加了第一个 DDX_Text
处理程序,因此注释值 dummy
。我刚刚复制并粘贴了该处理程序以添加第二个。
我将断言放在 app 命令中,这样程序就会中断并可以检查数据。