2014-04-24 45 views
1

Windows 7具有此功能,其中如果將窗口拖動到屏幕一側,則可以將其最大化爲一半。我的問題是 - 我試圖實現窗口小部件大小和位置的恢復,當Windows 7「最大化」窗口小部件時,qt仍然返回其位置和大小,就好像它仍然正常顯示,即 - 完全不正確的位置和大小。如何正確恢復由Windows 7/vista aero snap大小調整的窗口小部件

在qt5中有沒有這方面的控制權?我無法在文檔中的任何地方找到它,也很奇怪

回答

1

這實際上就是我最終做的。 底部的「framegeometry」部分是必要的,以彌補它看起來的widget的邊界。

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 
} 
相關問題