无边框窗口、控件的事件处理之nativeEvent(Qt 5.5.0)
2016-03-23 17:29
465 查看
窗口控件的无边框取消了窗口事件处理要么重写鼠标事件,要么就是本篇所涉及的方法。个人喜欢nativeEvent处理,各有优缺点。上代码:.h
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#000080;">#ifndef</span><span style=" color:#c0c0c0;"> </span>MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <windows.h>
#include <windowsx.h>
#include <QLabel>
class MainWindow : public QLabel
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public:
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
};
#endif // MAINWINDOW_H
<strong><span style="color:#ff0000;">.cpp</span></strong>
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;">"mainwindow.h"</span>
#include <QDebug>
#include <QDialog>
MainWindow::MainWindow(QWidget *parent)
: QLabel(parent)
{
this->setWindowFlags(Qt::FramelessWindowHint);
this->setMinimumSize(45,45);
this->setStyleSheet("background:#ff0000");
}
MainWindow::~MainWindow()
{
}
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG* msg = (MSG*)message;
switch(msg->message)
{
case WM_NCHITTEST:
int xPos = GET_X_LPARAM(msg->lParam) - this->frameGeometry().x();
int yPos = GET_Y_LPARAM(msg->lParam) - this->frameGeometry().y();
if(this->childAt(xPos,yPos) == 0)
{
*result = HTCAPTION;
}else{
return false;
}
if(xPos > 0 && xPos < 8)
*result = HTLEFT;
if(xPos > (this->width() - 8) && xPos < (this->width() - 0))
*result = HTRIGHT;
if(yPos > 0 && yPos < 8)
*result = HTTOP;
if(yPos > (this->height() - 8) && yPos < (this->height() - 0))
*result = HTBOTTOM;
if(xPos > 18 && xPos < 22 && yPos > 18 && yPos < 22)
*result = HTTOPLEFT;
if(xPos >b233; (this->width() - 22) && xPos < (this->width() - 18) && yPos > 18 && yPos < 22)
*result = HTTOPRIGHT;
if(xPos > 18 && xPos < 22 && yPos > (this->height() - 22) && yPos < (this->height() - 18))
*result = HTBOTTOMLEFT;
if(xPos > (this->width() - 22) && xPos < (this->width() - 18) && yPos > (this->height() - 22) && yPos < (this->height() - 18))
*result = HTBOTTOMRIGHT;
return true;
}
return false;
}
<pre style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#000080;">#include</span><span style=" color:#c0c0c0;"> </span><span style=" color:#008000;"><QApplication></span>
#include <QPushButton>
#include "mainwindow.h"
#include "form.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(100,100);
w.show();
return a.exec();
}
相关文章推荐
- Qt上传文件
- Qt OpenGL:学习现代3D图形编程之一,画一个三角形
- QT之中多个信号连接同一个槽【细说版】
- Qt构建OpenCV3.1CMake报错Failed to download opencv_ffmpeg.dll
- 怎样用QT在windows环境通过编程卸载installShield打包的程序
- QTimer调用Stop后再次start,以前connect的槽将失效
- Qt教程 第二章:调用退出
- Qt教程 第一章:Hello, World!
- Qt4.8.6移植到海思HI3536
- Qt 不能响应Tslib输入的问题
- 在Beaglebone Black板上使用Yocto移植Qt5
- QT下的几种透明效果
- Qt中的.qrc文件
- Qt简介
- Qt类图
- Qt布局
- Qt新建项目
- QT 模拟点击网页
- QT更改程序图标
- Qt中提高sqlite的读写速度(使用事务一次性写入100万条数据)