简体   繁体   中英

c++ Pass variable value from one code block to another

Checkbox doesn't change the value of " use_Xml " variable that is being written to a file. This is a matter of scope I think, but I don't know how to do it right. I'm a beginner. This has been probably answered a lot of times already but I just don't know how to ask the question right and find the answer. I will agree to a redirect to a relevant question.

#include <stdio.h>
#include <windows.h>
...
string use_Xml = "1";
...

LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(...) {...}

    LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {

        switch(msg)
        {

        case WM_COMMAND:

        switch(LOWORD(wParam))
        {

            case IDC_MAIN_CHKBX_X:
            {

                UINT checkedX = IsDlgButtonChecked(hWnd, IDC_MAIN_CHKBX_X);

                switch (checkedX)
                {

                case BST_CHECKED:
                    {
                    CheckDlgButton(hWnd, IDC_MAIN_CHKBX_X, BST_UNCHECKED);
                    SetWindowText(hWnd, TEXT("no"));
                    use_Xml = "0";
                    }
                    break;

                case BST_UNCHECKED:
                    {
                    CheckDlgButton(hWnd, IDC_MAIN_CHKBX_X, BST_CHECKED);
                    SetWindowText(hWnd, TEXT("yes"));
                    use_Xml = "1";
                    }
                    break;
                }

            }
            break;

            case IDC_MAIN_BUTTON_S:
            {
                ofstream myfile ("foo.ini");
                myfile << use_Xml << "\n";
                myfile.close();
            }
            break;
        }
        break;

        }

        return DefWindowProc(hWnd,msg,wParam,lParam);
    }

The return value of IsDlgButtonChecked() is a UINT and not a BOOL . From the linked reference page:

Return code       Description

BST_CHECKED       The button is checked.

BST_INDETERMINATE The button is in an indeterminate state (applies only 
                  if the button has the BS_3STATE or BS_AUTO3STATE style).

BST_UNCHECKED     The button is not checked.

It may be that all of these values are non-zero resulting in the same execution path being taken irrespective of the state of the check box in the dialog. Correct the code and add diagnostic logging to the if/else that uses the return value to confirm the code execution paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM