前言
- Qt原本的窗口雖然可以通過QSS樣式進(jìn)行美化,但是只是對客戶區(qū)有用,對于客戶區(qū)是無效的 。所以想做出一個比較好看的程序,還得自己重寫實(shí)現(xiàn)無邊框窗口 。
- Qt實(shí)現(xiàn)無邊框其實(shí)一句代碼就可以,但是窗口自帶的縮放,移動功和關(guān)閉功能都會沒有,需要自己重寫 。
setWindowFlags(Qt::FramelessWindowHint);

文章插圖

文章插圖
2.由于無邊框窗口沒有了標(biāo)題欄和最小化,最大化,關(guān)閉的按鈕,所以需要自己布局相對應(yīng)的控件,并重寫事件 。我的布局如下

文章插圖
3.事件對應(yīng)代碼
展開
4.窗口移動事件,需要重寫鼠標(biāo)的點(diǎn)擊事件和移動事件//窗口關(guān)閉事件void MainWindow::windowClose(){qApp->exit();}//窗口最小化void MainWindow::windowMin(){this->showMinimized();}//窗口最大化void MainWindow::windowMax(){isMaxWin=!isMaxWin;if(isMaxWin)//根據(jù)是否最大化窗口,改變對應(yīng)的圖標(biāo){ui->btnMax->setIcon(QIcon(":/icons/normal.png"));this->showMaximized();}else{ui->btnMax->setIcon(QIcon(":/icons/maxsize.png"));this->showNormal();}}展開
3.窗口的縮放功能比較麻煩,需要用到windows的消息機(jī)制.代碼如下void MainWindow::mousePressEvent(QMouseEvent*event){if(event->button()==Qt::LeftButton) //如果鼠標(biāo)左鍵按下{isPressed=true;curPos=event->pos();//記錄當(dāng)前的點(diǎn)擊坐標(biāo)}}void MainWindow::mouseMoveEvent(QMouseEvent*event){if(isPressed) //如果鼠標(biāo)左鍵按下{this->move(event->pos()-curPos+this->pos());//窗口移動}}//鼠標(biāo)釋放void MainWindow::mouseReleaseEvent(QMouseEvent*event){isPressed=false;}展開
4.要實(shí)現(xiàn)窗口的正常功能,還需要對窗口的Flags進(jìn)行一些設(shè)置,同時也要給父類設(shè)置,要不然會有問題的.//需要包含頭文件/*#include <qt_windows.h>#include <Windowsx.h>*///消息處理bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result){int m_nBorder = 5;//邊界寬度Q_UNUSED(eventType)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();*result = HTCAPTION;//判斷鼠標(biāo)位置是否位于窗口邊界if ((nX > 0) && (nX < m_nBorder))*result = HTLEFT;if ((nX > this->width() - m_nBorder) && (nX < this->width()))*result = HTRIGHT;if ((nY > 0) && (nY < m_nBorder))*result = HTTOP;if ((nY > this->height() - m_nBorder) && (nY < this->height()))*result = HTBOTTOM;if ((nX > 0) && (nX < m_nBorder) && (nY > 0)&& (nY < m_nBorder))*result = HTTOPLEFT;if ((nX > this->width() - m_nBorder) && (nX < this->width())&& (nY > 0) && (nY < m_nBorder))*result = HTTOPRIGHT;if ((nX > 0) && (nX < m_nBorder)&& (nY > this->height() - m_nBorder) && (nY < this->height()))*result = HTBOTTOMLEFT;if ((nX > this->width() - m_nBorder) && (nX < this->width())&& (nY > this->height() - m_nBorder) && (nY < this->height()))*result = HTBOTTOMRIGHT;if (*result == HTCAPTION){return false;}return true;}}return QMainWindow::nativeEvent(eventType, message, result);}
文章插圖
其中 Qt::FramelessWindowHint設(shè)置窗口為無邊框,Qt::Window表示widegt為窗口,Qt::WindowMinimizeButtonHint 程序在任務(wù)欄被點(diǎn)擊時能夠顯示/隱藏.
經(jīng)驗(yàn)總結(jié)擴(kuò)展閱讀
- 無線藍(lán)牙耳機(jī)哪個品牌好 藍(lán)牙耳機(jī)品牌排行榜前十名
- 感慨人生無奈的句子160句
- 京東一年換新是無理由換新嗎 京東一年免費(fèi)換新條件是怎么樣的
- 【Azure 事件中心】Event Hub 無法連接,出現(xiàn) Did not observe any item or terminal signal within 60000ms in 'flatMapMany' 的錯誤消息
- 形容無恥小人的句子 送給玩心眼的人的句子
- 無期迷途灼熱之風(fēng)怎么獲取
- 我的世界怎么去月球無模組無指令(我的世界新版怎么去月球)
- 2022UUCTF--WEB
- 枕頭過高真的能無憂嗎 枕頭要不要太高
- 我的世界虛無3月球怎么去(我的世界虛無世界怎么找傳送門)
