user4257914
user4257914

Reputation:

Using WINAPI how do I change the value of a checkbox button?

Currently I have a checkbox created in WM_CREATE:

hwndButtonPollFlag = 
CreateWindow(
    TEXT("BUTTON"), 
    TEXT(sA.getMonitor(monitorSelected)->szDevice),
    WS_CHILD | WS_VISIBLE | SS_WHITERECT | BS_CHECKBOX, 
    0, 
    0, 
    0,
    0, 
    hwnd, 
    (HMENU)IDB_PollFlag, 
    hInstance, 
    NULL);

I am trying to change it's value whenever another button is clicked with:

    if (sA.getScreenArray(monitorSelected)->getPollFlag())
    {
        SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_CHECKED);
    }
    else
    {
        SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_UNCHECKED);
    }
    SetWindowText(hwndButtonPollFlag, TEXT(sA.getMonitor(monitorSelected)->szDevice));

This does change the text displayed next to the checkbox but not the actual state of the button. Also I would like the checkbox to have only two states (checked or unchecked) is there any other way to create that effect other than in the button return have something along the lines of:

switch (HIWORD(wParam))
    {
    case BST_CHECKED:
        sA.getScreenArray(monitorSelected)->setPollFlag(true);
        return 0;
    case BST_INDETERMINATE:
        if (sA.getScreenArray(monitorSelected)->getPollFlag())
        {
            SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_UNCHECKED);
        }
        else
        {
            SetWindowLongPtr(hwndButtonPollFlag, GCL_STYLE, WS_VISIBLE | BST_CHECKED);
        }
        return 0;
    case BST_UNCHECKED:
        sA.getScreenArray(monitorSelected)->setPollFlag(false);
        return 0;
    }

EDIT: As Mark Ransom said I used messages with the BM_GETCHECK and BM_SETCHECK flag as so:

    case IDB_MONITOR:
    monitorSelected = LOWORD(lParam);
    if (sA.getScreenArray(monitorSelected)->getPollFlag())
    {
        SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_CHECKED, NULL);
    }
    else
    {
        SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_UNCHECKED, NULL);
    }
    SetWindowText(hwndButtonPollFlag, TEXT(sA.getMonitor(monitorSelected)->szDevice));
    return 0;
case WM_COMMAND:
    //sA.getScreenArray(monitorSelected)->setPollFlag(LOWORD(lParam));
    switch (LOWORD(wParam))
    {
    case IDB_PollFlag:
        if (SendMessage(GetDlgItem(hwnd, IDB_PollFlag), BM_GETCHECK, 0, 0) == BST_CHECKED)
        {
            SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_CHECKED, NULL);
            sA.getScreenArray(monitorSelected)->setPollFlag(true);
        }
        else {
            SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_UNCHECKED, NULL);
            sA.getScreenArray(monitorSelected)->setPollFlag(false);
        }
        break;
    }
    return 0;

Upvotes: 3

Views: 4785

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308176

You need to send the BM_SETCHECK message.

SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_CHECKED, 0);

Upvotes: 6

Jonathan Potter
Jonathan Potter

Reputation: 37132

BST_CHECKED and BST_UNCHECKED aren't window styles, they're simply flag values used by the CheckDlgButton and IsDlgButtonChecked API functions. CheckDlgButton is the function to call to change its state.

(Or, you can send BM_SETCHECK and BM_GETCHECK messages directly to the button for the same effect).

Upvotes: 3

Related Questions