Search code examples
c++windowswinapiwin32gui

Pop up window to child window in win API C++?


I am making an application in WINAPI 32 using C++. I have a parent window and a child window and a check box to make the child window pop out of parent window or dock in if unchecked

I use these methods to do that. It works fine when I move the parent window around , the child dock back into correct position

If I move child window around , it has this offset and does not fit correctly into parent.

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

}

Do I update it here? Or anything else I should take care of ?


Solution

  • I had to update the position of child manually so that the move wont affect it.

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