Group of Radio Buttons

Any chance I'm able to enumerate the Radio Buttons controlled by a Group element?

Like:

1
2
3
4
5
6
7
8

BOOL CALLBACK MyFunc(HERP derp)
{
    return 0;
}

HWND MyGroup = GetDlgItem(...);
EnumerateRadioButtons(MyGroup,MyFunc);

?
closed account (N36fSL3A)
First off why is bool in caps? What is this Visual Studio 1.5?
Any chance I'm able to enumerate the Radio Buttons controlled by a Group element?

There's no ready made function, and it would prob. be a bit messy; e.g. locate the group box, enumerate the other child windows of the dialog to find the radio buttons (using the window style).

The cheapest solution would be to assign the controls in the group contiguous IDs. Then you can just use a for loop.

Do you need a more complicated solution for some reason?

why is bool in caps?


I would guess that EnumerateRadioButtons is modelled on EnumWindows, which takes a function of this type

typedef BOOL (CALLBACK* WNDENUMPROC)(HWND, LPARAM);

i.e.

1
2
3
4
BOOL CALLBACK EnumWindowsProc(
  _In_  HWND hwnd,
  _In_  LPARAM lParam
);


Andy

PS Also see

EnumWindows function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx
Last edited on
@FredBill30: BOOL != bool.
BOOL is #define to int, in windows.h
(Reason is, Windows.h is for C, and bool type is not into C. So they use int.)

Well, thank you andywestken, not much to say then, I'll stick with what I knew.
Last edited on
Out of curiousity, I coded it...

Note that it assumes that all controls are direct children of the (same) dialog.

Andy

Part #1 - EnumRadioButtonsInGroup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
struct ERGB_Data
{
    RECT*       pRectGroup;
    WNDENUMPROC lpEnumFunc;
    LPARAM      lParam;
};

BOOL CALLBACK ERGB_EnumChildProc(HWND hwnd, LPARAM lParam)
{
    BOOL ret = TRUE;

    ERGB_Data* pData = (ERGB_Data*)lParam;

    RECT rectCtrl = {0};
    GetWindowRect(hwnd, &rectCtrl);

    POINT pointTL = {rectCtrl.left, rectCtrl.top};
    POINT pointBR = {rectCtrl.right, rectCtrl.bottom};

    if(    PtInRect(pData->pRectGroup, pointTL)
        && PtInRect(pData->pRectGroup, pointBR) )
    {
        TCHAR className[256] = {0};
        GetClassName(hwnd, className, _countof(className));

        DWORD styles = GetWindowLongPtr(hwnd, GWL_STYLE);
        DWORD buttonType = (styles & BS_TYPEMASK);

        if(    (0 == _tcsicmp(className, _T("Button")))
            && (    (BS_RADIOBUTTON == buttonType)
                 || (BS_AUTORADIOBUTTON == buttonType) ) )
        {
            ret = (*(pData->lpEnumFunc))(hwnd, pData->lParam);
        }
    }

    return ret;
}

BOOL EnumRadioButtonsInGroup(HWND hwndGroup, WNDENUMPROC lpEnumFunc, LPARAM lParam)
{
    HWND hwndDlg = GetParent(hwndGroup);

    RECT rectGroup = {0};
    GetWindowRect(hwndGroup, &rectGroup);

    ERGB_Data data = {&rectGroup, lpEnumFunc, lParam};

    return EnumChildWindows(hwndDlg, ERGB_EnumChildProc, (LPARAM)&data);
}


Part 2 - the enum proc for use with EnumRadioButtonsInGroup

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct MyData
{
    TCHAR* buffer;
    DWORD  bufferSize;
};

BOOL CALLBACK MyFunc(HWND hwnd, LPARAM lParam)
{
    MyData* pData = (MyData*)lParam;

    TCHAR windowText[256] = {0};
    GetWindowText(hwnd, windowText, _countof(windowText));

    if(pData->buffer[0] != _T('\0'))
    {
        _tcsncat(pData->buffer, _T(", "), pData->bufferSize);
    }
    _tcsncat(pData->buffer, windowText, pData->bufferSize);

    return TRUE;
}


Part #3 - Example usage

1
2
3
4
5
6
7
8
9
    const DWORD bufferSize = 1024;
    TCHAR buffer[bufferSize] = {0}; // must be zeroed

    HWND   hwndGroup = GetDlgItem(hDlg, IDC_GROUP1);
    MyData data = {buffer, bufferSize};

    EnumRadioButtonsInGroup(hwndGroup, MyFunc, (LPARAM)&data);

    MessageBox(hDlg, buffer, _T("Group Test"), MB_OK | MB_ICONEXCLAMATION);


For

===========================================
=                                         =
=  -Channel-------------  -Quality------  =
=  | ( ) Radio 1       |  | (o) Stereo |  =
=  | ( ) Radio 2       |  | ( ) Mono   |  =
=  | ( ) Radio 3       |  --------------  =
=  | (o) Radio 4       |                  =
=  | ( ) Radio 4 Extra |                  =
=  | ( ) Radio 5 Live  |                  =
=  ---------------------                  =
=                                         =
=        [Test]                           =
=                           [OK] [Cancel] =
===========================================


The MessageBox displays "Radio 1, Radio 2, Radio 3, Radio 4, Radio 4 Extra, Radio 5 Live" (where IDC_GROUP1 is the ID for the "Channel" group box, and Test is the button I use to trigger the code.).
Last edited on
:O
Topic archived. No new replies allowed.