简体   繁体   中英

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? 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.

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. There is no table that you could parse and therefore you cannot dynamically determine which DDX_* function is called in your DoDataExchange method.

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. So once the DoDataExchage function has been executed that array would contain all Control IDs used by DDX.

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).
  • In all your dialogs replace all calls to DDX_* functions my MY_DDX_* functions
  • Put the m_ddxcontrols members in all your dialogs

None that I know of, but you can copy the control resource IDs from the DoDataExchange block into an array with a zero terminator:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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