cococs在Windows下全屏实现和窗口最大化

本文解决Cocos2d-x在Windows环境下全屏显示时出现黑色边框及多次切换全屏导致屏幕缩小的问题。通过修改GLViewImpl类的onGLFWWindowSizeFunCallback、exitFullscreen和enterFullscreen函数,实现真正的无边框全屏效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目需要在windows下实现全屏功能,本人版本3.14,参考https://blog.youkuaiyun.com/wiyun_beijing/article/details/23423785代码发现,发现并不是能满足自己的要求,虽然可以实现全屏,然而上半部分有一条黑色边框,如下图:

且多次全屏切换后,屏幕会慢慢变小。分析代码发现,每次屏幕改变代码后,cocos 会调用void onGLFWWindowSizeFun Callback(),最终调用glfwSetWindowSize(),虽然是全屏,然而边框部分并未绘制。修改可用代码如下:

void GLViewImpl::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
{
	if (bFullScreen)
		return;
    if (width && height && _resolutionPolicy != ResolutionPolicy::UNKNOWN)
    {
        Size baseDesignSize = _designResolutionSize;
		ResolutionPolicy baseResolutionPolicy =  _resolutionPolicy;
		if ((_preWindowWidth != _fullScreenWidth) && (width != _fullScreenWidth))
		{
			width = _preWindowWidth;
			height = _preWindowHeight;
			_bChangScreen = true;
		}
		//_lastWidth = width;
		//_lastHeight = height;
        int frameWidth = width / _frameZoomFactor;
        int frameHeight = height / _frameZoomFactor;
        setFrameSize(frameWidth, frameHeight);
        setDesignResolutionSize(baseDesignSize.width, baseDesignSize.height, baseResolutionPolicy);
        Director::getInstance()->setViewport();
    }
}
bool GLViewImpl::exitFullscreen()
{
	bFullScreen = false;
	_preWindowWidth ;
	_preWindowHeight ;
	SetWindowLongPtr(this->getWin32Window(), GWL_EXSTYLE, WS_EX_LEFT);
	SetWindowLongPtr(this->getWin32Window(), GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_MAXIMIZEBOX);
	SetWindowPos(this->getWin32Window(), HWND_TOPMOST, 0, 0, _preWindowWidth, _preWindowHeight, SWP_SHOWWINDOW);
	ShowWindow(this->getWin32Window(), SW_RESTORE);
	ResolutionPolicy baseResolutionPolicy = ResolutionPolicy::NO_BORDER;
	Size baseDesignSize = _designResolutionSize;
	int frameWidth = _preWindowWidth / _frameZoomFactor;
	int frameHeight = _preWindowHeight / _frameZoomFactor;
	setFrameSize(frameWidth, frameHeight);
	setDesignResolutionSize(baseDesignSize.width, baseDesignSize.height, baseResolutionPolicy);
	Director::getInstance()->setViewport();
	return true;
}
void GLViewImpl::enterFullscreen()
{
	if (bFullScreen)
		return;
	bFullScreen = true;
	Size size = getFrameSize();
	_preWindowWidth = size.width;
	_preWindowHeight = size.height;
	DEVMODE fullscreenSettings;
	int fullscreenWidth, fullscreenHeight;
	int colourBits, refreshRate;
	Device::getColourAndRate(colourBits, refreshRate);
	Device::getWidthAndHeight(fullscreenWidth, fullscreenHeight);
	Size baseDesignSize = _designResolutionSize;
	ResolutionPolicy baseResolutionPolicy = ResolutionPolicy::NO_BORDER;

	int frameWidth = fullscreenWidth / _frameZoomFactor;
	int frameHeight =  fullscreenHeight / _frameZoomFactor;

	EnumDisplaySettings(NULL, 0, &fullscreenSettings);
	fullscreenSettings.dmPelsWidth = fullscreenWidth;
	fullscreenSettings.dmPelsHeight = fullscreenHeight;
	fullscreenSettings.dmBitsPerPel = colourBits;
	fullscreenSettings.dmDisplayFrequency = refreshRate;
	fullscreenSettings.dmFields = DM_PELSWIDTH |
		DM_PELSHEIGHT |
		DM_BITSPERPEL |
		DM_DISPLAYFREQUENCY;
	
	SetWindowLongPtr(this->getWin32Window(), GWL_EXSTYLE, WS_EX_APPWINDOW | WS_EX_TOPMOST);
	SetWindowLongPtr(this->getWin32Window(), GWL_STYLE, WS_POPUP | WS_VISIBLE);
	bool isChangeSuccessful = ChangeDisplaySettings(&fullscreenSettings, CDS_FULLSCREEN) == DISP_CHANGE_SUCCESSFUL;
	ShowWindow(this->getWin32Window(), SW_MAXIMIZE);	
	GLView::setFrameSize(frameWidth, frameHeight);
	setDesignResolutionSize(baseDesignSize.width, baseDesignSize.height, baseResolutionPolicy);
	Director::getInstance()->setViewport();
	
}

窗口最大化功能需要在下面函数添加同时onGLFWWindowSizeFunCallback修改如上:

bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor, bool resizable)
{
    。。。。。。。。。。。。。。。。
    SetWindowLongPtr(this->getWin32Window(), GWL_STYLE, WS_VISIBLE | WS_MINIMIZEBOX | 
    WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU);// |
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值