看到網(wǎng)上有很多不規(guī)則窗體的實現(xiàn),效果很酷.于是使用QT也實現(xiàn)了一個,QT的不規(guī)則窗體實現(xiàn)非常簡單,只需要設置一個mask(遮掩)圖片,這個圖片的格式可以使用png或bmp格式,我使用了png格式,默認窗體是矩形的,使用png圖像,將需要隔離在窗體之外的區(qū)域的像素設置為白色或透明色,其他顏色的區(qū)域對應顯示出來的窗體.關鍵代碼就幾行.
#ifndef IRREGULARFORM_H
#define IRREGULARFORM_H
#include
#include "ui_irregularform.h"
#include
#include
#include
#include
class IrregularForm : public QWidget
{
Q_OBJECT
public:
IrregularForm(QWidget *parent = 0);
~IrregularForm();
protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
private:
Ui::IrregularFormClass ui;
QPoint mouseMovePos;
};
#endif // IRREGULARFORM_H
#include "irregularform.h"
IrregularForm::IrregularForm(QWidget *parent)
: QWidget(parent)
{
setWindowFlags(Qt::FramelessWindowHint);
QPixmap mask(":/IrregularForm/Resources/mask.png");//加載掩碼圖像
setMask(QBitmap(mask.mask())); //設置窗體的掩碼圖像,摳除圖像的白色區(qū)域實現(xiàn)不規(guī)則窗體
QPalette p;//設置調色板
p.setBrush(QPalette::Window, QBrush(mask));//將調色板的畫刷設置為掩碼位圖,在不規(guī)則窗體上顯示出掩碼位圖
setPalette(p);
mouseMovePos = QPoint(0, 0);
}
IrregularForm::~IrregularForm()
{
}
void IrregularForm::mouseMoveEvent(QMouseEvent *event)//鼠標按下并移動則移動不規(guī)則窗體
{
if(mouseMovePos != QPoint(0, 0))
{
move(geometry().x() + event->globalPos().x() - mouseMovePos.x(), geometry().y() + event->globalPos().y() - mouseMovePos.y());
mouseMovePos = event->globalPos();
}
}
void IrregularForm::mousePressEvent(QMouseEvent *event)
{
mouseMovePos = event->globalPos();
}
void IrregularForm::mouseReleaseEvent(QMouseEvent *event)
{
mouseMovePos = QPoint(0, 0);
}
效果圖