user12405965
user12405965

Reputation:

Is there a way I can check if button is checked?

I working on a Installer, that installs my program.

I want to ask the user if he want to create a desktop shortcut or Run the program at startup. In order for him to Run at startup the program, or create a desktop shortcut, he need to check the checkboxes.

My program is debugging well, no errors, but I have one problem.

The desktop shortcut and the RunAtStartup function is always created, I don't know how can I check if button is checked or unchecked.

Is there a way I can check if button is checked ?

This is my code:

LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
    case WM_COMMAND: {
        // Buttons Check
        if (wparam == IDC_BUTTON1)
        {
            BOOL checked = IsDlgButtonChecked(hwnd, IDC_BUTTON1);
            if (checked) {
                CheckDlgButton(hwnd, IDC_BUTTON1, BST_UNCHECKED);
            }
            else {
                CheckDlgButton(hwnd, IDC_BUTTON1, BST_CHECKED);
            }
        }
        if (wparam == IDC_BUTTON3)
        {
            BOOL checked = IsDlgButtonChecked(hwnd, IDC_BUTTON3);
            if (checked) {
                CheckDlgButton(hwnd, IDC_BUTTON3, BST_UNCHECKED);
            }
            else {
                CheckDlgButton(hwnd, IDC_BUTTON3, BST_CHECKED);
            }
        }

        // Install Part
        switch (wparam)
        {
        case IDC_BUTTON2:
            Start_Install(hwnd, wparam);
            if (IDC_BUTTON1 == IsDlgButtonChecked(hwnd, IDC_BUTTON1)) {

            }
            else {
                Sleep(5000);
                link();
            }
            if (IDC_BUTTON3 == IsDlgButtonChecked(hwnd, IDC_BUTTON3)) {

            }
            else {
                Sleep(5000);
                RunStartup();
            }
            Sleep(5000);
            PostQuitMessage(0);
            break;
        }
    }
}

This is only the installation, if you need the full code, ask for it and I will edit.

Upvotes: 1

Views: 1030

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51815

I'm posting this answer because, although it seems my short comment resolved the OP's problem, I now realize that 'solution' is inaccurate: the reason being that I had (incorrectly) assumed that the IsDlgButtonChecked() function returns a BOOL value indicating whether the button is "checked" (= TRUE) or "unchecked" (= FALSE).

However, this is not the case, as can be seen from the M/S documentation - in fact, it returns a UINT that can have one of three values: BST_UNCHECKED (= 0), BST_CHECKED (= 1) or BST_INDETERMINATE (= 2). So, in the unusual/unlikely event that the button has an "indeterminate" status (whatever that means in this case), my 'quick solution' would likely fail.

I also noticed in the OP's code that there are unnecessary/redundant checks on the value of wparam - which does not change during the course of the given function. The mixture of if (wparam == ... checks and the subsequent switch(wparam) ... block can be greatly simplified into one single switch, as follows (noting also the 'corrected' tests of the IsDlgButtonChecked results):

LRESULT CALLBACK WindowProcessMessages(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    BOOL checked;
    LRESULT answer = DefWndProc(hwnd, msg, wparam, lparam); // Or something similar
    switch (msg)
    {
        case WM_COMMAND: {
            switch (wparam)
            {
                // Buttons Check
                case IDC_BUTTON1:
                    checked = IsDlgButtonChecked(hwnd, IDC_BUTTON1);
                    if (checked == BST_CHECKED) CheckDlgButton(hwnd, IDC_BUTTON1, BST_UNCHECKED);
                    else                        CheckDlgButton(hwnd, IDC_BUTTON1, BST_CHECKED);
                    break;
                case IDC_BUTTON3:
                    checked = IsDlgButtonChecked(hwnd, IDC_BUTTON3);
                    if (checked == BST_CHECKED) CheckDlgButton(hwnd, IDC_BUTTON3, BST_UNCHECKED);
                    else                        CheckDlgButton(hwnd, IDC_BUTTON3, BST_CHECKED);
                    break;
                // Install Part
                case IDC_BUTTON2:
                    Start_Install(hwnd, wparam);
                    if (IDC_BUTTON1 == IsDlgButtonChecked(hwnd, IDC_BUTTON1) == BST_CHECKED) {
                        //..
                    }
                    else {
                        Sleep(5000);
                        link();
                    }
                    if (IsDlgButtonChecked(hwnd, IDC_BUTTON3) == BST_CHECKED) {
                        //..
                    }
                    else {
                        Sleep(5000);
                        RunStartup();
                    }
                    Sleep(5000);
                    PostQuitMessage(0);
                    break;
            }
        }
    }
    return answer; // Need to set "answer" accordingly, probably dependent on the actions taken in the SWITCH block(s)
}

Upvotes: 1

Related Questions