【问题标题】:Qt/Windows, resizable frameless windowQt/Windows,可调整大小的无框窗口
【发布时间】:2017-09-16 06:21:06
【问题描述】:

我需要创建一个支持调整大小的无框架 Qt Windows 应用程序。

如果我使用

setWindowFlags(Qt::FramelessWindowHint);

那么我只能从右下角调整大小(就像使用大小手柄一样,我猜 QMainWindow 以某种方式包含它?)。

有没有什么简单的方法可以让它像普通窗口一样从各个方向调整大小?也许是特定于平台的东西(Windows)?

【问题讨论】:

标签: c++ qt winapi


【解决方案1】:

使用 WM_NCHITTEST 解决了这个问题,基于来自 https://bugreports.qt.io/browse/QTBUG-40578 的代码(与此无关的错误报告)。

.h

class MainWindow : public QMainWindow
{
    Q_OBJECT
......
protected:
    bool nativeEvent(const QByteArray& eventType, void* message, long* result) override;
};

.cpp

bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
    MSG* msg = static_cast<MSG*>(message);

    if (msg->message == WM_NCHITTEST)
    {
        if (isMaximized())
        {
            return false;
        }

        *result = 0;
        const LONG borderWidth = 8;
        RECT winrect;
        GetWindowRect(reinterpret_cast<HWND>(winId()), &winrect);

        // must be short to correctly work with multiple monitors (negative coordinates)
        short x = msg->lParam & 0x0000FFFF;
        short y = (msg->lParam & 0xFFFF0000) >> 16;

        bool resizeWidth = minimumWidth() != maximumWidth();
        bool resizeHeight = minimumHeight() != maximumHeight();
        if (resizeWidth)
        {
            //left border
            if (x >= winrect.left && x < winrect.left + borderWidth)
            {
                *result = HTLEFT;
            }
            //right border
            if (x < winrect.right && x >= winrect.right - borderWidth)
            {
                *result = HTRIGHT;
            }
        }
        if (resizeHeight)
        {
            //bottom border
            if (y < winrect.bottom && y >= winrect.bottom - borderWidth)
            {
                *result = HTBOTTOM;
            }
            //top border
            if (y >= winrect.top && y < winrect.top + borderWidth)
            {
                *result = HTTOP;
            }
        }
        if (resizeWidth && resizeHeight)
        {
            //bottom left corner
            if (x >= winrect.left && x < winrect.left + borderWidth &&
                y < winrect.bottom && y >= winrect.bottom - borderWidth)
            {
                *result = HTBOTTOMLEFT;
            }
            //bottom right corner
            if (x < winrect.right && x >= winrect.right - borderWidth &&
                y < winrect.bottom && y >= winrect.bottom - borderWidth)
            {
                *result = HTBOTTOMRIGHT;
            }
            //top left corner
            if (x >= winrect.left && x < winrect.left + borderWidth &&
                y >= winrect.top && y < winrect.top + borderWidth)
            {
                *result = HTTOPLEFT;
            }
            //top right corner
            if (x < winrect.right && x >= winrect.right - borderWidth &&
                y >= winrect.top && y < winrect.top + borderWidth)
            {
                *result = HTTOPRIGHT;
            }
        }

        if (*result != 0)
            return true;

        QWidget *action = QApplication::widgetAt(QCursor::pos());
        if (action == this){
            *result = HTCAPTION;
            return true;
        }
    }

    return false;
}

Qt: Resize borderless widget 的实现效果不佳:有时窗口在调整大小时会移动(即使在第一个版本中没有拖动)

【讨论】:

  • 这是 Windows 特定的解决方案。
猜你喜欢
  • 1970-01-01
  • 2011-09-26
  • 2013-07-12
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多