From e9999fa61ea0ffa602c69e10e54996b3a636a5e6 Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <2546789017@qq.com>
Date: 周五, 09 5月 2025 16:03:45 +0800
Subject: [PATCH] fix window reopen issue (#179)

---
 examples/qml/FramelessWindow.qml         |  259 ++++++++++++++++++++++++++++
 examples/qml/main.qml                    |  254 +---------------------------
 examples/qml/qml.qrc                     |    1 
 src/core/contexts/win32windowcontext.cpp |   12 +
 4 files changed, 281 insertions(+), 245 deletions(-)

diff --git a/examples/qml/FramelessWindow.qml b/examples/qml/FramelessWindow.qml
new file mode 100644
index 0000000..7a669ea
--- /dev/null
+++ b/examples/qml/FramelessWindow.qml
@@ -0,0 +1,259 @@
+import QtQuick 2.15
+import QtQuick.Window 2.15
+import QtQuick.Controls 2.15
+import Qt.labs.platform 1.1
+import QWindowKit 1.0
+
+Window {
+    property bool showWhenReady: true
+
+    id: window
+    width: 800
+    height: 600
+    color: darkStyle.windowBackgroundColor
+    title: qsTr("QWindowKit QtQuick Demo")
+    Component.onCompleted: {
+        windowAgent.setup(window)
+        windowAgent.setWindowAttribute("dark-mode", true)
+        if (window.showWhenReady) {
+            window.visible = true
+        }
+    }
+
+    QtObject {
+        id: lightStyle
+    }
+
+    QtObject {
+        id: darkStyle
+        readonly property color windowBackgroundColor: "#1E1E1E"
+    }
+
+    Timer {
+        interval: 100
+        running: true
+        repeat: true
+        onTriggered: timeLabel.text = Qt.formatTime(new Date(), "hh:mm:ss")
+    }
+
+    WindowAgent {
+        id: windowAgent
+    }
+
+    TapHandler {
+        acceptedButtons: Qt.RightButton
+        onTapped: contextMenu.open()
+    }
+
+    Rectangle {
+        id: titleBar
+        anchors {
+            top: parent.top
+            left: parent.left
+            right: parent.right
+        }
+        height: 32
+        //color: window.active ? "#3C3C3C" : "#505050"
+        color: "transparent"
+        Component.onCompleted: windowAgent.setTitleBar(titleBar)
+
+        Image {
+            id: iconButton
+            anchors {
+                verticalCenter: parent.verticalCenter
+                left: parent.left
+                leftMargin: 10
+            }
+            width: 18
+            height: 18
+            mipmap: true
+            source: "qrc:///app/example.png"
+            fillMode: Image.PreserveAspectFit
+            Component.onCompleted: windowAgent.setSystemButton(WindowAgent.WindowIcon, iconButton)
+        }
+
+        Text {
+            anchors {
+                verticalCenter: parent.verticalCenter
+                left: iconButton.right
+                leftMargin: 10
+            }
+            horizontalAlignment: Text.AlignHCenter
+            verticalAlignment: Text.AlignVCenter
+            text: window.title
+            font.pixelSize: 14
+            color: "#ECECEC"
+        }
+
+        Row {
+            anchors {
+                top: parent.top
+                right: parent.right
+            }
+            height: parent.height
+
+            QWKButton {
+                id: minButton
+                height: parent.height
+                source: "qrc:///window-bar/minimize.svg"
+                onClicked: window.showMinimized()
+                Component.onCompleted: windowAgent.setSystemButton(WindowAgent.Minimize, minButton)
+            }
+
+            QWKButton {
+                id: maxButton
+                height: parent.height
+                source: window.visibility === Window.Maximized ? "qrc:///window-bar/restore.svg" : "qrc:///window-bar/maximize.svg"
+                onClicked: {
+                    if (window.visibility === Window.Maximized) {
+                        window.showNormal()
+                    } else {
+                        window.showMaximized()
+                    }
+                }
+                Component.onCompleted: windowAgent.setSystemButton(WindowAgent.Maximize, maxButton)
+            }
+
+            QWKButton {
+                id: closeButton
+                height: parent.height
+                source: "qrc:///window-bar/close.svg"
+                background: Rectangle {
+                    color: {
+                        if (!closeButton.enabled) {
+                            return "gray";
+                        }
+                        if (closeButton.pressed) {
+                            return "#e81123";
+                        }
+                        if (closeButton.hovered) {
+                            return "#e81123";
+                        }
+                        return "transparent";
+                    }
+                }
+                onClicked: window.close()
+                Component.onCompleted: windowAgent.setSystemButton(WindowAgent.Close, closeButton)
+            }
+        }
+    }
+
+    Label {
+        id: timeLabel
+        anchors.centerIn: parent
+        font {
+            pointSize: 75
+            bold: true
+        }
+        color: "#FEFEFE"
+        Component.onCompleted: {
+            if ($curveRenderingAvailable) {
+                console.log("Curve rendering for text is available.")
+                timeLabel.renderType = Text.CurveRendering
+            }
+        }
+    }
+
+    Menu {
+        id: contextMenu
+
+        Menu {
+            id: themeMenu
+            title: qsTr("Theme")
+
+            MenuItemGroup {
+                id: themeMenuGroup
+                items: themeMenu.items
+            }
+
+            MenuItem {
+                text: qsTr("Light")
+                checkable: true
+                onTriggered: windowAgent.setWindowAttribute("dark-mode", false)
+            }
+
+            MenuItem {
+                text: qsTr("Dark")
+                checkable: true
+                checked: true
+                onTriggered: windowAgent.setWindowAttribute("dark-mode", true)
+            }
+        }
+
+        Menu {
+            id: specialEffectMenu
+            title: qsTr("Special effect")
+
+            MenuItemGroup {
+                id: specialEffectMenuGroup
+                items: specialEffectMenu.items
+            }
+
+            MenuItem {
+                enabled: Qt.platform.os === "windows"
+                text: qsTr("None")
+                checkable: true
+                checked: true
+                onTriggered: {
+                    window.color = darkStyle.windowBackgroundColor
+                    windowAgent.setWindowAttribute("dwm-blur", false)
+                    windowAgent.setWindowAttribute("acrylic-material", false)
+                    windowAgent.setWindowAttribute("mica", false)
+                    windowAgent.setWindowAttribute("mica-alt", false)
+                }
+            }
+
+            MenuItem {
+                enabled: Qt.platform.os === "windows"
+                text: qsTr("DWM blur")
+                checkable: true
+                onTriggered: {
+                    window.color = "transparent"
+                    windowAgent.setWindowAttribute("acrylic-material", false)
+                    windowAgent.setWindowAttribute("mica", false)
+                    windowAgent.setWindowAttribute("mica-alt", false)
+                    windowAgent.setWindowAttribute("dwm-blur", true)
+                }
+            }
+
+            MenuItem {
+                enabled: Qt.platform.os === "windows"
+                text: qsTr("Acrylic material")
+                checkable: true
+                onTriggered: {
+                    window.color = "transparent"
+                    windowAgent.setWindowAttribute("dwm-blur", false)
+                    windowAgent.setWindowAttribute("mica", false)
+                    windowAgent.setWindowAttribute("mica-alt", false)
+                    windowAgent.setWindowAttribute("acrylic-material", true)
+                }
+            }
+
+            MenuItem {
+                enabled: Qt.platform.os === "windows"
+                text: qsTr("Mica")
+                checkable: true
+                onTriggered: {
+                    window.color = "transparent"
+                    windowAgent.setWindowAttribute("dwm-blur", false)
+                    windowAgent.setWindowAttribute("acrylic-material", false)
+                    windowAgent.setWindowAttribute("mica-alt", false)
+                    windowAgent.setWindowAttribute("mica", true)
+                }
+            }
+
+            MenuItem {
+                enabled: Qt.platform.os === "windows"
+                text: qsTr("Mica Alt")
+                checkable: true
+                onTriggered: {
+                    window.color = "transparent"
+                    windowAgent.setWindowAttribute("dwm-blur", false)
+                    windowAgent.setWindowAttribute("acrylic-material", false)
+                    windowAgent.setWindowAttribute("mica", false)
+                    windowAgent.setWindowAttribute("mica-alt", true)
+                }
+            }
+        }
+    }
+}
diff --git a/examples/qml/main.qml b/examples/qml/main.qml
index dd4f284..7f203c4 100644
--- a/examples/qml/main.qml
+++ b/examples/qml/main.qml
@@ -1,255 +1,19 @@
 import QtQuick 2.15
 import QtQuick.Window 2.15
 import QtQuick.Controls 2.15
-import Qt.labs.platform 1.1
-import QWindowKit 1.0
 
-Window {
-    id: window
-    width: 800
-    height: 600
-    color: darkStyle.windowBackgroundColor
-    title: qsTr("Hello, world!")
-    Component.onCompleted: {
-        windowAgent.setup(window)
-        windowAgent.setWindowAttribute("dark-mode", true)
-        window.visible = true
+FramelessWindow {
+    property FramelessWindow childWindow: FramelessWindow {
+        showWhenReady: false
     }
 
-    QtObject {
-        id: lightStyle
-    }
-
-    QtObject {
-        id: darkStyle
-        readonly property color windowBackgroundColor: "#1E1E1E"
-    }
-
-    Timer {
-        interval: 100
-        running: true
-        repeat: true
-        onTriggered: timeLabel.text = Qt.formatTime(new Date(), "hh:mm:ss")
-    }
-
-    WindowAgent {
-        id: windowAgent
-    }
-
-    TapHandler {
-        acceptedButtons: Qt.RightButton
-        onTapped: contextMenu.open()
-    }
-
-    Rectangle {
-        id: titleBar
+    Button {
         anchors {
-            top: parent.top
-            left: parent.left
-            right: parent.right
+            horizontalCenter: parent.horizontalCenter
+            bottom: parent.bottom
+            bottomMargin: 20
         }
-        height: 32
-        //color: window.active ? "#3C3C3C" : "#505050"
-        color: "transparent"
-        Component.onCompleted: windowAgent.setTitleBar(titleBar)
-
-        Image {
-            id: iconButton
-            anchors {
-                verticalCenter: parent.verticalCenter
-                left: parent.left
-                leftMargin: 10
-            }
-            width: 18
-            height: 18
-            mipmap: true
-            source: "qrc:///app/example.png"
-            fillMode: Image.PreserveAspectFit
-            Component.onCompleted: windowAgent.setSystemButton(WindowAgent.WindowIcon, iconButton)
-        }
-
-        Text {
-            anchors {
-                verticalCenter: parent.verticalCenter
-                left: iconButton.right
-                leftMargin: 10
-            }
-            horizontalAlignment: Text.AlignHCenter
-            verticalAlignment: Text.AlignVCenter
-            text: window.title
-            font.pixelSize: 14
-            color: "#ECECEC"
-        }
-
-        Row {
-            anchors {
-                top: parent.top
-                right: parent.right
-            }
-            height: parent.height
-
-            QWKButton {
-                id: minButton
-                height: parent.height
-                source: "qrc:///window-bar/minimize.svg"
-                onClicked: window.showMinimized()
-                Component.onCompleted: windowAgent.setSystemButton(WindowAgent.Minimize, minButton)
-            }
-
-            QWKButton {
-                id: maxButton
-                height: parent.height
-                source: window.visibility === Window.Maximized ? "qrc:///window-bar/restore.svg" : "qrc:///window-bar/maximize.svg"
-                onClicked: {
-                    if (window.visibility === Window.Maximized) {
-                        window.showNormal()
-                    } else {
-                        window.showMaximized()
-                    }
-                }
-                Component.onCompleted: windowAgent.setSystemButton(WindowAgent.Maximize, maxButton)
-            }
-
-            QWKButton {
-                id: closeButton
-                height: parent.height
-                source: "qrc:///window-bar/close.svg"
-                background: Rectangle {
-                    color: {
-                        if (!closeButton.enabled) {
-                            return "gray";
-                        }
-                        if (closeButton.pressed) {
-                            return "#e81123";
-                        }
-                        if (closeButton.hovered) {
-                            return "#e81123";
-                        }
-                        return "transparent";
-                    }
-                }
-                onClicked: window.close()
-                Component.onCompleted: windowAgent.setSystemButton(WindowAgent.Close, closeButton)
-            }
-        }
-    }
-
-    Label {
-        id: timeLabel
-        anchors.centerIn: parent
-        font {
-            pointSize: 75
-            bold: true
-        }
-        color: "#FEFEFE"
-        Component.onCompleted: {
-            if ($curveRenderingAvailable) {
-                console.log("Curve rendering for text is available.")
-                timeLabel.renderType = Text.CurveRendering
-            }
-        }
-    }
-
-    Menu {
-        id: contextMenu
-
-        Menu {
-            id: themeMenu
-            title: qsTr("Theme")
-
-            MenuItemGroup {
-                id: themeMenuGroup
-                items: themeMenu.items
-            }
-
-            MenuItem {
-                text: qsTr("Light")
-                checkable: true
-                onTriggered: windowAgent.setWindowAttribute("dark-mode", false)
-            }
-
-            MenuItem {
-                text: qsTr("Dark")
-                checkable: true
-                checked: true
-                onTriggered: windowAgent.setWindowAttribute("dark-mode", true)
-            }
-        }
-
-        Menu {
-            id: specialEffectMenu
-            title: qsTr("Special effect")
-
-            MenuItemGroup {
-                id: specialEffectMenuGroup
-                items: specialEffectMenu.items
-            }
-
-            MenuItem {
-                enabled: Qt.platform.os === "windows"
-                text: qsTr("None")
-                checkable: true
-                checked: true
-                onTriggered: {
-                    window.color = darkStyle.windowBackgroundColor
-                    windowAgent.setWindowAttribute("dwm-blur", false)
-                    windowAgent.setWindowAttribute("acrylic-material", false)
-                    windowAgent.setWindowAttribute("mica", false)
-                    windowAgent.setWindowAttribute("mica-alt", false)
-                }
-            }
-
-            MenuItem {
-                enabled: Qt.platform.os === "windows"
-                text: qsTr("DWM blur")
-                checkable: true
-                onTriggered: {
-                    window.color = "transparent"
-                    windowAgent.setWindowAttribute("acrylic-material", false)
-                    windowAgent.setWindowAttribute("mica", false)
-                    windowAgent.setWindowAttribute("mica-alt", false)
-                    windowAgent.setWindowAttribute("dwm-blur", true)
-                }
-            }
-
-            MenuItem {
-                enabled: Qt.platform.os === "windows"
-                text: qsTr("Acrylic material")
-                checkable: true
-                onTriggered: {
-                    window.color = "transparent"
-                    windowAgent.setWindowAttribute("dwm-blur", false)
-                    windowAgent.setWindowAttribute("mica", false)
-                    windowAgent.setWindowAttribute("mica-alt", false)
-                    windowAgent.setWindowAttribute("acrylic-material", true)
-                }
-            }
-
-            MenuItem {
-                enabled: Qt.platform.os === "windows"
-                text: qsTr("Mica")
-                checkable: true
-                onTriggered: {
-                    window.color = "transparent"
-                    windowAgent.setWindowAttribute("dwm-blur", false)
-                    windowAgent.setWindowAttribute("acrylic-material", false)
-                    windowAgent.setWindowAttribute("mica-alt", false)
-                    windowAgent.setWindowAttribute("mica", true)
-                }
-            }
-
-            MenuItem {
-                enabled: Qt.platform.os === "windows"
-                text: qsTr("Mica Alt")
-                checkable: true
-                onTriggered: {
-                    window.color = "transparent"
-                    windowAgent.setWindowAttribute("dwm-blur", false)
-                    windowAgent.setWindowAttribute("acrylic-material", false)
-                    windowAgent.setWindowAttribute("mica", false)
-                    windowAgent.setWindowAttribute("mica-alt", true)
-                }
-            }
-        }
+        text: qsTr("Open Child Window")
+        onClicked: childWindow.visible = true
     }
 }
\ No newline at end of file
diff --git a/examples/qml/qml.qrc b/examples/qml/qml.qrc
index 865977e..7eaf353 100644
--- a/examples/qml/qml.qrc
+++ b/examples/qml/qml.qrc
@@ -2,5 +2,6 @@
     <qresource prefix="/">
         <file>main.qml</file>
         <file>QWKButton.qml</file>
+        <file>FramelessWindow.qml</file>
     </qresource>
 </RCC>
\ No newline at end of file
diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 7ffd7da..e155d3d 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -1909,6 +1909,18 @@
                 break;
             }
 
+            case WM_SHOWWINDOW: {
+                if (!wParam || !isWindowNoState(hWnd) || isFullScreen(hWnd)) {
+                    break;
+                }
+                RECT windowRect{};
+                ::GetWindowRect(hWnd, &windowRect);
+                static constexpr const auto swpFlags = SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_NOOWNERZORDER;
+                ::SetWindowPos(hWnd, nullptr, 0, 0, RECT_WIDTH(windowRect) + 1, RECT_HEIGHT(windowRect) + 1, swpFlags);
+                ::SetWindowPos(hWnd, nullptr, 0, 0, RECT_WIDTH(windowRect), RECT_HEIGHT(windowRect), swpFlags);
+                break;
+            }
+
             default:
                 break;
         }

--
Gitblit v1.9.1