简体   繁体   English

是否在MFC对话框中定义了控件列表或控件容器

[英]Is there a list of controls or controls container defined in an MFC dialog

I'm asking about a list or controls container in mfc dialog? 我问的是“ MFC”对话框中的列表或控件容器? i don't mean listing the child windows of the a dialog like this question Loop through MFC Child Dialogs, MDIFrames etc ,What i want is a list of the controls defined as variables in the dialog class those which DDX_Control method is applied on them. 我不是要列出像这样的问题的对话框的子窗口, 通过MFC子对话框,MDIFrames等循环 ,我想要的是在对话框类中定义为变量的控件的列表,这些控件上应用了DDX_Control方法。

i need to have a list of all control variables defined in the dialog 我需要在对话框中定义所有控制变量的列表

There is no such thing. 哪有这回事。 A control is used by DDX because the corresponding DDX_* function is called in the DoDataExchange method of your dialog class. DDX使用控件,因为在对话框类的DoDataExchange方法中调用了相应的DDX_ *函数。 There is no table that you could parse and therefore you cannot dynamically determine which DDX_* function is called in your DoDataExchange method. 没有可以解析的表,因此无法动态确定DoDataExchange方法中调用了哪个DDX_ *函数。

void CMySampleDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CReprendBudgetDlg)
    DDX_Text(pDX, IDC_EDIT1, m_name1);
    DDX_Text(pDX, IDC_EDIT2, m_name2);
    //}}AFX_DATA_MAP
}

But you could "override" the DDX_* functions by some functions of your own that would put the control IDs in an array. 但是您可以通过自己的某些函数“覆盖” DDX_ *函数,这些函数会将控件ID放入数组中。 So once the DoDataExchage function has been executed that array would contain all Control IDs used by DDX. 因此,一旦执行了DoDataExchage函数,该数组将包含DDX使用的所有控件ID。

void AFXAPI MY_DDX_Text(CDataExchange* pDX, int nIDC, CString& value, CWordArray & ddxcontrols)
{
  DDX_Text(pDX, nIDC, value);
  if (!pDX->bSaveAndValidate)
    ddxcontrols.Add(nIDC) ;
}


#define DDX_Text(a,b,c) MY_DDX_Text(a,b,c)  // now we can continue to use DDX_Text
                                            // and the Class Wizard will be happy

class CMySampleDlg : public CDialog
{
 ...
  protected:
    CWordArray m_ddxcontrols ;  // array that will contain all control IDs use by DDX
  ...
}


void CMySampleDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CReprendBudgetDlg)
    DDX_Text(pDX, IDC_EDIT1, m_name1, m_ddxcontrols);
    DDX_Text(pDX, IDC_EDIT2, m_name2, m_ddxcontrols);
    //}}AFX_DATA_MAP
}

So all you have to do is 所以你要做的就是

  • Write the MY_DDX_* functions for all DDX_* functions (they are defined in afxdd_.h). 为所有DDX_ *函数编写MY_DDX_ *函数(它们在afxdd_.h中定义)。
  • In all your dialogs replace all calls to DDX_* functions my MY_DDX_* functions 在所有对话框中,替换所有对DDX_ *函数和MY_DDX_ *函数的调用
  • Put the m_ddxcontrols members in all your dialogs 将m_ddxcontrols成员放入所有对话框中

None that I know of, but you can copy the control resource IDs from the DoDataExchange block into an array with a zero terminator: 我不知道,但是您可以将控制资源ID从DoDataExchange块复制到带有零终止符的数组中:

const UINT myControls[] =
{
  IDC_EDIT1,      IDC_EDIT2,      IDC_EDIT3,
  IDC_BUTTON1,    IDC_BUTTON2,    IDC_BUTTON3,
  IDC_STATIC1,    IDC_STATIC2,    IDC_STATIC3,
  0
};

then you can use this array for iterating over the controls to do with as you wish: 那么您可以根据需要使用此数组迭代控件以进行处理:

for (const UINT* p = myControls; *p; ++p)
{
    CWnd *wnd = GetDlgItem(*p);
    ...
}

Not a dynamic solution, but simple enough. 不是动态解决方案,但足够简单。

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

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