项目需要在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);// |
}