简体   繁体   English

与结构成员变量关联的对话框控件

[英]Associated dialog controls with a structure member variables

I have this struct definition:我有这个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;

This structure is passed into a dialog and retrieved like this:这个结构被传递到一个对话框中并像这样检索:

void CWeekendMeetingDlg::SetWeekendMeetingInfo(S_TALK_INFO &rsTalkInfo)
{
    m_sWeekendMeetingInfo = rsTalkInfo;
}

S_TALK_INFO CWeekendMeetingDlg::GetWeekendMeetingInfo()
{
    return m_sWeekendMeetingInfo;
}

At the moment I am mapping the dialog controls to distinct variables and transfer to / from my structure.目前我正在将对话框控件映射到不同的变量并传输到/从我的结构中传输。 For example:例如:

m_strChairman = m_sWeekendMeetingInfo.strChairman;
m_sWeekendMeetingInfo.strChairman = m_strChairman;

Is possible to map my controls directly to the structure member CString variables or would that be considered bad practice?是否可以将 map 我的控件直接用于结构成员CString变量,或者这会被认为是不好的做法?

Yes, as Adrian says, you don't have to confine yourself to using the variables that the wizard would hand you.是的,正如 Adrian 所说,您不必将自己局限于使用向导会交给您的变量。 I used my test project to do this with the about dialog.我使用我的测试项目通过 about 对话框执行此操作。

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);
}

Here I passed a reference to the external data to work on it directly.在这里,我传递了对外部数据的引用以直接对其进行处理。 Because of the DoDataExchange mechanism, The data will only be modified if the user pushes OK .由于DoDataExchange机制,只有用户按下OK才会修改数据。 I added the first DDX_Text handler with the wizard, hence the commented value dummy .我使用向导添加了第一个DDX_Text处理程序,因此注释值dummy I just copied and pasted that handler to add the second.我只是复制并粘贴了那个处理程序来添加第二个。

I put the assert in the app command so the program would break there and could examine the data.我将断言放在 app 命令中,这样程序就会在那里中断并检查数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM