窗体拖动
//窗体按图片的形状显示
QPixmap m_pBg.load("./Resources/image/bonus_get_ok.png");
//重载paintEvent
void Demo::paintEvent(QPaintEvent* pEvent)
{
if(!m_pBg.isNull())
{
QPainter painter;
painter.begin(this);
painter.drawPixmap(m_rectMain,m_pBg);//m_rectMain 窗体的rect()
painter.end();
}
}
//改变窗口的样式
setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(windowFlags() & ~Qt::WindowMinMaxButtonsHint | Qt::FramelessWindowHint /*| Qt::WindowStaysOnTopHint*/);
ui.setupUi(this);
//重载 nativeEvent,实现窗体可拖动
bool Demo::nativeEvent( const QByteArray & eventType, void * message, long * result )
{
const MSG* pMsg=static_cast<const MSG*>(message);
if(pMsg && pMsg->message == WM_NCHITTEST)
{
QPoint pos=QPoint(GET_X_LPARAM (pMsg->lParam), GET_Y_LPARAM (pMsg->lParam));
pos=mapFromGlobal(pos);
bool bRes=false;
if(this->geometry().contains(pos))
{
bRes=true;
}
if(bRes)
return QWidget::nativeEvent(eventType,message,result);
auto pChild=childAt(pos);
if(!pChild ||
(!pChild->inherits("QAbstractButton")&&
!pChild->inherits("QComboBox")&&
!pChild->inherits("QLineEdit")&&
pChild->objectName() != "qt_scrollarea_viewport"))
{
*result=HTCAPTION;
return true;
}
}
return QWidget::nativeEvent(eventType,message,result);
}