云起工作室 15711107967
无边框拖拽,伸缩
2025-02-06 10:42:48

1、设置无边框

	this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinMaxButtonsHint);


//当多个按钮绑定同一个方法时,获取被点击的按钮对象

QPushButton* btn = qobject_cast<QPushButton*>(sender());


this->setAttribute(Qt::WA_TranslucentBackground,true);设置窗体透明


this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);//设置无边框


setAttribute(Qt::WA_StyledBackground); //禁止父窗口影响子窗口样式,放在子窗口里



2、无边窗口拖拽移动

在哪个组件,在哪个组件上拖动窗口,其他组件不起作用

#include <qt_window.h>

#pragma comment(lib,"user32.lib")

void CTitleBar::mousePressEvent(QMouseEvent* event){

if(ReleaseCapture()){

QWidget* pWindow = this->window();

if(pWindow->isTopLevel()){

SendMessage(HWND(pWindow->winId()),WM_SYSCOMMAND,SC_MOVE+HTCAPTION,0);

}

}

event->ignore();

}



3、自定义窗口伸缩方法

nativeEvent

#include<qt_windows.h>

#include<qt_windowsx.h>

#include<windows.h>

#include<windowsx.h>

int m_nBorderWidth = 5;

bool Widget::nativeEvent(const QByteArray &eventType, void *message, long *result)

{

MSG* param = static_cast<MSG*>(message);

switch(param->message)

{

case WM_NCHITTEST:

{


int nX = GET_X_LPARAM(param->lParam) - this ->geometry().x();

int nY = GET_Y_LPARAM(param->lParam) - this ->geometry().y();

//兼容拖拽

if( (nX>m_nBorderWidth) && (nX<this->width()-m_nBorderWidth) && (nY>m_nBorderWidth) && (nY<this->height()-m_nBorderWidth) ){

if(childAt(nX,nY) !=nullptr){

return QWidget::nativeEvent(eventType,message,result);

}

}


if( (nX>0) && (nX<m_nBorderWidth) ){

*result = HTLEFT;

}

if( (nY>0) && (nY<m_nBorderWidth) ){

*result = HTTOP;

}

if( (nX>this->width() - m_nBorderWidth) && (nX<this->width()) ){

*result = HTRIGHT;

}


if( (nY>this->height()-m_nBorderWidth) && (nY<this->height()) ){

*result = HTBOTTOM;

}


if( (nX>0) && (nX<m_nBorderWidth) && (nY>0) && (nY < m_nBorderWidth) ){

*result = HTTOPLEFT;

}

if( (nX>this->width()-m_nBorderWidth) && (nX<this->width()) && (nY>0) && (nY<m_nBorderWidth) ){

*result = HTTOPRIGHT;

}


if( (nX>this->width() - m_nBorderWidth) && (nX<this->width()) && (nY>this->height() - m_nBorderWidth) && (nY<this->height()) ){

*result = HTBOTTOMRIGHT;

}

if( (nX>0) && (nX<m_nBorderWidth) && (nY > this->height() - m_nBorderWidth) && (nY<this->height()) ){

*result = HTBOTTOMLEFT;

}


return true;

}

}


return false;

}