win API C++中弹出窗口到子窗口?

问题描述 投票:0回答:1

我正在使用 C++ 在 WINAPI 32 中制作一个应用程序。我有一个父窗口和一个子窗口以及一个复选框,可以使子窗口弹出父窗口或在未选中的情况下停靠

我使用这些方法来做到这一点。当我移动父窗口、子窗口回到正确位置时,效果很好

如果我移动子窗口,它会有这个偏移量并且不能正确地适合父窗口。

 void ChangeWindowToPopup(HWND hwnd) {
    LONG style = GetWindowLong(hwnd, GWL_STYLE);

    style &= ~WS_CHILD; 
    style |= WS_OVERLAPPEDWINDOW;  
    style &= ~WS_MAXIMIZEBOX;      
    // Apply the new style
    SetWindowLong(hwnd, GWL_STYLE, style);
    SetParent(hwnd, NULL);
    }

void RevertToChildWindow(HWND hwnd, HWND parent) {


    LONG style = GetWindowLong(hwnd, GWL_STYLE);
    style &= ~WS_OVERLAPPEDWINDOW; 
    style |= WS_CHILD;             

    SetWindowLong(hwnd, GWL_STYLE, style);

    SetParent(hwnd, parent);

}

我在这里更新吗?或者还有什么需要我注意的吗?

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

我必须手动更新孩子的位置,这样移动就不会影响它。

void RevertToChildWindow(HWND hwnd, HWND parent)
    {
        LONG style = GetWindowLong(hwnd, GWL_STYLE);
        style &= ~WS_OVERLAPPEDWINDOW;
        style |= WS_CHILD;
        SetWindowLong(hwnd, GWL_STYLE, style);
        SetParent(hwnd, parent); // Adjust position and size to dock into the parent
        RECT parentRect;
        GetClientRect(parent, &parentRect); // Get parent client area
        SetWindowPos(hwnd, NULL, parentRect.left, parentRect.top + 10,
            parentRect.right - parentRect.left, parentRect.bottom - parentRect.top-10,
            SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
    }
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.