WindowsFramelessHelper.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include <QAbstractNativeEventFilter>
  3. #include <QWindow>
  4. class WindowsFramelessHelper;
  5. class TitleBar;
  6. class NativeWindowFilter : public QAbstractNativeEventFilter
  7. {
  8. public:
  9. static void deliver(QWindow *window, WindowsFramelessHelper *helper);
  10. protected:
  11. bool nativeEventFilter(const QByteArray &eventType,
  12. void *message, long *result) final;
  13. private:
  14. static QScopedPointer<NativeWindowFilter> s_instance;
  15. static QHash<WindowsFramelessHelper *, WId> s_windows;
  16. static QHash<QWindow *, WId> s_winIds;
  17. static QHash<WId, WindowsFramelessHelper *> s_helpers;
  18. };
  19. class WindowsFramelessHelper : public QObject
  20. {
  21. Q_OBJECT
  22. public:
  23. explicit WindowsFramelessHelper(QWidget *widget);
  24. ~WindowsFramelessHelper();
  25. void setMaximizedMargins(int left, int top, int right, int bottom);
  26. void setMaximizedMargins(const QMargins &margins);
  27. void setDraggableMargins(int left, int top, int right, int bottom);
  28. void setDraggableMargins(const QMargins &margins);
  29. void setTitleBar(TitleBar* titleBar);
  30. inline TitleBar* titleBar() const
  31. {
  32. return m_titleBar;
  33. }
  34. void setResizeEnabled(bool isEnabled);
  35. bool getResizeEnabled() const;
  36. public:
  37. qreal scaleFactor() const;
  38. bool nativeEventFilter(const QByteArray & eventType, void *msg, long *result);
  39. protected:
  40. bool eventFilter(QObject *obj, QEvent *ev) final;
  41. signals:
  42. void scaleFactorChanged(qreal factor);
  43. private:
  44. void initWindow();
  45. void updateWindowStyle();
  46. bool isMaximized() const;
  47. QRect availableGeometry() const;
  48. int hitTest(int x, int y) const;
  49. inline QMargins maximizedMargins() const
  50. {
  51. return m_maximizedMargins * m_scaleFactor;
  52. }
  53. inline QMargins draggableMargins() const
  54. {
  55. return m_draggableMargins * m_scaleFactor;
  56. }
  57. private:
  58. bool m_isResizeEnabled = true;
  59. QWidget* m_widget = nullptr;
  60. QWindow* m_window = nullptr;
  61. qreal m_scaleFactor = 1.0;
  62. HWND m_oldWindow = NULL;
  63. QMargins m_maximizedMargins;
  64. QMargins m_draggableMargins;
  65. TitleBar* m_titleBar = nullptr;
  66. int m_titleBarHeight = 35;
  67. };