How to get screen resolution (height, width) in ruby scr开发者_开发问答ipt?
On Linux:
x, y = `xrandr`.scan(/current (\d+) x (\d+)/).flatten
On windows, use WIN32OLE and such.
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
Ruby has no notion of a GUI. You would need to use somethign like the Ruby Xlib Wrapper for this.
I solved it using tput
,
e.g.
cols = %x[tput cols].to_i
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.
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}"
精彩评论