Re: resource.h at runtime
There are several ways of doing this:
You could ensure that all the edit controls have contiguous IDs in your resource.h and just code:
for( long lIDC= IDC_FIRSTEDIT; lIDC<= IDC_LASTEDIT; lIDC++)
{
pEdit = (CEdit*)GetDlgItem(lIDC);
}
but that's asking for trouble if someone who doesn't realize the order changes them.
You could set up a static array in your program cpp file.
e.g.
#include "stdafx.h"
#include "resource.h"
static UINT MyEditControls[]=
{
IDC_FIRSTEDIT,
IDC_SECONDEDIT,
IDC_THIRDEDIT,
IDC_LASTEDIT
};
...
const int iLimit=sizeof(MyEditControls)/sizeof(MyEditControls[0]);
for( long lIDC= 0; lIDC<iLimit; lIDC++)
{
pEdit = (CEdit*)GetDlgItem(MyEditControls[lIDC]);
}
which at least means they don't have to be kept contiguous. But you still have to update the table when you add or remove edit controls.
Finally you could use GetTopWindow()/GetNextWindow() to iterate over the controls in the dialog at run time and pick out the ones that are Edit controls. That would adapt to changes to the dialog and be pretty much self-maintaining.
You could always compare the classnames found against that obtained from something you know is a CEdit control.
void CDummytDlg::OnButton1()
{
CWnd *pWnd=GetTopWindow();
while(pWnd)
{
TCHAR sBUFFER[1024];
VERIFY(GetClassName(pWnd->m_hWnd,sBUFFER,sizeof(sBUFFER)/sizeof(TCHAR)));
{
TRACE("found %s\n",sBUFFER);
}
pWnd=pWnd->GetNextWindow();
}
}
Hope one of those solves it.
Cheers,
Roger
Re: resource.h at runtime
Thank you Roger. The GetTopWindow/GetNextWindow was a simple and elegant solution.
Phil McGahan
(303) 252-8889
12100 Eli
Re: resource.h at runtime
No probs. Glad it helped.
Method 3 is a bit slower than 2 but unless the dialog has many controls it's probably worth it, IMHO.
Re: resource.h at runtime
Actually, thinking a bit harder, GetDlgItem probably iterates over the controls anyway, so method 3 is probably faster too.