mfc >> Enumerate windows in a groupbox...

by Tom » Tue, 02 Dec 2003 17:32:21 GMT

Hi,
what is the standard way to enumerate all windows within a groupbox?

I wonder since I have a groupbox in which I have placed several controls
such as editbox, textfield, button etc. Now, under certain circumstances I'd
like to process all of those "contained" controls.

First I thought that I'd loop them by first setting up a tab order in such
way that the groupbox is first and then just do:
CWnd* pWnd = this;
while (pWnd && !Finished)
{
DoStuff(pWnd);
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
But I do not know how to set Finished...

The other approach was to do an IntersectRect to see which controls lies
within the gropbox:
CWnd* pWnd = GetParent()->GetWindow(GW_CHILD);
while (pWnd)
{
pWnd->GetWindowRect(rectWnd);
if (pWnd != this && rectTmp.IntersectRect(rectGroupbox, rectWnd))
{
DoStuff(pWnd);
}
pWnd = pWnd->GetWindow(GW_HWNDNEXT);
}
However, this fails due to controls such as other groupboxes may contain the
current groupbox which results in that the other control gets processed as
well by our DoStuff method.
This can of course be worked around by calculating x and y positions and add
the following to the if-statement above:
rectGroupbox.TopLeft().x > rectWnd.TopLeft().x &&
rectGroupbox.TopLeft().y < rectWnd.TopLeft().y
But this feels like a hack :-(

So I wonder which is the standard way to do this kind of loop?
Thanks in advance,
/Tom




mfc >> Enumerate windows in a groupbox...

by David Lowndes » Wed, 03 Dec 2003 03:26:53 GMT


>what is the standard way to enumerate all windows within a groupbox?

Tom,

There is no standard way of doing it - since there is no fixed
relationship that says whether a control is "within" a groupbox.


If you can rely on the group being the first control, then you can
make this work if you know the child ID of the last control in the
group. I think this is the most straight-forward way of doing it.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq



mfc >> Enumerate windows in a groupbox...

by rquirk » Thu, 04 Dec 2003 17:03:39 GMT

Set the WS_GROUP style on starts of groups. Then it either finds a new
group or finishes because there are no more windows. Tab order is
important for this the 'group static control' should be first in the
tab-order with the windows within it also in order (as you know).

// ------------------------------------------------------------------
// Name: TC1260ServiceCADlg::EnableControlGroup
// Description: Enables or disables a group of controls.
// ------------------------------------------------------------
void TC1260ServiceCADlg::EnableControlGroup(UINT nCtrlStartID, BOOL
bEnable)
{
CWnd *pWnd = GetDlgItem(nCtrlStartID);

// Window with ID == nID should exist and ...
ASSERT( pWnd != NULL );
// it should be marked as the start of a group
ASSERT( (pWnd->GetStyle() & WS_GROUP) == WS_GROUP );

// Walk through all the controls in a group (based on Tab order)
enabling /
// disabling them until we find the next group (the control has
'Group'
// checked in the resource editor) or we've run out of controls
BOOL bInGroup = TRUE;
while ( pWnd && bInGroup )
{
pWnd->EnableWindow(bEnable);
pWnd = pWnd->GetNextWindow();
if ( pWnd )
{
// If found next group start ...
if ( (pWnd->GetStyle() & WS_GROUP) == WS_GROUP )
{
bInGroup = FALSE;
}
}
}
}


Similar Threads

1. c# windows form groupbox border

Hi,

I would like to set the GroupBox's border to invisible, how to do that?
Thanks for help.


Jason 


2. Enumerating local printer drivers in C# / Windows 2000

3. Enumerate Windows

Hi,

I need to enumerate windows and find the sum of the rect of all the windows 
of  a specific application. In C++, I use the APIs - 'EnumWindows , 
GetWindowRect and UnionRect to accomplish the same.

How to perform the above in c#? Is PInvoke the only way to do it or Is there 
some inherent c# way?

4. Enumerating windows from an NT service - CSharp/C#

5. Enumerating Explorer shell windows out of memory exception

On certain occasions I get COM exceptions errors when enumerating the
Windows Explorer active windows

Some customers reported the error
System.OutOfMemoryException: Retrieving the COM class factory for
component with CLSID {9BA05972-F6A8-11CF-A442-00A0C90A8F39} failed due
to the following error: 8007000e.

Here's the code I use to retrieve the list of IE windows. Are there
better solutions that prevent the exceptions?

try {
SHDocVw.ShellWindows shell = new ShellWindowsClass();

int ihandle=handle.ToInt32();
foreach (InternetExplorer ie in shell) {
				
try {							
	if (ihandle == ie.HWND) {
		path=ie.LocationURL;
	}
}
catch (System.Runtime.InteropServices.COMException ex) {

}
catch (System.UnauthorizedAccessException ex) {
}


} catch (FileNotFoundException ex) {
}
catch (System.TypeLoadException ex) {
}

6. Enumerating Windows Network Resources - CSharp/C#

7. Enumerating Windows

Hey There,
    I have this function that is called through EnumWindows:

BOOL CALLBACK EnumOpenWindows(HWND hwnd, LPARAM lParam)
{
	DWORD						dwID;
	LPDWORD						dwID2;
	LPDWORD						currProcId;
	FILE*						fp;
	struct _HwndProcStruct*		locHPStruct;

	fp = fopen(THREAD_LOG_FILE,APPEND_MODE);
	fprintf(fp,"*******************<EnumOpenWindows>*******************\n");
	fclose(fp);

	dwID2		= (LPDWORD)calloc(1,sizeof(DWORD));
	locHPStruct = (struct _HwndProcStruct*)lParam;
	*currProcId = locHPStruct->procID;
	*dwID2 = 0;

	GetWindowThreadProcessId(hwnd, dwID2);

		fp = fopen(THREAD_LOG_FILE,APPEND_MODE);
		fprintf(fp,"currProcId=%d dwID2=%d\n",*currProcId,*dwID2);
		fclose(fp);

	if(*dwID2 == *currProcId)
	{
		locHPStruct->WindHandle = hwnd;
		free(dwID2);
		return FALSE;
	}
	else
	{
		free(dwID2);
		return TRUE;
	}
}

I'm using it to try to find the HWND from a Process ID. For some
reason, when I have the file print statements below
"GetWindowThreadProcessId(hwnd, dwID2); " the function will generate
the following error:

Unhandled exception at 0x0040126c in SysTrayApp.exe: 0xC0000005: Access
violation writing location 0x000809be.

But if I comment out those three lines, then the function executes
normally. What could cause this?

-Jay
(patelj27b at gmail dot com)

8. Enumerating windows from an NT service