Qt之自定义界面(窗体缩放)
2024-08-27 06:03:45
简述
通过前两节内容,我们实现了自定义窗体的移动,以及自定义标题栏-用来显示窗体的图标、标题,以及控制窗体最小化、最大化、关闭。
在这之后,我们还缺少窗体的缩放-当鼠标移动到窗体的边框-左、上、右、下、左上角、左下角、右上角、右下角时候,鼠标变为相应的样式,并且窗体可以随着鼠标拖动而进行放大、缩小。
效果
窗体缩放
实现
首先,设置无边框,用于实现自定义标题栏。
// 设置无边框
setWindowFlags(Qt::FramelessWindowHint);
// 背景透明
setAttribute(Qt::WA_TranslucentBackground, true);
包含头文件与所需要的库。
#ifdef Q_OS_WIN
#include <qt_windows.h>
#include <Windowsx.h>
#endif
使用nativeEvent进行窗体缩放。
注意: m_nBorder表示鼠标位于边框缩放范围的宽度,可以设置为5。
bool Widget::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(eventType)
MSG *param = static_cast<MSG *>(message);
switch (param->message)
{
case WM_NCHITTEST:
{
int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();
int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();
// 如果鼠标位于子控件上,则不进行处理
if (childAt(nX, nY) != NULL)
return QWidget::nativeEvent(eventType, message, result);
*result = HTCAPTION;
// 鼠标区域位于窗体边框,进行缩放
if ((nX > 0) && (nX < m_nBorder))
*result = HTLEFT;
if ((nX > this->width() - m_nBorder) && (nX < this->width()))
*result = HTRIGHT;
if ((nY > 0) && (nY < m_nBorder))
*result = HTTOP;
if ((nY > this->height() - m_nBorder) && (nY < this->height()))
*result = HTBOTTOM;
if ((nX > 0) && (nX < m_nBorder) && (nY > 0)
&& (nY < m_nBorder))
*result = HTTOPLEFT;
if ((nX > this->width() - m_nBorder) && (nX < this->width())
&& (nY > 0) && (nY < m_nBorder))
*result = HTTOPRIGHT;
if ((nX > 0) && (nX < m_nBorder)
&& (nY > this->height() - m_nBorder) && (nY < this->height()))
*result = HTBOTTOMLEFT;
if ((nX > this->width() - m_nBorder) && (nX < this->width())
&& (nY > this->height() - m_nBorder) && (nY < this->height()))
*result = HTBOTTOMRIGHT;
return true;
}
}
return QWidget::nativeEvent(eventType, message, result);
}
接口说明
Qt5与Qt4其中的一个区别就是用nativeEvent代替了winEvent。
nativeEvent主要用于进程间通信-消息传递。使用这种方式后,窗体就可以随意缩放了,而且可以去掉标题栏中控制界面移动的代码 - 在mousePressEvent中使用SendMessage来进行移动。
当然,这种实现只能在Windows下使用,因为用的是Win API,如果需要跨平台的话,需要自己处理各种事件,而且得考虑的很全面。
最新文章
- jquery之toggleClass应用
- Collection
- metasploit用法
- CoreData的使用
- ACM/ICPC 之 DFS范例(ZOJ2412-ZOJ1008)
- IrregularGridCollectionView处理不定宽度的标签cell
- SVN Can&#39;t open file &#39;xxx&#39;:Premission denied
- java 四种逻辑运算符
- (转载)不能启动虚拟机 Unable to open kernel device ";\\.\Global\vmx86
- 从头学Qt Quick系列
- 道路修建 2(自创题+题解)(From NOI2011)
- C++ C# python 中常用数学计算函数对比
- Hdoj 1253.胜利大逃亡 题解
- mysql主从配置和galera集群
- TypeScript 模块系统
- Spring boot MultipartResolver
- centos 关闭selinux
- 用django实现一个资产管理的系统
- Spring boot 集成Dubbo简单版,准备工作,
- Ubuntu 14.04服务器安装及软件配置
热门文章
- 仿知乎/途家导航栏渐变文字动画效果-b
- Start my cnBlogs
- [转载+原创]Emgu CV on C# (一) —— Emgu CV on Visual C# 2010
- 【UOJ】【34】多项式乘法
- 并查集 基础 AC 2014-01-14 13:37 202人阅读 评论(0) 收藏
- new Date()的数据类型的问题
- 让32位Eclipse和64位Eclipse同时在64的Windows7上运行
- jsp bean标签
- this指针指向的彻底理解
- java基础知识回顾之javaIO类--File类