调整父窗口大小后如何处理子窗口的大小?

问题描述 投票:-2回答:1

因此,我已经尝试了一段时间。调整父窗口的大小时,我无法处理子窗口的调整大小。当我不处理调整大小时,将调整父窗口的大小,而将子窗口留在同一位置。

我知道这必须在WM_SIZE的消息中,但是我不知道如何从那里处理其余的消息。我已经尝试过MoveWindow()和UpdateWindow()函数,但是它似乎不适用于我。

我一直在尝试让此窗口子项正确调整大小:hName = CreateWindowW(L"Edit", L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 50, 98, 38, hWnd, NULL, NULL, NULL);。到目前为止,没有任何效果。感谢帮助!谢谢!

c++ winapi win32gui
1个回答
0
投票

我使用全局RECT来存储Edit控件(RECT editSize = { 100, 50 , 100, 100 })的左侧,顶部,宽度和高度。在WM_SIZE消息中,调用EnumChildWindows,在EnumChildProc]中调整我的子窗口大小

case WM_SIZE:
        GetClientRect(hWnd, &rcClient);
        EnumChildWindows(hWnd, EnumChildProc, (LPARAM)&rcClient);
        return 0;

EnumChildProc

#define ID_Edit1  200 

...

BOOL CALLBACK EnumChildProc(HWND hwndChild, LPARAM lParam)
{
    int idChild;
    idChild = GetWindowLong(hwndChild, GWL_ID);
    LPRECT rcParent;
    rcParent = (LPRECT)lParam;

    if (idChild == ID_Edit1) {

        //Calculate the change ratio
        double cxRate = rcParent->right * 1.0 / 884; //884 is width of client area
        double cyRate = rcParent->bottom * 1.0 / 641; //641 is height of client area

        LONG newRight = editSize.left * cxRate;
        LONG newTop = editSize.top * cyRate;
        LONG newWidth = editSize.right * cxRate;
        LONG newHeight = editSize.bottom * cyRate;

        MoveWindow(hwndChild, newRight, newTop, newWidth, newHeight, TRUE);

        // Make sure the child window is visible. 
        ShowWindow(hwndChild, SW_SHOW);
    }
    return TRUE;
}

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.