How can I handle the resize of children windows when the parent windows is resized?

*爱你&永不变心* 提交于 2020-01-26 04:56:11

问题


So I have been trying to accomplish this for a bit now. I am having trouble handling the resize of the children windows when the parent window is resized. When I do not handle the resize, the parent window is resized and the child windows stay in the same place.

I have know that this has to be in the message of WM_SIZE but I do not know how to handle the rest from there. I have tried the MoveWindow() and UpdateWindow() function but it didn't seem to work for me.

I have been trying to get this window child to resize correctly: hName = CreateWindowW(L"Edit", L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 50, 98, 38, hWnd, NULL, NULL, NULL);. So far nothing has worked. Help is appreciated! Thanks!


回答1:


I use a global RECT to storage the left, top, width and height of Edit control(RECT editSize = { 100, 50 , 100, 100 }) . In WM_SIZE message, call EnumChildWindows, resize my child windows in 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;
}



来源:https://stackoverflow.com/questions/58676439/how-can-i-handle-the-resize-of-children-windows-when-the-parent-windows-is-resiz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!