• Qt的一个无边界窗口公共类


    头文件:

    1. #pragma once
    2. #include
    3. class CFrameLessWidgetBase :
    4. public QWidget
    5. {
    6. public:
    7. CFrameLessWidgetBase(QWidget* p = nullptr);
    8. ~CFrameLessWidgetBase() {}
    9. protected:
    10. bool nativeEvent(const QByteArray& eventType, void* message, long* result) override;
    11. private:
    12. int m_nBorderWidth = 5;
    13. };

    实现:

    1. #include "CFrameLessWidgetBase.h"
    2. #include
    3. #include
    4. #include
    5. #pragma comment(lib, "user32.lib")
    6. #pragma comment(lib,"dwmapi.lib")
    7. CFrameLessWidgetBase::CFrameLessWidgetBase(QWidget* p)
    8. :QWidget(p)
    9. {
    10. this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
    11. setAttribute(Qt::WA_Hover); //很重要
    12. }
    13. bool CFrameLessWidgetBase::nativeEvent(const QByteArray& eventType, void* message, long* result)
    14. {
    15. MSG* param = static_cast(message);
    16. switch (param->message)
    17. {
    18. case WM_NCHITTEST:
    19. {
    20. int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();
    21. int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();
    22. /*if (childAt(nX, nY) != nullptr)
    23. return QWidget::nativeEvent(eventType, message, result);*/
    24. if (nX > m_nBorderWidth && nX < this->width() - m_nBorderWidth &&
    25. nY > m_nBorderWidth && nY < this->height() - m_nBorderWidth)
    26. {
    27. if (childAt(nX, nY) != nullptr)
    28. return QWidget::nativeEvent(eventType, message, result);
    29. }
    30. if ((nX > 0) && (nX < m_nBorderWidth))
    31. *result = HTLEFT;
    32. if ((nX > this->width() - m_nBorderWidth) && (nX < this->width()))
    33. *result = HTRIGHT;
    34. if ((nY > 0) && (nY < m_nBorderWidth))
    35. *result = HTTOP;
    36. if ((nY > this->height() - m_nBorderWidth) && (nY < this->height()))
    37. *result = HTBOTTOM;
    38. if ((nX > 0) && (nX < m_nBorderWidth) && (nY > 0)
    39. && (nY < m_nBorderWidth))
    40. *result = HTTOPLEFT;
    41. if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
    42. && (nY > 0) && (nY < m_nBorderWidth))
    43. *result = HTTOPRIGHT;
    44. if ((nX > 0) && (nX < m_nBorderWidth)
    45. && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
    46. *result = HTBOTTOMLEFT;
    47. if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
    48. && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
    49. *result = HTBOTTOMRIGHT;
    50. return true;
    51. }
    52. }
    53. return false;
    54. }

    要实现无边界窗口的设计,只需要集成自这个类就可以了。

  • 相关阅读:
    mysql笔记
    Win10纯净版和官方原版哪个好?
    前端无法获取Django自定义响应头 Response Header
    REST风格【SpringBoot】
    【数据结构】第二章课后练习题——线性结构
    一个重要的问题:怎么寻找自己的终身事业呢?
    卷积神经网络模型之——LeNet网络结构与代码实现
    链接到语音识别控制面板时发生错误(0x80004005L)
    【C++】C++ 入门
    6.云原生-KubeSphere3.3.0安装MYSQL
  • 原文地址:https://blog.csdn.net/weixin_38241876/article/details/134562305