Windows api questions

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

Problems processing keyboard messages I executed the code for processing by entering the enter key, but the result is slightly different. When you enter the enter key, you change the line, but all the previous inputs disappear Why is this happening? How do I change the line while maintaining the previous inputs? Since the str array variable is declared static, shouldn't the saved contents be maintained?

#include <windows.h>
#include <TCHAR.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    HWND     hwnd;
    MSG      msg;
    WNDCLASS WndClass;
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = WndProc;
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hInstance = hInstance;
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    WndClass.hCursor = LoadCursor (NULL, IDC_ARROW); // Specify Cursor

    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.lpszMenuName = NULL;
    WndClass.lpszClassName = _T("Window Class Name");
    RegisterClass(&WndClass);
    hwnd = CreateWindow(_T("Window Class Name"),
        _T("Window Title Name"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    static TCHAR str[100];
    static int count, yPos;

    switch (iMsg)
    {
    case WM_CREATE:
        yPos = 0;
        count = 0;
        break;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        TextOut(hdc, 0, yPos, str, _tcslen(str));
        EndPaint(hwnd, &ps);
        break;

    case WM_CHAR:

        if (wParam == VK_RETURN) {
            yPos = yPos + 20;
            count = 0;
        }
        else 
            str[count++] = wParam;

        str[count] = NULL;

        InvalidateRgn(hwnd, NULL, TRUE);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

api

2022-09-20 15:02

1 Answers

The problem is that the entire screen is updated because we are using the InvalidRgn function and we put NULL as the second argument of the function. As the entire screen is updated, the previous value is erased, and then a new line is written as a TextOut function.

The TextOut function can only write one line, while the DrawText function can write multiple lines at once.

There are various solutions. And there are many problems with the current code.

Anyway, you can modify it as below to solve the question.

#include <windows.h>
#include <TCHAR.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    HWND     hwnd;
    MSG      msg;
    WNDCLASS WndClass;
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = WndProc;
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hInstance = hInstance;
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);

    WndClass.hCursor = LoadCursor (NULL, IDC_ARROW); // Specify Cursor

    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.lpszMenuName = NULL;
    WndClass.lpszClassName = _T("Window Class Name");
    RegisterClass(&WndClass);
    hwnd = CreateWindow(_T("Window Class Name"),
        _T("Window Title Name"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    static TCHAR str[100] = { 0 };
    static int count, yPos;
    RECT rect{ 0,0,300,300 };

    switch (iMsg)
    {
    case WM_CREATE:
        yPos = 0;
        count = 0;
        break;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        DrawText(hdc, str, _tcslen(str), &rect, DT_LEFT | DT_EXTERNALLEADING | DT_WORDBREAK);
        EndPaint(hwnd, &ps);
        break;

    case WM_CHAR:

        if (wParam == VK_RETURN) {
            str[count++] = '\n';
        }
        else
            str[count++] = wParam;

        str[count] = NULL;

        InvalidateRgn(hwnd, NULL, TRUE);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}


2022-09-20 15:02

If you have any answers or tips


© 2022 pinfo. All rights reserved.