有没有办法检测窗口在哪个显示器上?

3

我希望制作一个应用程序,在不同的显示器上表现出不同的行为(例如,针对液晶屏、CRT、pentile矩阵等更改抗锯齿技术)。有没有办法发现给定窗口所在的显示器? 我只需要这个作为整数。


2
我相信这将根据你所谈论的平台而大不相同。Windows?Linux?Mac? - Falmarri
我不确定有什么好的方法可以做到这一点 - 即使有一种从系统中获取模型的方法,您可能还需要找到一些库,可以根据该字符串告诉您设备的类型。 话虽如此,您是否无法让用户从菜单中选择其监视器类型? 我知道这很烦人,但如果您要为不同类型进行优化,这可能不是太过分的要求。 - childofsoong
抱歉,我应该有说明清楚。我正在制作一个偏好设置,让用户可以为每个单独的监视器设置抗锯齿技术。 - Ky -
1个回答

3

是的,这是可能的。我会根据 Window 的顶部 x,y 位置来抓取监视器(如果您将框架的顶半部分从任何监视器中移开,则会失败,在一般情况下,此代码不够健壮,但是可以作为一个示例让您开始)。

public GraphicsDevice getMonitorWindowIsOn(Window window)
{
    // First, grab the x,y position of the Window object
    GraphicsConfiguration windowGraphicsConfig = window.getGraphicsConfiguration();
    if (windowGraphicsConfig == null)
    {
         return null; // Should probably be handled better
    }
    Rectangle windowBounds = windowGraphicsConfig.getBounds();

    for (GraphicsDevice gd : ge.getScreenDevices()) {
        Rectangle monitorBounds = gd.getDefaultConfiguration().getBounds();
        if (monitorBounds.contains(windowBounds.x, windowBounds.y))
        {
            return gd;
        }
    }
    // um, return null I guess, should make this default better though,
    // maybe to the default screen device, except, I am sure if no monitors are
    // connected that may be null as well.
    return null;
}

关于 window.getGraphicsConfiguration() 的初始调用,它可能返回 null

获取与此组件相关联的 GraphicsConfiguration。如果此组件未分配特定的 GraphicsConfiguration,则返回 Component 对象的顶级容器的 GraphicsConfiguration。如果已创建组件但尚未添加到容器中,则此方法返回 null。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接