gpt4 book ai didi

java - 如何在多台显示器上正确切换全屏无边框

转载 作者:行者123 更新时间:2023-12-02 01:13:59 25 4
gpt4 key购买 nike

我正在寻找一种方法来根据全屏之前窗口的位置正确选择要全屏显示的监视器。

我在网上查了很长时间,但找不到任何东西,所以我最终尝试了很多东西,直到我找到了一些东西。

我认为最终会有人尝试查找这个问题,我不妨分享我的解决方案。

最佳答案

我最终做的是使用 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));
}

我希望有人会觉得这很有用。

关于java - 如何在多台显示器上正确切换全屏无边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58938562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com