#include <QEvent>
#include <QDebug>
#include <QDatetime>
#include <windows.h>
#include <windowsx.h>
#include "FrameWidgetBase.h"
FrameWidgetBase::FrameWidgetBase(QWidget *parent) : QWidget(parent)
{
_padding = 8;
_moveEnable = true;
_resizeEnable = true;
_mousePressed = false;
_mousePoint = QPoint(0, 0);
_mouseRect = QRect(0, 0, 0, 0);
for (int i = 0; i < 8; ++i) {
_pressedArea << false;
_pressedRect << QRect(0, 0, 0, 0);
}
_isMin = false;
_flags = this->windowFlags();
_titleBar = 0;
//设置背景透明 官方在5.3以后才彻底修复 WA_TranslucentBackground+FramelessWindowHint 并存不绘制的BUG
this->setAttribute(Qt::WA_TranslucentBackground);
//设置无边框属性
this->setWindowFlags(_flags | Qt::FramelessWindowHint);
//安装事件过滤器识别拖动
this->installEventFilter(this);
//设置属性产生win窗体效果,移动到边缘半屏或者最大化等
//设置以后会产生标题栏需要在下面拦截消息重新去掉
HWND hwnd = (HWND)this->winId();
DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
::SetWindowLong(hwnd, GWL_STYLE
, style | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CAPTION);
}
void FrameWidgetBase::showEvent(QShowEvent *event)
{
//解决有时候窗体重新显示的时候假死不刷新的BUG
setAttribute(Qt::WA_Mapped);
QWidget::showEvent(event);
}
void FrameWidgetBase::doWindowStateChange(QEvent *event)
{
//非最大化才能移动和拖动大小
if (windowState() == Qt::WindowNoState)
{
_moveEnable = true;
_resizeEnable = true;
} else
{
_moveEnable = false;
_resizeEnable = false;
}
//发出最大化最小化等改变事件,以便界面上更改对应的信息比如右上角图标和文字
emit windowStateChange(!_moveEnable);
}
bool FrameWidgetBase::eventFilter(QObject *watched, QEvent *event)
{
if (watched == this)
{
if (event->type() == QEvent::WindowStateChange)
{
doWindowStateChange(event);
}
}
return QWidget::eventFilter(watched, event);
}
bool FrameWidgetBase::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
if (eventType == "windows_generic_MSG")
{
MSG *msg = static_cast<MSG *>(message);
//不同的消息类型和参数进行不同的处理
if (msg->message == WM_NCCALCSIZE)
{
*result = 0;
return true;
}
else if (msg->message == WM_NCHITTEST)
{
//计算鼠标对应的屏幕坐标
//这里最开始用的 LOWORD HIWORD 在多屏幕的时候会有问题
//官方说明在这里 https://docs.microsoft.com/zh-cn/windows/win32/inputdev/wm-nchittest
long x = GET_X_LPARAM(msg->lParam);
long y = GET_Y_LPARAM(msg->lParam);
QPoint pos = mapFromGlobal(QPoint(x, y));
//判断当前鼠标位置在哪个区域
bool left = pos.x() < _padding;
bool right = pos.x() > width() - _padding;
bool top = pos.y() < _padding;
bool bottom = pos.y() > height() - _padding;
//鼠标移动到四个角,这个消息是当鼠标移动或者有鼠标键按下时候发出的
*result = 0;
if (_resizeEnable) {
if (left && top) {
*result = HTTOPLEFT;
} else if (left && bottom) {
*result = HTBOTTOMLEFT;
} else if (right && top) {
*result = HTTOPRIGHT;
} else if (right && bottom) {
*result = HTBOTTOMRIGHT;
} else if (left) {
*result = HTLEFT;
} else if (right) {
*result = HTRIGHT;
} else if (top) {
*result = HTTOP;
} else if (bottom) {
*result = HTBOTTOM;
}
}
//先处理掉拉伸
if (0 != *result) {
return true;
}
//识别标题栏拖动产生半屏全屏效果
if (_titleBar != 0 && _titleBar->rect().contains(pos))
{
QWidget *child = _titleBar->childAt(pos);
if (!child)
{
*result = HTCAPTION;
return true;
}
}
}
else if (msg->wParam == PBT_APMSUSPEND
&& msg->message == WM_POWERBROADCAST)
{
//系统休眠的时候自动最小化可以规避程序可能出现的问题
this->showMinimized();
} else if (msg->wParam == PBT_APMRESUMEAUTOMATIC)
{
//休眠唤醒后自动打开
this->showNormal();
}
}
return false;
}
void FrameWidgetBase::setPadding(int padding)
{
_padding = padding;
}
void FrameWidgetBase::setMoveEnable(bool moveEnable)
{
_moveEnable = moveEnable;
}
void FrameWidgetBase::setResizeEnable(bool resizeEnable)
{
_resizeEnable = resizeEnable;
}
void FrameWidgetBase::setTitleBar(QWidget *titleBar)
{
_titleBar = titleBar;
_titleBar->installEventFilter(this);
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于QT的自定义对话框界面
共10个文件
cpp:4个
h:3个
ui:2个
需积分: 3 0 下载量 147 浏览量
2023-06-21
11:20:33
上传
评论
收藏 8KB RAR 举报
温馨提示
基于通用三方开源的Qt界面库的基础之上,高效、省时的帮你完成个性化界面。用户基于上述开发系统,实现个性化的界面;个性化的实现了界面的几大元素(最大化、最小化以及关闭按钮);对界面的自定义拖动的实现。 本源码基于网络源码升级而成,后续大家有好的建议可以留言或者升级分享!
资源推荐
资源详情
资源评论
收起资源包目录
FrameWidget.rar (10个子文件)
FrameWidget
FramelessWidgetExample.ui 1KB
FramelessWidgetExample.h 624B
main.cpp 410B
FramelessWidgetExample.cpp 3KB
FrameWidget.cpp 2KB
FrameWidgetBase.cpp 5KB
FrameWidget.ui 6KB
FrameWidget.h 540B
FrameWidget.pro 425B
FrameWidgetBase.h 2KB
共 10 条
- 1
资源评论
龙门飞客
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功