0
点赞
收藏
分享

微信扫一扫

Qt Windows下无边框解决方案

柠檬的那个酸_2333 2022-04-23 阅读 58
qt

效果展示

代码

基于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);

//重写nativeEvent实现操作拦截
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 && x <= 8 && y >= 0 && y <= 8) {
			*result = HTTOPLEFT;
			return true;
		} else if (x > 8 && x < (w - 8) && y >= 0 && y <= 8) {
			*result = HTTOP;
			return true;
		} else if (x >=(w - 8) && x <= w && y >= 0 && y <= 8) {
			*result = HTTOPRIGHT;
			return true;
		} else if (x >= 0 && x <= 8 && y > 8 && y < (h - 8)) {
			*result = HTLEFT;
			return true;
		} else if (x >=(w - 8) && x <= w && y > 8 && y < (h - 8)) {
			*result = HTRIGHT;
			return true;
		} else if (x >= 0 && x <= 8 && y >= (h - 8) && y <= h) {
			*result = HTBOTTOMLEFT;
			return true;
		} else if (x > 8 && x < (w - 8) && y >= (h - 8) && y <= h) {
			*result = HTBOTTOM;
			return true;
		} else if (x >=(w - 8) && x <= w && y >= (h - 8) && y <= 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);
}

举报

相关推荐

0 条评论