免费A级毛片无码专区网站-成人国产精品视频一区二区-啊 日出水了 用力乖乖在线-国产黑色丝袜在线观看下-天天操美女夜夜操美女-日韩网站在线观看中文字幕-AV高清hd片XXX国产-亚洲av中文字字幕乱码综合-搬开女人下面使劲插视频

FrameLess Qt--無邊框窗口完美實(shí)現(xiàn),包含縮放和移動功能重寫。

前言

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

    文章插圖
重寫無邊框窗口1.效果如下
FrameLess Qt--無邊框窗口完美實(shí)現(xiàn),包含縮放和移動功能重寫。

文章插圖
2.由于無邊框窗口沒有了標(biāo)題欄和最小化,最大化,關(guān)閉的按鈕,所以需要自己布局相對應(yīng)的控件,并重寫事件 。我的布局如下
FrameLess Qt--無邊框窗口完美實(shí)現(xiàn),包含縮放和移動功能重寫。

文章插圖
3.事件對應(yīng)代碼
展開//窗口關(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();}}
4.窗口移動事件,需要重寫鼠標(biāo)的點(diǎn)擊事件和移動事件
展開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;}
3.窗口的縮放功能比較麻煩,需要用到windows的消息機(jī)制.代碼如下
展開//需要包含頭文件/*#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);}
4.要實(shí)現(xiàn)窗口的正常功能,還需要對窗口的Flags進(jìn)行一些設(shè)置,同時也要給父類設(shè)置,要不然會有問題的.
FrameLess Qt--無邊框窗口完美實(shí)現(xiàn),包含縮放和移動功能重寫。

文章插圖
其中 Qt::FramelessWindowHint設(shè)置窗口為無邊框,Qt::Window表示widegt為窗口,Qt::WindowMinimizeButtonHint 程序在任務(wù)欄被點(diǎn)擊時能夠顯示/隱藏.

經(jīng)驗(yàn)總結(jié)擴(kuò)展閱讀