当父窗口调整大小时,如何处理子窗口的调整大小? [英] How can I handle the resize of children windows when the parent windows is resized?

查看:56
本文介绍了当父窗口调整大小时,如何处理子窗口的调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在努力实现这一点.调整父窗口的大小时,我无法处理子窗口的调整大小.当我不处理调整大小时,父窗口被调整大小,子窗口保持在同一个地方.

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.

我知道这必须在 WM_SIZE 的消息中,但我不知道如何从那里处理其余部分.我尝试过 MoveWindow() 和 UpdateWindow() 函数,但它似乎对我不起作用.

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.

我一直在尝试让这个窗口孩子正确调整大小:hName = CreateWindowW(L"Edit", L"", WS_CHILD | WS_VISIBLE | WS_BORDER, 200, 50, 98, 38, hWnd, NULL, NULL, NULL);.到目前为止,一切都没有奏效.帮助表示赞赏!谢谢!

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!

推荐答案

我使用一个全局的 RECT 来存储 Edit 控件的左、上、宽和高(RECT editSize = {100, 50, 100, 100 }) .在 WM_SIZE 消息中,调用 EnumChildWindows,在 EnumChildProc

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;
}

这篇关于当父窗口调整大小时,如何处理子窗口的调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆