Qt 实现无边框,可移动,可拉伸,靠屏幕边自动分屏显示的自定义窗体

分享一个无边框窗体,可以自由拖动,可以拉伸窗体,并且在靠近屏幕会自动分屏显示,跟windows自带的效果一样。最小化的同时还有动画效果。

效果图:

 

代码是网上找的拼凑过来滴~ 基本上就是重新实现Qt的nativeEvent 方法,捕捉消息。

nativeEvent 入下定义:

bool MaxWin::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
#ifdef Q_OS_WIN
	if (eventType != "windows_generic_MSG")
		return false;

	MSG* msg = static_cast(message);
	QWidget* widget = QWidget::find(reinterpret_cast(msg->hwnd));
	if (!widget)
		return false;

	switch (msg->message) {

	case WM_NCCALCSIZE: {
		*result = 0;
		return true;
	}

	case WM_NCHITTEST: 
	{
		int x = GET_X_LPARAM(msg->lParam);
		int y = GET_Y_LPARAM(msg->lParam);
		QPoint pt = mapFromGlobal(QPoint(x, y));
		*result = OnTestBorder(pt);
		if (*result == HTCLIENT)
		{
			QWidget* tempWidget = this->childAt(pt.x(), pt.y());
			if (tempWidget == NULL)
			{
				*result = HTCAPTION;
			}
		}
		return true;
	}

	case WM_GETMINMAXINFO: {
		if (::IsZoomed(msg->hwnd)) {

			RECT frame = { 0, 0, 0, 0 };
			AdjustWindowRectEx(&frame, WS_OVERLAPPEDWINDOW, FALSE, 0);
			frame.left = abs(frame.left);
			frame.top = abs(frame.bottom);
			widget->setContentsMargins(frame.left, frame.top, frame.right, frame.bottom);
		}
		else {
			widget->setContentsMargins(0, 0, 0, 0);
		}

		*result = ::DefWindowProc(msg->hwnd, msg->message, msg->wParam, msg->lParam);
		return true;
	}
	break;

	default:
		break;
	}

#endif

	return QWidget::nativeEvent(eventType, message, result);
}

 

完整代码:https://download.csdn.net/download/qq_36131739/11728233

你可能感兴趣的:(Qt)