2017-04-19 5 views
0

Ich muss eine rahmenlose Qt Windows App erstellen, die die Größenänderung unterstützt.Qt/Windows, veränderbares rahmenloses Fenster

Wenn ich

verwenden
setWindowFlags(Qt::FramelessWindowHint); 

dann kann ich nur von unten rechts der Größe (wie mit der Größe Griff, ich denke, es QMainWindow umfasst irgendwie?).

Gibt es eine einfache Möglichkeit, es von allen Seiten wie ein normales Fenster in der Größe veränderbar zu machen? Vielleicht etwas plattformspezifisch (Windows)?

+1

bewegen Sie kann [WM_NCHITTEST] (https://msdn.microsoft.com/en-us/library/windows/desktop/ms645618 (v = vs.85) .aspx) verarbeiten und gibt beispielsweise * HTLEFT * zurück, wenn X_cursor - X_window <= SM_CXBORDER . und so für 4 Seiten – RbMm

+1

Siehe http://stackoverflow.com/a/37507341/4149835 –

Antwort

2

Gelöst es mit WM_NCHITTEST, basierend auf Code aus https://bugreports.qt.io/browse/QTBUG-40578 (Fehlerbericht nicht im Zusammenhang damit).

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

Implementierung von Qt: Resize borderless widget nicht gut funktioniert: manchmal das Fenster während einer Größenänderung (auch in der ersten Version ohne Ziehen)

Verwandte Themen