当我在 TextBox 中输入一个值,然后单击按钮时, MessageBox 必须显示输入的值,而不是空的MessageBox。
那么,如何在单击按钮后使输入的值显示在MessageBox上?

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {

HWND TextBox;
char textSaved[20];
    switch(Message) {
        case WM_CREATE:

        TextBox = CreateWindow("EDIT",
                                "",
                                WS_BORDER | WS_CHILD | WS_VISIBLE,
                                10,10,300,20,
                                hwnd, NULL, NULL, NULL);
        CreateWindow("BUTTON",
                    "Go",
                    WS_VISIBLE | WS_CHILD | WS_BORDER,
                    420, 10, 70, 20,
                    hwnd, (HMENU) 1, NULL, NULL);

        break;

        case WM_COMMAND:

            switch(LOWORD(wParam))
            {
                case 1:

                int gwtstat =0;
                gwtstat = GetWindowText(TextBox, &textSaved[0], 20);
                ::MessageBox(hwnd, textSaved, textSaved, MB_OK);
                break;
            }

        break;

        case WM_DESTROY: { PostQuitMessage(0);break;}
        default:
            return DefWindowProc(hwnd, Message, wParam, lParam);
    }
    return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    memset(&wc,0,sizeof(wc));
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInstance;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    //HBRUSH brush = CreateSolidBrush(RGB(0, 0, 255));
    wc.hbrBackground = CreateSolidBrush(RGB(20, 60, 12));//(brush)(COLOR_WINDOW+1);
    wc.lpszClassName = "WindowClass";
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        640,
        480,
        NULL,NULL,hInstance,NULL);

    if(hwnd == NULL) {
        MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
        return 0;
    }

    while(GetMessage(&Msg, NULL, 0, 0) > 0) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

最佳答案

HWND TextBox;中声明WndProc意味着每次您的窗口过程被调用时,其值都被重置(为未初始化的值)。如果要保存值,则需要设置变量static或global。

关于c++ - 获取TextBox值并将其显示为MessageBox WinApi,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27260843/

10-17 00:18