将文本附加到窗口最终会停止添加文本(Win32)

时间:2019-06-20 18:09:30

标签: c++ winapi win32gui

我正在尝试将文本追加到子窗口中。在写了许多文本之后,没有其他内容被附加。我尝试捕获错误并在停止添加文本时遍历调试器,但是没有什么不同寻常的-调试器遍历代码并且将不添加任何文本,并且不会显示任何错误。

这是我的附加函数:

void AppendText(HWND hEditWnd, std::string Text) {
    int idx = GetWindowTextLength(hEditWnd);
    SetLastError(NOERROR);
    SendMessage(hEditWnd, WM_SETFOCUS, (WPARAM)hEditWnd, 0);
    SendMessage(hEditWnd, EM_SETSEL, (WPARAM) idx, (LPARAM) idx);
    SendMessage(hEditWnd, EM_REPLACESEL, WPARAM(TRUE), (LPARAM) ((LPSTR) Text.c_str()));
    ULONG dwErrorCode = GetLastError();
    if (dwErrorCode != NOERROR)
    {
        printf("fail with %u error\n", dwErrorCode);
    }
}

这里有一些示例代码可以尝试。为了延迟函数调用,我放置了一个for循环以使程序紧缩。约30秒后,文本将停止添加到窗口中。

#include <windows.h>
#include <tchar.h>
#include <string>
#include <windowsx.h>

#define ID_GO 1
#define ID_GO_ONCE 2

const char g_szClassName[] = "myWindowClass";
HWND hOut;

void AppendText(HWND hEditWnd, std::string Text) {
    int idx = GetWindowTextLength(hEditWnd);
    SetLastError(NOERROR);
    SendMessage(hEditWnd, WM_SETFOCUS, (WPARAM)hEditWnd, 0);
    SendMessage(hEditWnd, EM_SETSEL, (WPARAM) idx, (LPARAM) idx);
    SendMessage(hEditWnd, EM_REPLACESEL, WPARAM(TRUE), (LPARAM) ((LPSTR) Text.c_str()));
    ULONG dwErrorCode = GetLastError();
    if (dwErrorCode != NOERROR)
    {
        printf("fail with %u error\n", dwErrorCode);
    }
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {
            HMENU hMenu, hSubMenu;

            hMenu = CreateMenu();

            hSubMenu = CreatePopupMenu();
            AppendMenu(hSubMenu, MF_STRING, ID_GO_ONCE, "&Go Once");
            AppendMenu(hSubMenu, MF_STRING, ID_GO, "&Go");
            AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "&Output");
            SetMenu(hwnd, hMenu);

        }
            break;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case ID_GO_ONCE:
                    AppendText(hOut, "-----HELLO-----\r\n");
                    AppendText(hOut, "-----TEST-----\r\n");
                    break;
                case ID_GO:
                    while(1) {
                        AppendText(hOut, "-----HELLO-----\r\n");
                        for(int q = 0; q < 7000000; q++);
                        AppendText(hOut, "-----TEST-----\r\n");
                        for(int q = 0; q < 7000000; q++);
                    }
                    break;
            }
            break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    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,
            g_szClassName,
            "Test GUI",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 680, 402,
            NULL, NULL, hInstance, NULL);

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

    hOut = CreateWindowW(L"Edit", L"",  WS_CHILD | WS_VISIBLE | ES_LEFT | ES_MULTILINE |
                                        ES_AUTOHSCROLL | ES_AUTOVSCROLL, 0, 0, 680 - 20, 340, hwnd, NULL, NULL, NULL);

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

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

在一个窗口中可以显示多少个文字是否有限制?

0 个答案:

没有答案