【问题标题】:How can I handle the resize of children windows when the parent windows is resized?调整父窗口大小时如何处理子窗口的大小调整?
【发布时间】:2023-03-24 06:50:01
【问题描述】:

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

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

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

【问题讨论】:

  • 但看起来你甚至没有尝试移动/调整孩子的大小。你不要给孩子打电话MoveWindow。但最好使用BeginDeferWindowPos + DeferWindowPos + EndDeferWindowPos
  • MoveWndow 可以正常工作。大概您的代码在某些方面存在缺陷。如果您需要帮助,您需要显示您的代码。
  • 所以对于 MoveWindow 函数,我对如何获取子窗口与父窗口的相对位置感到困惑。那是我感到困惑的部分。

标签: c++ winapi win32gui


【解决方案1】:

我使用全局RECT 来存储编辑控件的左、上、宽和高(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;
}

【讨论】:

  • 不幸的是,这不起作用。当我更改父窗口的大小时,子窗口并没有随之移动。这里是全局的:#define ID_Edit1 200 RECT editSize = { 100, 50 , 100, 100 }; RECT rcClient; WM_Size:case WM_SIZE: GetClientRect(hName, &rcClient); EnumChildWindows(hName, EnumChildProc, (LPARAM)&rcClient); break; EnumChildProc 的代码没有变化。我该怎么办?
  • 我的主窗口大小是(100, 100, 900, 700)
  • 我添加了两张截图。使用全局变量存储原始大小。当主窗口大小发生变化时,计算变化率,乘以控件原来的大小,得到变化后的大小,使用MoveWindow改变大小。
  • 我已经这样做了,但它仍然不起作用。这是一些图像。 WM_SIZEGlobalEnum
  • 一定是某个地方出了问题。我的主窗口:hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, 100, 100, 900, 700, nullptr, nullptr, hInstance, nullptr);,创建编辑控件case WM_CREATE: // Create three invisible child windows. editControl = CreateWindowEx(0, L"Edit", (LPCTSTR)NULL, WS_CHILD | WS_BORDER | WS_VISIBLE, editSize.left, editSize.top, editSize.right, editSize.bottom, hWnd, (HMENU)(int)ID_Edit1, hInst, NULL); return 0;。你能告诉我你的整个代码吗?
猜你喜欢
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多