c++winapi

How to update WC_EDIT control margins when resized


This program creates WC_EDIT control inside the main window. The main window resizes edit control whenever the window itself is resized. I am trying to set margins of edit control content by updating formatting rectange of WC_EDIT control whenever it is resized.

Main window responds to WM_SIZE message and resizes edit control, then edit control responds to its own WM_SIZE message and tries to update formatting rectangle using EM_SETRECT, but nothing happens.

#include <windows.h>
#include <commctrl.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK EditWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

HWND hwnd;
HWND editHwnd;
WNDPROC origEditProc;

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
{

    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = "Window Class";

    RegisterClass(&wc);

    hwnd = CreateWindow(
        wc.lpszClassName, 
        "",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL
    );

    editHwnd = CreateWindow(
        WC_EDIT,
        "Lorem ipsum",
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL,
        0,
        0,
        100,
        100,
        hwnd,
        NULL,
        hInstance,
        NULL
    );

    origEditProc = (WNDPROC) GetWindowLongPtr(editHwnd, GWLP_WNDPROC);
    SetWindowLongPtr(editHwnd, GWLP_WNDPROC, (LONG_PTR)EditWindowProc);

    ShowWindow(hwnd, nCmdShow);

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        return 0;
        case WM_SIZE:
            RECT clientRect;
            GetClientRect(hwnd, &clientRect);
            SetWindowPos(editHwnd, NULL, 0, 0, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top,
                SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

LRESULT CALLBACK EditWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_SIZE:
            RECT rect;
            GetClientRect(editHwnd, &rect);
            rect.left += 50;
            rect.top += 50;
            rect.right -= 50;
            rect.bottom -= 50;
            SendMessage(editHwnd, EM_SETRECT, NULL, (LPARAM)&rect);
    }
    return CallWindowProc(origEditProc, hwnd, uMsg, wParam, lParam);
}

Solution

  • Original window procedure of the WC_EDIT control should be called before updating formatting rectangle:

    LRESULT CALLBACK EditWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        LRESULT result = CallWindowProc(origEditProc, hwnd, uMsg, wParam, lParam);
        switch (uMsg)
        {
            case WM_SIZE:
                RECT rect;
                GetClientRect(editHwnd, &rect);
                rect.left += 50;
                rect.top += 50;
                rect.right -= 50;
                rect.bottom -= 50;
                SendMessage(editHwnd, EM_SETRECT, NULL, (LPARAM)&rect);
        }
        return result;
    }