QT实现的不规则窗口,是根据图片的形状显示
1.去标题栏
2.设置窗口背景为透明色
3.最后给窗口设置背景色
注:背景图为镂空的 格式为.png
图片资源下载:http://pan.baidu.com/s/1i5JkIot
.h
1 #ifndef QANORMALYDLG_H 2 #define QANORMALYDLG_H 3 4 #include <QWidget> 5 #include "ui_qanormalydlg.h" 6 7 class QAnormalyDlg : public QWidget 8 { 9 Q_OBJECT 10 11 public: 12 QAnormalyDlg(QWidget *parent = 0); 13 ~QAnormalyDlg(); 14 15 void paintEvent(QPaintEvent *e); 16 17 void mousePressEvent(QMouseEvent *e); 18 19 void mouseMoveEvent(QMouseEvent *e); 20 private: 21 Ui::QAnormalyDlg ui; 22 23 QPoint move_point; //移动的距离 24 }; 25 26 #endif // QANORMALYDLG_H
.cpp
1 #include "qanormalydlg.h" 2 #include <QPainter> 3 #include <QMouseEvent> 4 5 QAnormalyDlg::QAnormalyDlg(QWidget *parent) 6 : QWidget(parent) 7 { 8 ui.setupUi(this); 9 10 //去表框 同时保留窗口原有的属性 11 setWindowFlags(Qt::FramelessWindowHint | windowFlags() ); 12 13 //把窗口背景设为透明 14 setAttribute(Qt::WA_TranslucentBackground); 15 16 resize(600,652); 17 } 18 19 QAnormalyDlg::~QAnormalyDlg() 20 { 21 22 } 23 24 void QAnormalyDlg::paintEvent(QPaintEvent *e) 25 { 26 QPainter p(this); 27 p.drawPixmap(0, 0, QPixmap("122.png")); 28 } 29 30 void QAnormalyDlg::mousePressEvent(QMouseEvent *e) 31 { 32 if (e->button() == Qt::RightButton) 33 { 34 //右键关闭窗口 35 close(); 36 } 37 38 else if (e->button() == Qt::LeftButton) 39 { 40 //求坐标差,当前鼠标坐标 - 窗口左上角坐标 41 //frameGeometry返回窗口的矩形坐标, topLeft返回窗口左上角点的坐标 42 //move_point = e->globalPos() - this->frameGeometry().topLeft(); 43 move_point = e->globalPos() - this->pos(); 44 } 45 } 46 47 void QAnormalyDlg::mouseMoveEvent(QMouseEvent *e) 48 { 49 if (e->buttons() & Qt::LeftButton) 50 { 51 move(e->globalPos()-move_point); 52 } 53 }
效果: