检查单选按钮状态winapi

时间:2013-04-03 14:32:24

标签: c++ winapi radio-button

我使用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;
        ....

但是当我点击radiobutton以便它不检查状态时,这总会运行。 如何在不使用click事件的情况下检查状态?

3 个答案:

答案 0 :(得分:4)

使用IsDlgButtonChecked

if(IsDlgButtonChecked(hwnd, ID_RADIOBTN1))
    MessageBox(NULL, "radio1 is checked")
else
    MessageBox(NULL, "radio1 is not checked")

答案 1 :(得分: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;

答案 2 :(得分:0)

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