由于显示器的分辨率尺寸各种大小都有。所以在不同分辨率的显示器上,不做屏幕适配的窗体就可能会出现显示不完整或者太小的问题。
所以我们需要根据屏幕尺寸大小去动态调整窗体大小。
但是这里有个问题需要注意:windows 显示设置中的缩放与布局、显示器分辨率都会影响获取屏幕分辨率的大小。所以要把dpi的缩放计算进去。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
void ProjectConfig::setAutoResolution(float gameWndWidth, float gameWndHeight, float rate) { HWND hWnd = GetDesktopWindow(); HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
MONITORINFOEX miex; miex.cbSize = sizeof(miex); GetMonitorInfo(hMonitor, &miex); int cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left); int cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);
DEVMODE dm; dm.dmSize = sizeof(dm); dm.dmDriverExtra = 0; EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm); int cxPhysical = dm.dmPelsWidth; int cyPhysical = dm.dmPelsHeight; double dpi_scale = ((double)cxPhysical / (double)cxLogical);
int cx = GetSystemMetrics(SM_CXSCREEN) * dpi_scale; int cy = GetSystemMetrics(SM_CYSCREEN) * dpi_scale; float width = cx > cy ? cx : cy; float height = cx > cy ? cy : cx; float scale = gameWndWidth / gameWndHeight;
cocos2d::Size size; size.width = height * rate * scale; size.height = height * rate;
setFrameSize(size); }
|