標籤:
這裡討論的只是Windows平台上的實現。
在QT中繪製異形視窗,只要設定 windowFlag 為 CustomizeWindowHint,再結合setMask()就可以做出各種奇形怪狀的視窗。相對來說比較麻煩的, 是進行視窗拖動和縮放的處理。
在 Windows SDK 和 MFC 中比較容易,只要處理 WM_NCHITTEST,返回相應的測試值就可以了。幸運的是,QT中也提供了直接處理各平台訊息的方法,在 Windows下只需要重載winEvent方法。
下面給出了範例程式碼:
// include <windows.h>bool MyDialog::winEvent(MSG* msg, long* result){ const int captionHeight = 25; const int frameWidth = 6; if (msg->message != WM_NCHITTEST) return false; QPoint pos = mapFromGlobal(QCursor::pos()); int w = width(); int h = height(); if (QRect(frameWidth, captionHeight, w-frameWidth-frameWidth, h-captionHeight-frameWidth).contains(pos)) { *result = HTCLIENT; } else if (QRect(0, 0, w, captionHeight).contains(pos)) { *result = HTCAPTION; } else if (QRect(0, captionHeight, frameWidth, h-captionHeight-frameWidth).contains(pos)) { *result = HTLEFT; } else if (QRect(w-frameWidth, captionHeight, frameWidth, h-captionHeight-frameWidth).contains(pos)) { *result = HTRIGHT; } else if (QRect(frameWidth, h-frameWidth, w-frameWidth-frameWidth, frameWidth).contains(pos)) { *result = HTBOTTOM; } else if (QRect(0, h-frameWidth, frameWidth, frameWidth).contains(pos)) { *result = HTBOTTOMLEFT; } else if (QRect(w-frameWidth, h-frameWidth, frameWidth, frameWidth).contains(pos)) { *result = HTBOTTOMRIGHT; } return true;}
參考:http://www.cppblog.com/eXile/archive/2007/12/09/38084.html
QT中異形視窗的繪製(winEvent處理WM_NCHITTEST訊息)