Click to See Complete Forum and Search --> : resource.h at runtime
Philip McGahan
April 17th, 1999, 08:26 AM
Point me in the right direction please.
I would like to iterate through all the edit controls on a dialog. Something like this.
CEdit *pEdit
for( long lIDC_Value = 0; lIDC_Value <= Last Edit Control; LIDC_Value++)
{
pEdit = (CEdit*)GegDlgItem(lIDC_Value);
}
I know all the IDC values are defined in the
resource.h, but I do not know how to get them at run time.
Thank you.
Phil McGahan
(303) 252-8889
12100 Elizabeth St.
Thornton, Colorado 80241
Roger Osborn
April 17th, 1999, 04:49 PM
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
Philip McGahan
April 18th, 1999, 08:25 PM
Thank you Roger. The GetTopWindow/GetNextWindow was a simple and elegant solution.
Phil McGahan
(303) 252-8889
12100 Eli
Roger Osborn
April 19th, 1999, 03:02 AM
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.
Roger Osborn
April 19th, 1999, 03:04 AM
Actually, thinking a bit harder, GetDlgItem probably iterates over the controls anyway, so method 3 is probably faster too.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.