How to get screen resolution in ruby

时光毁灭记忆、已成空白 提交于 2019-12-11 02:07:29

问题


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


回答1:


On Linux:

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

On windows, use WIN32OLE and such.




回答2:


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




回答3:


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



回答4:


I solved it using tput, e.g.

cols  = %x[tput cols].to_i



回答5:


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.




回答6:


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


来源:https://stackoverflow.com/questions/4154262/how-to-get-screen-resolution-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!