如何正确恢复窗口7/vista aero快照调整大小的窗口小部件

How to correctly restore widgets that are resized by windows 7/vista aero snap

本文关键字:窗口 调整 小部 快照 aero 恢复 何正确 vista      更新时间:2023-10-16

Windows 7有这样的功能,如果一个窗口被拖到屏幕的一侧,它会被最大化,占据一半。我的问题是-我正在尝试恢复小部件的大小和位置,当Windows 7"最大化"小部件时,qt仍然返回其位置和大小,就好像它仍然正常显示一样,即,位置和尺寸完全不正确。

qt5中对此有任何控制吗?我在文档中找不到它,它的奇怪

这基本上就是我最终所做的。底部的"框架几何体"部分似乎是补偿小部件边界所必需的。

QT += gui-private

在.pro文件中需要以下代码才能使用

#include <WinUser.h>
#include <qpa/qplatformnativeinterface.h>
static QWindow* windowForWidget(const QWidget* widget)
{
    QWindow* window = widget->windowHandle();
    if (window)
        return window;
    const QWidget* nativeParent = widget->nativeParentWidget();
    if (nativeParent)
        return nativeParent->windowHandle();
    return 0;
}
#include <QWindow>
static HWND getHWNDForWidget(const QWidget* widget)
{
    QWindow* window = ::windowForWidget(widget);
    if (window && window->handle())
    {
        QPlatformNativeInterface* natInterface = QGuiApplication::platformNativeInterface();
        return static_cast<HWND>(natInterface->nativeResourceForWindow(QByteArrayLiteral("handle"), window));
    }
    return 0;
}
void SaveSize(QWidget* w)
{
    QSize size;
    QPoint pos;
    RECT pRect = { 0 };
    HWND hwnd = getHWNDForWidget(w);
    GetWindowRect(hwnd, &pRect);
    auto left = w->frameGeometry().left();
    auto right = w->frameGeometry().right();
    auto width = w->width();
    pos.setX(pRect.left);
    pos.setY(pRect.top);
    size.setWidth(pRect.right - pRect.left - (right - left - width));
    size.setHeight(pRect.bottom - pRect.top);
    //.... the rest of the code
}