【问题标题】:Is there any way to detect which monitor a window is on? [duplicate]有什么方法可以检测窗口在哪个监视器上? [复制]
【发布时间】:2015-02-19 01:00:33
【问题描述】:

我想制作一个针对不同显示器表现不同的应用(例如,更改 LCD、CRT、pentile 矩阵等的抗锯齿技术)。 有什么方法可以发现给定窗口在哪个监视器上?我只需要它作为一个整数。

【问题讨论】:

  • 我相信这会根据您所谈论的平台而有很大差异。视窗? Linux?麦克?
  • 我不确定是否有一种好方法可以做到这一点 - 即使有办法从系统中获取模型,您也可能必须找到一些可以告诉您的库,根据该字符串,它是什么类型的设备。也就是说,您是否有理由不能让用户从菜单中选择他们的显示器类型?我知道这很烦人,但如果你不厌其烦地针对不同的类型进行优化,那可能不会问太多。
  • 对不起,我应该指定的。我正在设置用户可以在每个单独的显示器上设置用户使用的抗锯齿技术的首选项。

标签: java antialiasing multiple-monitors


【解决方案1】:

是的,这是可能的。我将根据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()it can return null的初始调用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 2012-04-23
    • 1970-01-01
    • 2010-09-17
    • 2018-01-02
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多