GetWindowtext没有返回任何内容

时间:2017-05-21 20:09:29

标签: c++ winapi visual-studio-2017

我正在尝试学习如何使用visual studio为C ++制作GUI。我试图创建一个按钮,当你按下它时会出现一个带有编辑窗口内容的文本框,但GetWindowText函数在我运行程序时返回一个空白字符串。我试图调整缓冲区的大小以查看是否存在问题,但我无法以这种方式解决问题。我已经尝试使用数字而不是GetWindowTextLength()funktion,但我不能让它以这种方式工作。

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{`      HMENU hMenubar = CreateMenu();
    HMENU hFile = CreateMenu();
    HMENU hOptions = CreateMenu();

    AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hFile, L"File");
    AppendMenu(hMenubar, MF_POPUP, NULL, L"Edit");
    AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hOptions, L"Options");

    AppendMenu(hFile, MF_STRING, NULL, L"Exit");
    AppendMenu(hOptions, MF_STRING, NULL, L"option 1");
    AppendMenu(hOptions, MF_STRING, NULL, L"option 2");

    SetMenu(hWnd, hMenubar);

    CreateWindow(TEXT("button"), TEXT("Hello"),
        WS_VISIBLE | WS_CHILD,
        10, 10, 80, 25,
        hWnd, (HMENU) ID_BUTTON1, NULL, NULL);

    static HWND hWndTextbox = CreateWindow(TEXT("edit"), TEXT("sim"),
        WS_VISIBLE | WS_CHILD|WS_BORDER | ES_AUTOHSCROLL,
        90, 120, 300, 20,
        hWnd, (HMENU) ID_TEXT3, NULL, NULL);

    CreateWindow(TEXT("button"), TEXT("shiny"),
        WS_VISIBLE | WS_CHILD,
        50, 50, 80, 50,
        hWnd, (HMENU) ID_BUTTON2, NULL, NULL);
}
break;
case WM_COMMAND:
    {
    if (LOWORD(wParam) == ID_BUTTON1) {
        MessageBox(hWnd, TEXT("Button has been clicked"), TEXT("title for popup"), MB_ICONINFORMATION);
    }
    if (LOWORD(wParam) == ID_BUTTON2) {
        // create some default vars
        // Allocate buffer including terminating null

        int length = GetWindowTextLength(hWndTextbox) + 1;
        std::wstring uinput(GetWindowTextLength(hWndTextbox) + 1, 0);

        // Address of first character is used to obtain pointer to non-const data
        // (as opposed to wstring::c_str()).
        int size = GetWindowText(hWndTextbox, &uinput[0], length+1);

        // Resize buffer to the actual text length
        uinput.resize(size);

        // MessageBox only needs pointer to const string, so we can use wstring::c_str() here.
        MessageBox(hWnd, uinput.c_str(), TEXT("Message box"), MB_OK);

    }
    break;
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
    }
    break;
case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        // TODO: Add any drawing code that uses hdc here...
        EndPaint(hWnd, &ps);
    }
    break;
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}`

其余的代码是由visual studio自动生成的,所以我只包含了我做出更改的部分

0 个答案:

没有答案
相关问题