标签:qt
<pre name="code" class="cpp">#include <QBoxLayout> #include <QPalette> #include <QGraphicsDropShadowEffect> #include <QDebug> #include <QSizePolicy> #include <QPainter> #include "bfwidget.h" //------------------------------------------------------------ //----------- Created with 010 Editor ----------- //------ www.sweetscape.com/010editor/ ------ // // File : G:\project\Sample\ShadeWindow\box-shadow.png // Address : 0 (0x0) // Size : 588 (0x24C) //------------------------------------------------------------ unsigned char _hexData[588] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x1D, 0x00, 0x00, 0x00, 0x1D, 0x08, 0x06, 0x00, 0x00, 0x00, 0x56, 0x93, 0x67, 0x0F, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x06, 0x62, 0x4B, 0x47, 0x44, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0xA0, 0xBD, 0xA7, 0x93, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0D, 0xD7, 0x00, 0x00, 0x0D, 0xD7, 0x01, 0x42, 0x28, 0x9B, 0x78, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4D, 0x45, 0x07, 0xDA, 0x03, 0x0A, 0x01, 0x3B, 0x39, 0x35, 0x19, 0x02, 0x5A, 0x00, 0x00, 0x00, 0x22, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6F, 0x6D, 0x6D, 0x65, 0x6E, 0x74, 0x00, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4D, 0x50, 0x20, 0x6F, 0x6E, 0x20, 0x61, 0x20, 0x4D, 0x61, 0x63, 0x87, 0xA8, 0x77, 0x43, 0x00, 0x00, 0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 0x8D, 0x72, 0x0B, 0x7C, 0xE8, 0x7B, 0x6B, 0xAC, 0xE9, 0x92, 0x9B, 0xC3, 0x56, 0x7D, 0x87, 0x78, 0x21, 0xB5, 0x66, 0x94, 0xA8, 0x52, 0x75, 0xC5, 0x3B, 0x52, 0x37, 0xAF, 0xC9, 0x3B, 0xEE, 0x6F, 0xDF, 0x06, 0x83, 0xFB, 0xC3, 0x7B, 0xEF, 0x61, 0xCF, 0x8E, 0x72, 0xC3, 0x77, 0x63, 0xFC, 0xCB, 0x7C, 0x01, 0x3F, 0xD0, 0xC3, 0x34, 0xFA, 0xD4, 0x35, 0x7A, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82 }; const QPixmap _DrawNinePatch(QSize szDst, const QPixmap &srcPix) { // 绘制背景图到, 以九宫格形式 QPixmap dstPix(szDst); dstPix.fill(QColor(255, 255, 255, 0)); QPainter painter; painter.begin(&dstPix); int nW = szDst.width(); int nH = szDst.height(); int nWBg = srcPix.width(); int nHBg = srcPix.height(); QPoint m_ptBgLT(10, 10); QPoint m_ptBgRB(19, 19); QPoint ptDstLT(m_ptBgLT.x(), m_ptBgLT.y()); QPoint ptDstRB(nW - (nWBg - m_ptBgRB.x()), nH - (nHBg - m_ptBgRB.y())); //LT painter.drawPixmap(QRect(0, 0, ptDstLT.x(), ptDstLT.y()), srcPix, QRect(0, 0, m_ptBgLT.x(), m_ptBgLT.y())); //MT painter.drawPixmap(QRect(ptDstLT.x(), 0, ptDstRB.x() - ptDstLT.x(), ptDstLT.y()), srcPix, QRect(m_ptBgLT.x(), 0, m_ptBgRB.x() - m_ptBgLT.x(), m_ptBgLT.y())); //RT painter.drawPixmap(QRect(ptDstRB.x(), 0, nW - ptDstRB.x(), ptDstLT.y()), srcPix, QRect(m_ptBgRB.x(), 0, nWBg - m_ptBgRB.x(), m_ptBgLT.y())); //LM painter.drawPixmap(QRect(0, ptDstLT.y(), ptDstLT.x(), ptDstRB.y() - ptDstLT.y()), srcPix, QRect(0, m_ptBgLT.y(), m_ptBgLT.x(), m_ptBgRB.y() - m_ptBgLT.y())); //MM painter.drawPixmap(QRect(ptDstLT.x(), ptDstLT.y(), ptDstRB.x() - ptDstLT.x(), ptDstRB.y() - ptDstLT.y()), srcPix, QRect(m_ptBgLT.x(), m_ptBgLT.y(), m_ptBgRB.x() - m_ptBgLT.x(), m_ptBgRB.y() - m_ptBgLT.y())); //RM painter.drawPixmap(QRect(ptDstRB.x(), ptDstLT.y(), nW - ptDstRB.x(), ptDstRB.y() - ptDstLT.y()), srcPix, QRect(m_ptBgRB.x(), m_ptBgLT.y(), nWBg - m_ptBgRB.x(), m_ptBgRB.y() - m_ptBgLT.y())); //LB painter.drawPixmap(QRect(0, ptDstRB.y(), ptDstLT.x(), nH - ptDstRB.y()), srcPix, QRect(0, m_ptBgRB.y(), m_ptBgLT.x(), nHBg - m_ptBgRB.y())); //MB painter.drawPixmap(QRect(ptDstLT.x(), ptDstRB.y(), ptDstRB.x() - ptDstLT.x(), nH - ptDstRB.y()), srcPix, QRect(m_ptBgLT.x(), m_ptBgRB.y(), m_ptBgRB.x() - m_ptBgLT.x(), nHBg - m_ptBgRB.y())); //RB painter.drawPixmap(QRect(ptDstRB.x(), ptDstRB.y(), nW - ptDstRB.x(), nH - ptDstRB.y()), srcPix, QRect(m_ptBgRB.x(), m_ptBgRB.y(), nWBg - m_ptBgRB.x(), nHBg - m_ptBgRB.y())); painter.end(); return dstPix; } BFWidget::BFWidget(QWidget *parent) : QDialog(parent) { //加载阴影图片 _pixShadow.loadFromData(_hexData, 588); //_pixShadow.load("box-shadow.png"); this->setWindowFlags(Qt::FramelessWindowHint); this->setAttribute(Qt::WA_TranslucentBackground); this->resize(600 + 12, 400 + 12); this->setContentsMargins(6, 6, 6, 6); } void BFWidget::mousePressEvent(QMouseEvent * event) { //只能是鼠标左键移动和改变大小 if (event->button() == Qt::LeftButton) { mouse_press = true; } //窗口移动距离 move_point = event->globalPos() - pos(); } void BFWidget::mouseReleaseEvent(QMouseEvent *) { mouse_press = false; } void BFWidget::mouseMoveEvent(QMouseEvent *event) { //移动窗口 if (mouse_press) { QPoint move_pos = event->globalPos(); move(move_pos - move_point); } } //void BFWidget::resizeEvent(QResizeEvent *event) //{ // QSize size = event->size(); // container->setGeometry(5, 5, size.width() - 10, size.height() - 10); // qDebug() << container->rect(); //} void BFWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); static QColor colItemBg(255, 255, 255); // 默认背景色 QSize szThis = this->rect().size(); QPixmap pixShadowBg = _DrawNinePatch(szThis, _pixShadow); //pixShadowBg.save("c:/pixShadowBg1.png"); painter.drawPixmap(this->rect(), pixShadowBg); painter.setPen(colItemBg); //需要画背景,不然会穿透 painter.setBrush(colItemBg); painter.drawRect(6, 6, szThis.width() - 6 * 2, szThis.height() - 6 * 2); //this->setMask(pixShadowBg.mask()); //设置圆角 }
标签:qt
原文地址:http://blog.csdn.net/ahyswang/article/details/41476343