我一直在寻找一种方法,可以在全屏显示之前根据窗口的位置正确选择要全屏显示的监视器。
我在网上寻找了很长时间,但找不到任何东西,因此最终尝试了一堆东西,直到找到了可行的方法。
我认为最终有人会尝试查找此问题,因此不妨分享我的解决方案。
我最后要做的是使用LWJGL2的Display类获得窗口中心的虚拟屏幕坐标,如下所示:
int x = Display.getX() + Display.getWidth()/2,
y = Display.getY() + Display.getHeight()/2;
然后,我使用AWT获取所有可用的监视器:
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
我遍历了它们并得到了它们的虚拟边界(并应用了它们可能具有的任何DPI缩放比例):
Rectangle bounds = screenDevice.getDefaultConfiguration().getDefaultTransform().createTransformedShape(screenDevice.getDefaultConfiguration().getBounds()).getBounds();
编辑:稍作更改的行,以便它可以正确支持Windows的DPI缩放。
如果边界包含窗口的中心,则意味着大多数窗口可能在该监视器内:
if(bounds.contains(x,y))
return bounds; //getMonitorFromWindow()
然后在LibGDX中的无边框全屏窗口和普通窗口之间切换,我做了以下工作:
// config is the LwjglApplicationConfiguration of the application
// upon changing using alt+enter
if(fullscreen) {
config.resizable = false;
Gdx.graphics.setUndecorated(true);
Rectangle monitor = getMonitorFromWindow();
// set to full screen in current monitor
Gdx.graphics.setWindowedMode(monitor.width, monitor.height);
Display.setLocation(monitor.x, monitor.y);
} else {
config.resizable = true;
Gdx.graphics.setUndecorated(false);
Rectangle monitor = getMonitorFromWindow();
// set to windowed centered in current monitor
Gdx.graphics.setWindowedMode((int) (monitor.width * 0.8f), (int) (monitor.height * 0.8f));
Display.setLocation(monitor.x + (int) (monitor.width * 0.1f), monitor.y + (int) (monitor.height * 0.1f));
}
我希望有人会觉得有用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句