4

我使用 mingw32 编译器。我创建了两个radioButton:

radio1 = CreateWindowExW(WS_EX_TRANSPARENT  , L"BUTTON", L"RadioButton1",
   WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON  ,
   0, 0, 0, 0,
   hwnd, (HMENU)ID_RADIOBTN1,
   GetModuleHandle(NULL), 0);
SendMessage(radioBtnDaily, WM_SETFONT, (WPARAM) font, TRUE);

radio2 = CreateWindowExW(WS_EX_TRANSPARENT  , L"BUTTON", L"RadioButton2",
   WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON  ,
   0, 0, 0, 0,
   hwnd, (HMENU)ID_RADIOBTN2,
   GetModuleHandle(NULL), 0);

SendMessage(radio1, BM_SETCHECK , (WPARAM) font, TRUE);
SendMessage(radio2,  WM_SETFONT, (WPARAM) font, TRUE);

我处理检查状态,如下所示:

case WM_COMMAND:
    switch(LOWORD(wParam))
    {   
        case ID_RADIOBTN1:
        {
            MessageBoxW( NULL, "radio1 is checked", L"radio", MB_OK | MB_ICONERROR );
        }
        break;

        case ID_RADIOBTN2:
        {
        MessageBoxW( NULL, "radio2 is checked", L"radio", MB_OK | MB_ICONERROR );
        }
        break;
        ....

但这总是在我单击单选按钮时运行,因此它不会检查状态。如何在不使用点击事件的情况下检查状态?

4

3 回答 3

4

使用IsDlgButtonChecked

if(IsDlgButtonChecked(hwnd, ID_RADIOBTN1))
    MessageBox(NULL, "radio1 is checked")
else
    MessageBox(NULL, "radio1 is not checked")
于 2013-04-03T15:03:47.637 回答
3

使用BM_GETCHECK消息或Button_GetCheck()宏来获取单选按钮的实际状态,例如:

radio1 = CreateWindowExW(WS_EX_TRANSPARENT  , L"BUTTON", L"RadioButton1",
   WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON  ,
   0, 0, 0, 0,
   hwnd, (HMENU)ID_RADIOBTN1,
   GetModuleHandle(NULL), 0);
SendMessage(radio1, WM_SETFONT, (WPARAM) font, TRUE);

radio2 = CreateWindowExW(WS_EX_TRANSPARENT  , L"BUTTON", L"RadioButton2",
   WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON  ,
   0, 0, 0, 0,
   hwnd, (HMENU)ID_RADIOBTN2,
   GetModuleHandle(NULL), 0);
SendMessage(radio2,  WM_SETFONT, (WPARAM) font, TRUE);

.

case WM_COMMAND:
    if ((HIWORD(wParam) == BN_CLICKED)
    {
        switch(LOWORD(wParam))
        {   
            case ID_RADIOBTN1:
            {
                switch (Button_GetCheck(radio1))
                {
                    case BST_CHECKED:
                        MessageBoxW( NULL, L"radio1 is checked", L"radio", MB_OK );
                        break;

                    case BST_INDETERMINATE:
                        MessageBoxW( NULL, L"radio1 is indeterminate", L"radio", MB_OK );
                        break;

                    case BST_UNCHECKED:
                        MessageBoxW( NULL, L"radio1 is unchecked", L"radio", MB_OK );
                        break;
                }
            }
            break;

            case ID_RADIOBTN2:
            {
                switch (Button_GetCheck(radio2))
                {
                    case BST_CHECKED:
                        MessageBoxW( NULL, L"radio2 is checked", L"radio", MB_OK );
                        break;

                    case BST_INDETERMINATE:
                        MessageBoxW( NULL, L"radio2 is indeterminate", L"radio", MB_OK );
                        break;

                    case BST_UNCHECKED:
                        MessageBoxW( NULL, L"radio2 is unchecked", L"radio", MB_OK );
                        break;
                }
            }
            break;
        }
    }
    break;
于 2013-04-03T17:46:27.830 回答
0

这取决于您要检查按钮状态的事件,因此您需要某种交互(这可以是另一个事件,如窗口最小化、调整大小等或另一个线程检查但不修改控件的状态)

于 2013-04-03T14:46:26.787 回答