Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group radio box buttons using Win32 API

Tags:

c++

winapi

I am working on a program with grouped check box and get confused on how the messages convey through different handles.

IDE:VC++, Win32 API

First, I have a main window which has a handle, say hWnd.

And in the WndProc function under case WM_CREATE, we create the "group button" and the individual 2 check boxes

Note: the first button uses "BS_GROUPBOX" style, and it was created with the handle hGrpButton while its parent handler is hWnd. The second and third button is "BS_AUTORADIOBUTTON" style and it's parent handle is hGrpButton.

If the 2 buttons are not grouped(so their parent hanlder would be hWnd), it is easy to check the status of them. Just simply go to case WM_COMMAND AND use their ID to check with the IsDlgButtonChecked function. After the two check boxes are grouped (their parent handle are not longer hWnd but hGrpButtons), I don't think the case WM_COMMAND will find their IDs since it is looking for the IDs under hWnd.

In short, after 2 check boxes are grouped, I don't know what is the event to monitor them.

case WM_CREATE:
{        
/*Group for Radio button for preview/single or batch operation */
hGrpButtons=CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"Select Process Mode:", 
                    WS_VISIBLE | WS_CHILD|BS_GROUPBOX,  // Styles 
                    10,280,
                    350,100, 
                    hWnd, 
                    NULL, 
                    hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"Batch Process Mode", 
                    WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON,  // Styles 
                    10,20,
                    300,20, 
                    hGrpButtons, 
                    (HMENU)IDC_CHK1, 
                    hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"Single Process Mode (Preview Mode)", 
                    WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON,  // Styles 
                    10,45,
                    300,20, 
                    hGrpButtons, 
                    (HMENU)IDC_CHK2, 
                    hInst, NULL);
like image 257
user2500299 Avatar asked Jun 19 '13 09:06

user2500299


1 Answers

After read Coder_Dan's comment and MSDN article "http://msdn.microsoft.com/en-us/library/bb775947(v=vs.85).aspx#related_topics" about the button types, I finally sort it out.

  1. BS_GROUPBOX is just an "eye candy" and it did no contribute to the group of the radio box!! The critical parameter to control the grouping is the "WS_GROUP"

  2. How to group the radio box: Put the WS_GROUP in the first radio button's style. It will group the current radio button until it see the second WS_GROUP as mentioned by Coder_Dan.

Modification on my previous code

a. use hWnd as parent for all 4 radio buttons

b. Put WS_GROUP in the first and third button style so that we group the 1,2 and 3,4 radio buttons

c. Now you can go to the WM_COMMAND, and listen to the messages from the 4 buttons according to their IDs under the main window's handle hWnd

    case WM_CREATE:
{        

/*Group for Radio button for preview/single or batch operation */
hGrpButtons=CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"Select Process Mode:", 
                    WS_VISIBLE | WS_CHILD|BS_GROUPBOX,// <----BS_GROUPBOX does nothing on the grouping 
                    10,280,
                    350,100, 
                    hWnd, 
                    (HMENU)IDC_GRPBUTTONS, 
                    hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"first radio button", 
                    WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON|WS_GROUP,  // <---- WS_GROUP group the following radio buttons 1st,2nd button 
                    10,520,
                    300,20, 
                    hWnd, //<----- Use main window handle
                    (HMENU)IDC_CHK1, 
                    hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"second radio button", 
                    WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON,  // Styles 
                    10,545,
                    300,20, 
                    hWnd, 
                    (HMENU)IDC_CHK2, 
                    hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"third radio button", 
                    WS_VISIBLE | WS_CHILD|BS_AUTOCHECKBOX|WS_GROUP,  //<---Start second group for 3rd,4th button
                    10,570,
                    300,20, 
                    hWnd, 
                    (HMENU)IDC_CHK3, 
                    hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
                    L"BUTTON",
                    L"forth radio button", 
                    WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON,  // Styles 
                    500,545,
                    300,20, 
                    hWnd, 
                    (HMENU)IDC_CHK4, 
                    hInst, NULL);
like image 183
user2500299 Avatar answered Sep 29 '22 09:09

user2500299