ruby中如何获取屏幕分辨率

发布于 2024-10-02 00:34:50 字数 32 浏览 4 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

メ斷腸人バ 2024-10-09 00:34:50

在 Linux 上:

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

在 Windows 上,使用 WIN32OLE 等

On Linux:

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

On windows, use WIN32OLE and such.

美胚控场 2024-10-09 00:34:50

这就是我解决问题的方法。由于我使用的是 Ruby 2.3.0,所以我无法使用 DL 模块(因为它已被废弃)。以下是使用 Fiddle

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

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

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
初熏 2024-10-09 00:34:50

Ruby 没有 GUI 的概念。为此,您需要使用 Ruby Xlib Wrapper 之类的东西。

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

烂柯人 2024-10-09 00:34:50

我使用 tput 解决了这个问题,
例如

cols  = %x[tput cols].to_i

I solved it using tput,
e.g.

cols  = %x[tput cols].to_i
枫林﹌晚霞¤ 2024-10-09 00:34:50

我在寻找有关如何处理多显示器设置的解决方案时遇到了此页面,因此我将在此处添加我找到的内容。对我来说,最好的解决方案是使用 Qt,可以按如下方式完成:

require 'Qt4'

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

screenGeometry 返回的对象是一个 QRect ,它具有高度、宽度和 一大堆其他可能有用的属性。显然,这是专门针对主屏幕的,但您也可以使用desktop.numScreens来确定有多少个屏幕并单独检查它们。

我意识到这个问题很老了,但希望这对某人有用。

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:

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.

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

岁月如刀 2024-10-09 00:34:50

来自 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}"

From Ruby Forum

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}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文