效果展示
代码
基于windowsAPI实现
#include <windowsx.h>
#include<windows.h>
this->setWindowFlags(this->windowFlags()| Qt::FramelessWindowHint);
HWND hwnd = reinterpret_cast<HWND>(winId());
LONG style = GetWindowLong(hwnd, GWL_STYLE);
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION);
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG* msg = reinterpret_cast<MSG*>(message);
switch(msg->message) {
case WM_NCCALCSIZE:
{
*result = 0;
return true;
}
case WM_NCHITTEST:
{
auto x = GET_X_LPARAM(msg->lParam) - this->x();
auto y = GET_Y_LPARAM(msg->lParam) - this->y();
auto w = width();
auto h = height();
if (x >= 0 8 0 8) {
*result = HTTOPLEFT;
return true;
} else if (x > 8 && x < (w - 8) 0 8) {
*result = HTTOP;
return true;
} else if (x >=(w - 8) w 0 8) {
*result = HTTOPRIGHT;
return true;
} else if (x >= 0 8 && y > 8 && y < (h - 8)) {
*result = HTLEFT;
return true;
} else if (x >=(w - 8) w && y > 8 && y < (h - 8)) {
*result = HTRIGHT;
return true;
} else if (x >= 0 8 (h - 8) h) {
*result = HTBOTTOMLEFT;
return true;
} else if (x > 8 && x < (w - 8) (h - 8) h) {
*result = HTBOTTOM;
return true;
} else if (x >=(w - 8) w (h - 8) h) {
*result = HTBOTTOMRIGHT;
return true;
}
if (y<38&&x<width()-100){
*result = HTCAPTION;
return true;
} else {
*result = HTCLIENT;
return true;
}
}
}
return QMainWindow::nativeEvent(eventType, message, result);
}