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

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

  • 相关阅读:
    LCD1602
    Word控件Spire.Doc 【页面设置】教程(10) ;通过 Spire.Doc 在 Word 文档的不同部分添加页码
    vue3 项目部署,Nginx配置https,重定向,详细流程
    IP子网的划分
    哪些因素会影响离子交换树脂交换能力?树脂交换能力变差的原因?
    前后端跨域解决方案——jsonp,core,代理服务器
    使用国内源加速pip安装包
    用HTML+CSS+JS做一个漂亮简单的公司网站(JavaScript期末大作业)
    Git服务端
    Datastage部署与使用
  • 原文地址:https://blog.csdn.net/weixin_38241876/article/details/134562305