首页 > 其他分享 >Qt无边框窗口实现拖动和改变大小(nativeEvent利用系统消息处理)

Qt无边框窗口实现拖动和改变大小(nativeEvent利用系统消息处理)

时间:2023-02-08 19:25:43浏览次数:49  
标签:include Qt nativeEvent MainWindow 拖动 ui result else mainwindow

相关资料:

https://blog.csdn.net/qq_22642239/article/details/120565343

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12 
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
17 
18 SOURCES += \
19     main.cpp \
20     mainwindow.cpp
21 
22 HEADERS += \
23     mainwindow.h
24 
25 FORMS += \
26     mainwindow.ui
27 
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
View Code

main.cpp

 1 #include "mainwindow.h"
 2 
 3 #include <QApplication>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication a(argc, argv);
 8     MainWindow w;
 9     w.show();
10     return a.exec();
11 }
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 
 6 QT_BEGIN_NAMESPACE
 7 namespace Ui { class MainWindow; }
 8 QT_END_NAMESPACE
 9 
10 class MainWindow : public QMainWindow
11 {
12     Q_OBJECT
13 
14 public:
15     MainWindow(QWidget *parent = nullptr);
16     ~MainWindow();
17 
18 protected:
19     //第一种方案是走windows自己的消息机制
20     virtual bool nativeEvent(const QByteArray &eventType, void *message, long *result);
21 private:
22     Ui::MainWindow *ui;
23 };
24 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 #include <QMouseEvent>
 5 #include <qt_windows.h>
 6 const int g_nBorder = 4;
 7 
 8 MainWindow::MainWindow(QWidget *parent)
 9     : QMainWindow(parent)
10     , ui(new Ui::MainWindow)
11 {
12     ui->setupUi(this);
13     this->setWindowFlags(Qt::FramelessWindowHint);
14     setMouseTracking(true);
15 }
16 
17 MainWindow::~MainWindow()
18 {
19     delete ui;
20 }
21 
22 bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
23 {
24     MSG* pMsg = (MSG*)message;
25     switch (pMsg->message)
26     {
27     case WM_NCHITTEST:
28     {
29         QPoint pos = mapFromGlobal(QPoint(LOWORD(pMsg->lParam), HIWORD(pMsg->lParam)));
30         bool bHorLeft = pos.x() < g_nBorder;
31         bool bHorRight = pos.x() > width() - g_nBorder;
32         bool bVertTop = pos.y() < g_nBorder;
33         bool bVertBottom = pos.y() > height() - g_nBorder;
34         if (bHorLeft && bVertTop)
35             *result = HTTOPLEFT;
36         else if (bHorLeft && bVertBottom)
37             *result = HTBOTTOMLEFT;
38         else if (bHorRight && bVertTop)
39             *result = HTTOPRIGHT;
40         else if (bHorRight && bVertBottom)
41             *result = HTBOTTOMRIGHT;
42         else if (bHorLeft)
43             *result = HTLEFT;
44         else if (bHorRight)
45             *result = HTRIGHT;
46         else if (bVertTop)
47             *result = HTTOP;
48         else if (bVertBottom)
49             *result = HTBOTTOM;
50         else
51             return false;
52         return true;
53         }
54         break;
55         default:
56         break;
57     }
58     return QWidget::nativeEvent(eventType, message, result);
59 }
View Code

mainwindow.ui

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <ui version="4.0">
 3  <class>MainWindow</class>
 4  <widget class="QMainWindow" name="MainWindow">
 5   <property name="geometry">
 6    <rect>
 7     <x>0</x>
 8     <y>0</y>
 9     <width>800</width>
10     <height>600</height>
11    </rect>
12   </property>
13   <property name="windowTitle">
14    <string>MainWindow</string>
15   </property>
16   <widget class="QWidget" name="centralwidget"/>
17   <widget class="QMenuBar" name="menubar">
18    <property name="geometry">
19     <rect>
20      <x>0</x>
21      <y>0</y>
22      <width>800</width>
23      <height>22</height>
24     </rect>
25    </property>
26   </widget>
27   <widget class="QStatusBar" name="statusbar"/>
28  </widget>
29  <resources/>
30  <connections/>
31 </ui>
View Code

 

 

搜索

复制

<iframe></iframe>

标签:include,Qt,nativeEvent,MainWindow,拖动,ui,result,else,mainwindow
From: https://www.cnblogs.com/FKdelphi/p/17103006.html

相关文章

  • Home Assistant MQTT发现服务的实现
    HomeAssistantMQTT发现服务的实现MQTT收发基于mosquito以下内容摘自官方文档,可查看文档原文MQTTDiscoveryThediscoveryofMQTTdeviceswillenableonetouse......
  • Qt使用图片旋转绘制圆环,圆弧类UI界面(方法可适用于winform和WPF等其余UI绘制)
    先上效果图:  这个主要是一个试剂杯盘的循环图,相信大家伙一定都想到了使用GDI和三角函数来进行计算,但如果没有实际尝试,可能会踩一些坑,留此文帮助大家方便快捷的绘制出类......
  • Qt动画
    一、介绍类名功能介绍QAbstractAnimation动画基类提供基本的动画属性和接口,它有两个子类QVariantAnimation和QAnimationGroup。QAbstractAnimation是其......
  • QT使用画家设置背景图片
    protected:voidpaintEvent(QPaintEvent*);voidPet::paintEvent(QPaintEvent*){//重写自动执行QPixmappixmap=QPixmap("./images/background.jpg").......
  • 【Qt】-学Qt前的准备
    文章目录​​1Qt概述​​​​1.1什么是Qt​​​​2创建第一个Qt程序​​​​3命名规范以及快捷键​​​​3.1.pro文件介绍​​​​3.2.h文件介绍​​​​3.3命名规范......
  • Qt QLabel设置字体大小
    方法一(使用ui布局):选中label控件搜索“font” 修改pointsize后面的值方法二(使用代码):设置字体大小使用QFont的setPontSize1QLabel*lb=newQLabel(tr("examp......
  • Qt QLabel设置字体、大小、加粗等
    QFontfont(“MicrosoftYaHei”,10,75);//第一个属性是字体(微软雅黑),第二个是大小,第三个是加粗(权重是75)ui->label->setFont(font);常见权重QFont::Light-25高亮QF......
  • PyQt 主窗口、子窗口交互
    主窗口main.pyfromPySide6.QtCoreimport(QCoreApplication,QDate,QDateTime,QLocale,QMetaObject,QObject,QPoint,QRect,QSize,QTime,QUrl,Qt)fr......
  • 【嵌入式】微芯旺KungFu32A156MQT使用PWM实现呼吸灯
    由于例程给我的IO口是G8,但是我的板子上没有暴露G8引脚,所以需要查看数据手册,重映射一个IO口作为CCP的PWM输出 例:可见PB10使用AF2重映射到CCP0的通道3上,所以使用PB10观......
  • STM32MP157开发板Linux+Qt项目实战:智慧家庭
    stm32mp157开发板FS-MP1A是华清远见自主研发的一款高品质、高性价比的Linux+单片机二合一的嵌入式教学级开发板。开发板搭载ST的STM32MP157高性能微处理器,集成2个Cortex-A7......