屏幕显示尺寸

嗨我正在写一个图形程序,我一直在寻找一种方法来获得正在使用的屏幕的物理尺寸。 我可以获得屏幕大小(以像素为单位)以及逻辑分辨率。 我似乎无法找到的任何地方都可以获得任何显示器规格中的物理尺寸(例如19“ – 376 x 301 mm)。问题是,这些信息甚至存储在操作系统的任何位置,当它加载时正在使用的特定屏幕的驱动程序?我正在编写的程序需要在Mac和Windows上运行。

谢谢!

NT

我觉得java不可能。 如果此function在您的程序中绝对必要,您可以尝试编写一些jni代码。 否则,只需询问用户的显示器尺寸。

编辑看起来SWT可以为您提供DPI,您可以使用它来计算显示器大小: http : //www.java2s.com/Code/JavaAPI/org.eclipse.swt.graphics/DevicegetDPI.htm

但是,你必须使用SWT :)如果你想为Mac开发好看的程序,这实际上并不是一个糟糕的选择。

在SWT中,您可以使用getShell()。getDisplay()。getPrimaryMonitor()。getClientArea();

final Display display = getShell().getDisplay(); final Monitor monitor = display.getPrimaryMonitor(); final Rectangle rect; if (monitor != null) { rect = monitor.getClientArea(); } else { // In case we cannot find the primary monitor get the entire display rectangle // Note that it may include the dimensions of multiple monitors. rect = display.getBounds(); } System.out.println("Monitor width=" + String.valueOf(rect.width)); System.out.println("Monitor height=" + String.valueOf(rect.height)); 

尝试这个,

 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 

好的,按照Andreas的评论,我进行了一些搜索并找到了Windows解决方案。 您可以使用WMI。 可以使用其中一个互操作性工具(jinterop,jintegra,jawin)或通过执行外部脚本(VBScript或JScript)从Java调用WMI。 以下脚本是一个示例:

  strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" _ & strComputer & "\root\cimv2") Set colSoftware = objWMIService.ExecQuery _ ("Select * from Win32_DesktopMonitor") For Each objSoftware in colSoftware Wscript.Echo "Caprion: " & objSoftware.Caption Wscript.Echo "Description: " & objSoftware.Description Wscript.Echo "PixelsPerXLogicalInch: " & objSoftware.PixelsPerXLogicalInch Wscript.Echo "ScreenWidth: " & objSoftware.ScreenWidth Next 

将其保存在文件screen.vbs中并使用命令行cscript screen.vbs运行它然后ScreenWidth / PixelsPerXLogicalInch为您提供以英寸为单位的宽度。

有关详细信息,请参阅[http://msdn.microsoft.com/en-us/library/aa389273%28v=VS.85%29.aspx][1] 。 我相信其他操作系统类型的解决方案也存在。

没有纯Java的方法。 操作系统不关心物理屏幕大小。 解决方案就是它所需要的。 使用本机代码,您可以从驱动程序获取已连接显示的名称,并尝试查找此类型的技术规范。

正如安格斯所提到的那样 – 操作系统或驱动程序甚至可以告诉连接屏幕的dpi值,但即使在最好的情况下,至少屏幕必须报告它的dpi /物理尺寸。

使用Toolkit类的getScreenResolution()方法,该方法以每英寸点数的forms返回屏幕分辨率。 (ref javadocs)

您可以尝试使用:

 Dimension screen = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); int pixelPerInch= java.awt.Toolkit.getDefaultToolkit().getScreenResolution(); double height=screen.getHeight()/pixelPerInch; double width=screen.getWidth()/pixelPerInch; double x=Math.pow(height,2); double y=Math.pow(width,2); double diagonal= Math.sqrt(x+y); 

“对角线”值以英寸为单位。