Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

1 Answers

You need to send the BM_SETCHECK message.

SendMessage(hwndButtonPollFlag, BM_SETCHECK, BST_CHECKED, 0);
like image 142
Mark Ransom Avatar answered Nov 25 '25 16:11

Mark Ransom