改进大量代码
This commit is contained in:
@@ -16,8 +16,10 @@ CONFIG += warn_off
|
||||
SOURCES += main.cpp
|
||||
SOURCES += frmframelesswidget.cpp
|
||||
SOURCES += framelesswidget.cpp
|
||||
SOURCES += framelesswidget2.cpp
|
||||
|
||||
HEADERS += frmframelesswidget.h
|
||||
HEADERS += framelesswidget.h
|
||||
HEADERS += framelesswidget2.h
|
||||
|
||||
FORMS += frmframelesswidget.ui
|
||||
|
||||
85
framelesswidget/framelesswidget2.cpp
Normal file
85
framelesswidget/framelesswidget2.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "framelesswidget2.h"
|
||||
#include "qevent.h"
|
||||
#include "qdebug.h"
|
||||
#ifdef Q_OS_WIN
|
||||
#include "windows.h"
|
||||
#endif
|
||||
|
||||
FramelessWidget2::FramelessWidget2(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
padding = 5;
|
||||
this->installEventFilter(this);
|
||||
}
|
||||
|
||||
bool FramelessWidget2::eventFilter(QObject *watched, QEvent *event)
|
||||
{
|
||||
if (watched == this) {
|
||||
static QPoint mousePoint;
|
||||
static bool mousePressed = false;
|
||||
|
||||
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
|
||||
if (mouseEvent->type() == QEvent::MouseButtonPress) {
|
||||
if (mouseEvent->button() == Qt::LeftButton) {
|
||||
mousePressed = true;
|
||||
mousePoint = mouseEvent->globalPos() - this->pos();
|
||||
}
|
||||
} else if (mouseEvent->type() == QEvent::MouseButtonRelease) {
|
||||
mousePressed = false;
|
||||
} else if (mouseEvent->type() == QEvent::MouseMove) {
|
||||
if (mousePressed) {
|
||||
this->move(mouseEvent->globalPos() - mousePoint);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return QWidget::eventFilter(watched, event);
|
||||
}
|
||||
|
||||
bool FramelessWidget2::nativeEvent(const QByteArray &eventType, void *message, long *result)
|
||||
{
|
||||
#ifdef Q_OS_WIN
|
||||
MSG *msg = (MSG *)message;
|
||||
switch (msg->message) {
|
||||
case WM_NCHITTEST: {
|
||||
QPoint pos = mapFromGlobal(QPoint(LOWORD(msg->lParam), HIWORD(msg->lParam)));
|
||||
bool left = pos.x() < padding;
|
||||
bool right = pos.x() > width() - padding;
|
||||
bool top = pos.y() < padding;
|
||||
bool bottom = pos.y() > height() - padding;
|
||||
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;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
bool FramelessWidget2::winEvent(MSG *message, long *result)
|
||||
{
|
||||
return nativeEvent("windows_generic_MSG", message, result);
|
||||
}
|
||||
#endif
|
||||
28
framelesswidget/framelesswidget2.h
Normal file
28
framelesswidget/framelesswidget2.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef FRAMELESSWIDGET2_H
|
||||
#define FRAMELESSWIDGET2_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#ifdef quc
|
||||
class Q_DECL_EXPORT FramelessWidget2 : public QWidget
|
||||
#else
|
||||
class FramelessWidget2 : public QWidget
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit FramelessWidget2(QWidget *parent = 0);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *watched, QEvent *event);
|
||||
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
|
||||
#ifdef Q_OS_WIN
|
||||
bool winEvent(MSG *message, long *result);
|
||||
#endif
|
||||
|
||||
private:
|
||||
int padding;
|
||||
};
|
||||
|
||||
#endif // FRAMELESSWIDGET2_H
|
||||
@@ -5,11 +5,14 @@
|
||||
#include "qpushbutton.h"
|
||||
#include "qcheckbox.h"
|
||||
#include "framelesswidget.h"
|
||||
#include "framelesswidget2.h"
|
||||
|
||||
frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
w = 0;
|
||||
widget1 = 0;
|
||||
widget2 = 0;
|
||||
frameless = 0;
|
||||
}
|
||||
|
||||
frmFramelessWidget::~frmFramelessWidget()
|
||||
@@ -22,52 +25,70 @@ void frmFramelessWidget::closeEvent(QCloseEvent *)
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void frmFramelessWidget::initWidget(QWidget *w)
|
||||
{
|
||||
//设置无边框属性
|
||||
w->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
|
||||
//w->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
||||
w->setWindowTitle("自由拉伸无边框窗体");
|
||||
w->setMinimumSize(200, 120);
|
||||
w->resize(480, 320);
|
||||
|
||||
//设置下背景颜色区别看
|
||||
QPalette palette = w->palette();
|
||||
palette.setBrush(QPalette::Background, QColor(162, 121, 197));
|
||||
w->setPalette(palette);
|
||||
|
||||
QPushButton *btn = new QPushButton(w);
|
||||
btn->setText("关闭");
|
||||
btn->setGeometry(10, 10, 130, 25);
|
||||
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
|
||||
|
||||
QCheckBox *cboxMove = new QCheckBox(w);
|
||||
cboxMove->setText("可移动");
|
||||
cboxMove->setChecked(true);
|
||||
cboxMove->setGeometry(10, 40, 70, 25);
|
||||
connect(cboxMove, SIGNAL(stateChanged(int)), this, SLOT(stateChanged1(int)));
|
||||
|
||||
QCheckBox *cboxResize = new QCheckBox(w);
|
||||
cboxResize->setText("可拉伸");
|
||||
cboxResize->setChecked(true);
|
||||
cboxResize->setGeometry(80, 40, 70, 25);
|
||||
connect(cboxResize, SIGNAL(stateChanged(int)), this, SLOT(stateChanged2(int)));
|
||||
}
|
||||
|
||||
void frmFramelessWidget::on_pushButton_clicked()
|
||||
{
|
||||
if (w == 0) {
|
||||
w = new QWidget;
|
||||
//设置无边框属性
|
||||
w->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
|
||||
//w->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
|
||||
w->setWindowTitle("自由拉伸无边框窗体");
|
||||
w->setMinimumSize(200, 120);
|
||||
w->resize(480, 320);
|
||||
|
||||
//设置下背景颜色区别看
|
||||
QPalette palette = w->palette();
|
||||
palette.setBrush(QPalette::Background, QColor(162, 121, 197));
|
||||
w->setPalette(palette);
|
||||
|
||||
QPushButton *btn = new QPushButton(w);
|
||||
btn->setText("关闭");
|
||||
btn->setGeometry(10, 10, 130, 25);
|
||||
connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));
|
||||
|
||||
QCheckBox *cboxMove = new QCheckBox(w);
|
||||
cboxMove->setText("可移动");
|
||||
cboxMove->setChecked(true);
|
||||
cboxMove->setGeometry(10, 40, 70, 25);
|
||||
connect(cboxMove, SIGNAL(stateChanged(int)), this, SLOT(stateChanged1(int)));
|
||||
|
||||
QCheckBox *cboxResize = new QCheckBox(w);
|
||||
cboxResize->setText("可拉伸");
|
||||
cboxResize->setChecked(true);
|
||||
cboxResize->setGeometry(80, 40, 70, 25);
|
||||
connect(cboxResize, SIGNAL(stateChanged(int)), this, SLOT(stateChanged2(int)));
|
||||
|
||||
frameless = new FramelessWidget(w);
|
||||
frameless->setWidget(w);
|
||||
if (widget1 == 0) {
|
||||
widget1 = new QWidget;
|
||||
this->initWidget(widget1);
|
||||
frameless = new FramelessWidget(widget1);
|
||||
frameless->setWidget(widget1);
|
||||
}
|
||||
|
||||
w->show();
|
||||
widget1->show();
|
||||
}
|
||||
|
||||
void frmFramelessWidget::stateChanged1(int arg1)
|
||||
{
|
||||
frameless->setMoveEnable(arg1 != 0);
|
||||
if (frameless != 0) {
|
||||
frameless->setMoveEnable(arg1 != 0);
|
||||
}
|
||||
}
|
||||
|
||||
void frmFramelessWidget::stateChanged2(int arg1)
|
||||
{
|
||||
frameless->setResizeEnable(arg1 != 0);
|
||||
if (frameless != 0) {
|
||||
frameless->setResizeEnable(arg1 != 0);
|
||||
}
|
||||
}
|
||||
|
||||
void frmFramelessWidget::on_pushButton_2_clicked()
|
||||
{
|
||||
if (widget2 == 0) {
|
||||
widget2 = new FramelessWidget2;
|
||||
this->initWidget(widget2);
|
||||
}
|
||||
|
||||
widget2->show();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <QWidget>
|
||||
class FramelessWidget;
|
||||
class FramelessWidget2;
|
||||
|
||||
namespace Ui {
|
||||
class frmFramelessWidget;
|
||||
@@ -21,13 +22,16 @@ protected:
|
||||
|
||||
private:
|
||||
Ui::frmFramelessWidget *ui;
|
||||
QWidget *w;
|
||||
QWidget *widget1;
|
||||
FramelessWidget2 *widget2;
|
||||
FramelessWidget *frameless;
|
||||
|
||||
private slots:
|
||||
void initWidget(QWidget *w);
|
||||
void on_pushButton_clicked();
|
||||
void stateChanged1(int arg1);
|
||||
void stateChanged2(int arg1);
|
||||
void on_pushButton_2_clicked();
|
||||
};
|
||||
|
||||
#endif // FRMFRAMELESSWIDGET_H
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>600</width>
|
||||
<height>480</height>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@@ -26,6 +26,19 @@
|
||||
<string>弹出</string>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QPushButton" name="pushButton_2">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>110</x>
|
||||
<y>10</y>
|
||||
<width>92</width>
|
||||
<height>28</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>无边框2</string>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
|
||||
Reference in New Issue
Block a user