QT中异形窗口的绘制(winEvent处理WM_NCHITTEST消息)
2015-10-01 16:02
477 查看
这里讨论的只是Windows平台上的实现。
在QT中绘制异形窗口,只要设定 windowFlag 为 CustomizeWindowHint,再结合setMask()就可以做出各种奇形怪状的窗口。相对来说比较麻烦的, 是进行窗口拖动和缩放的处理。
在 Windows SDK 和 MFC 中比较容易,只要处理 WM_NCHITTEST,返回相应的测试值就可以了。幸运的是,QT中也提供了直接处理各平台消息的方法,在 Windows下只需要重载winEvent方法。
下面给出了示例代码:
参考:http://www.cppblog.com/eXile/archive/2007/12/09/38084.html
在QT中绘制异形窗口,只要设定 windowFlag 为 CustomizeWindowHint,再结合setMask()就可以做出各种奇形怪状的窗口。相对来说比较麻烦的, 是进行窗口拖动和缩放的处理。
在 Windows SDK 和 MFC 中比较容易,只要处理 WM_NCHITTEST,返回相应的测试值就可以了。幸运的是,QT中也提供了直接处理各平台消息的方法,在 Windows下只需要重载winEvent方法。
下面给出了示例代码:
// include <windows.h> bool MyDialog::winEvent(MSG* msg, long* result) { const int captionHeight = 25; const int frameWidth = 6; if (msg->message != WM_NCHITTEST) return false; QPoint pos = mapFromGlobal(QCursor::pos()); int w = width(); int h = height(); if (QRect(frameWidth, captionHeight, w-frameWidth-frameWidth, h-captionHeight-frameWidth).contains(pos)) { *result = HTCLIENT; } else if (QRect(0, 0, w, captionHeight).contains(pos)) { *result = HTCAPTION; } else if (QRect(0, captionHeight, frameWidth, h-captionHeight-frameWidth).contains(pos)) { *result = HTLEFT; } else if (QRect(w-frameWidth, captionHeight, frameWidth, h-captionHeight-frameWidth).contains(pos)) { *result = HTRIGHT; } else if (QRect(frameWidth, h-frameWidth, w-frameWidth-frameWidth, frameWidth).contains(pos)) { *result = HTBOTTOM; } else if (QRect(0, h-frameWidth, frameWidth, frameWidth).contains(pos)) { *result = HTBOTTOMLEFT; } else if (QRect(w-frameWidth, h-frameWidth, frameWidth, frameWidth).contains(pos)) { *result = HTBOTTOMRIGHT; } return true; }
参考:http://www.cppblog.com/eXile/archive/2007/12/09/38084.html
相关文章推荐
- Qt学习之路【4】:qmake:syntax expecting ")")
- ZetCode PyQt4 tutorial First programs
- QT 停靠窗简单使用
- 【QT5学习历程】
- qt delegate drawDisplay()
- qt delegate委托 paint()方法
- qt delegate createEditor()方法
- qt delegate setEditorData()
- qt delegate代理 setModelData()方法
- qt 委托 paint() 显示数据
- qt QItemDelegate 委托 相关函数
- qt 委托 delegate
- qt model 中的角色 role Qt::CheckStateRole角色
- qt 代理模型 proxy model QSortFilterProxyModel
- QListWidget QTableWidget 项视图类
- pyqt5加载网路图片,不本地下载。
- qt多文档
- 软件实习笔记二(Qt简单对话框实例开发)
- 学习记录-Qt文件的读写和删除等操作
- 诡异的Qt eventFilter