您的位置:首页 > 编程语言 > Qt开发

无边框窗口、控件的事件处理之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();
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: