如何在ruby中获得屏幕分辨率

时间:2021-07-07 01:38:26

How to get screen resolution (height, width) in ruby script?

如何在ruby脚本中获得屏幕分辨率(高度、宽度)?

6 个解决方案

#1


3  

On Linux:

在Linux上:

x, y = `xrandr`.scan(/current (\d+) x (\d+)/).flatten

On windows, use WIN32OLE and such.

在windows上,使用WIN32OLE等。

#2


2  

Ruby has no notion of a GUI. You would need to use somethign like the Ruby Xlib Wrapper for this.

Ruby没有GUI的概念。为此需要使用Ruby Xlib之类的设计。

#3


2  

This is how I solved my problem of getting resolution. As I was using Ruby 2.3.0, I cannot use DL module(as its been depricated). Following is by using Fiddle

这就是我解决问题的办法。因为我使用的是Ruby 2.3.0,所以不能使用DL模块(因为它被剥夺了)。接下来是使用小提琴

usr32=Fiddle::dlopen("user32")
gsm=Fiddle::Function.new(usr32["GetSystemMetrics"],[Fiddle::TYPE_LONG],Fiddle::TYPE_LONG)
x= gsm.call(0)
y= gsm.call(1)
puts x,y

#4


0  

I came across this page while looking for solutions on how to deal with multi-monitor setups, so I'll add what I found here. For me the best solution was using Qt, which can be done as follows:

我在寻找如何处理多显示器设置的解决方案时遇到了这一页,因此我将在这里添加我发现的内容。对我来说,最好的解决方法是使用Qt,可以做以下事情:

require 'Qt4'

desktop = Qt::DesktopWidget.new
desktop.screenGeometry(desktop.primaryScreen)

The object returned by screenGeometry is a QRect which has height, width and a whole bunch of other potentially useful attributes. Obviously this is specifically for the primary screen, but you could also use desktop.numScreens to determine how many screens there are and check them all individually.

由screen几何体返回的对象是一个QRect,它具有高度、宽度和一大堆其他可能有用的属性。显然,这是专门针对主屏幕的,但您也可以使用桌面。数字屏幕,以确定有多少屏幕,并分别检查它们。

I realise this question is old, but hopefully this will be useful to someone.

我意识到这个问题由来已久,但希望它对某些人有用。

#5


0  

From Ruby Forum

从Ruby论坛

require 'dl/import'
require 'dl/struct'

SM_CXSCREEN =   0
SM_CYSCREEN =   1

user32 = DL.dlopen("user32")

get_system_metrics = user32['GetSystemMetrics', 'ILI']
x, tmp = get_system_metrics.call(SM_CXSCREEN,0)
y, tmp = get_system_metrics.call(SM_CYSCREEN,0)

puts "#{x} x #{y}"

#6


-1  

I solved it using tput, e.g.

我用tput解决了这个问题。

cols  = %x[tput cols].to_i

#1


3  

On Linux:

在Linux上:

x, y = `xrandr`.scan(/current (\d+) x (\d+)/).flatten

On windows, use WIN32OLE and such.

在windows上,使用WIN32OLE等。

#2


2  

Ruby has no notion of a GUI. You would need to use somethign like the Ruby Xlib Wrapper for this.

Ruby没有GUI的概念。为此需要使用Ruby Xlib之类的设计。

#3


2  

This is how I solved my problem of getting resolution. As I was using Ruby 2.3.0, I cannot use DL module(as its been depricated). Following is by using Fiddle

这就是我解决问题的办法。因为我使用的是Ruby 2.3.0,所以不能使用DL模块(因为它被剥夺了)。接下来是使用小提琴

usr32=Fiddle::dlopen("user32")
gsm=Fiddle::Function.new(usr32["GetSystemMetrics"],[Fiddle::TYPE_LONG],Fiddle::TYPE_LONG)
x= gsm.call(0)
y= gsm.call(1)
puts x,y

#4


0  

I came across this page while looking for solutions on how to deal with multi-monitor setups, so I'll add what I found here. For me the best solution was using Qt, which can be done as follows:

我在寻找如何处理多显示器设置的解决方案时遇到了这一页,因此我将在这里添加我发现的内容。对我来说,最好的解决方法是使用Qt,可以做以下事情:

require 'Qt4'

desktop = Qt::DesktopWidget.new
desktop.screenGeometry(desktop.primaryScreen)

The object returned by screenGeometry is a QRect which has height, width and a whole bunch of other potentially useful attributes. Obviously this is specifically for the primary screen, but you could also use desktop.numScreens to determine how many screens there are and check them all individually.

由screen几何体返回的对象是一个QRect,它具有高度、宽度和一大堆其他可能有用的属性。显然,这是专门针对主屏幕的,但您也可以使用桌面。数字屏幕,以确定有多少屏幕,并分别检查它们。

I realise this question is old, but hopefully this will be useful to someone.

我意识到这个问题由来已久,但希望它对某些人有用。

#5


0  

From Ruby Forum

从Ruby论坛

require 'dl/import'
require 'dl/struct'

SM_CXSCREEN =   0
SM_CYSCREEN =   1

user32 = DL.dlopen("user32")

get_system_metrics = user32['GetSystemMetrics', 'ILI']
x, tmp = get_system_metrics.call(SM_CXSCREEN,0)
y, tmp = get_system_metrics.call(SM_CYSCREEN,0)

puts "#{x} x #{y}"

#6


-1  

I solved it using tput, e.g.

我用tput解决了这个问题。

cols  = %x[tput cols].to_i