Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby:如何在Windows中获得屏幕分辨率_Ruby_Windows - Fatal编程技术网

Ruby:如何在Windows中获得屏幕分辨率

Ruby:如何在Windows中获得屏幕分辨率,ruby,windows,Ruby,Windows,不久前我看到一篇帖子,但主要答案是针对Linux的。目前,使用Ruby on Windows获得屏幕分辨率(宽度/高度)的最简单方法是什么。一种简单的方法是包装系统命令并在Ruby中执行它们: @screen = `wmic desktopmonitor get screenheight, screenwidth` 您可以显示它们,也可以将其输出保存在文件中 要实际解析它,我在以下位置找到了Windows cmd.exe的帮助程序: 对于/f%%i in('wmic desktopmonito

不久前我看到一篇帖子,但主要答案是针对Linux的。目前,使用Ruby on Windows获得屏幕分辨率(宽度/高度)的最简单方法是什么。

一种简单的方法是包装系统命令并在Ruby中执行它们:

@screen = `wmic desktopmonitor get screenheight, screenwidth`
您可以显示它们,也可以将其输出保存在文件中

要实际解析它,我在以下位置找到了Windows cmd.exe的帮助程序:

对于/f%%i in('wmic desktopmonitor获取屏幕高度^,屏幕宽度/值^查找“=”),请设置“%%f”
回显您的屏幕为%screenwidth%*%screenheight%像素
通过这种方式,您可以轻松地获取变量中的值,并将它们存储在Ruby程序中


不过,我找不到一个简单的gem来实现这一点,就像您在Linux上所做的那样。

您可以按照建议在使用WIN32OLE库的平台上尝试这段代码。不过,这只适用于Windows


我还建议直接使用系统命令包装。 在win7上测试

# also this way
res_cmd =  %x[wmic desktopmonitor get screenheight, screenwidth]
res = res_cmd.split
p w = res[3].to_i
p h = res[2].to_i

# or this way
command = open("|wmic desktopmonitor get screenheight, screenwidth")
res_cmd = command.read()
res = res_cmd.split
p w = res[3].to_i
p h = res[2].to_i

# or making a method
def screen_res
    res_cmd =  %x[wmic desktopmonitor get screenheight, screenwidth]
    res = res_cmd.split
    return res[3].to_i, res[2].to_i
end

w, h = screen_res

p w
p h
你试过了吗?
# also this way
res_cmd =  %x[wmic desktopmonitor get screenheight, screenwidth]
res = res_cmd.split
p w = res[3].to_i
p h = res[2].to_i

# or this way
command = open("|wmic desktopmonitor get screenheight, screenwidth")
res_cmd = command.read()
res = res_cmd.split
p w = res[3].to_i
p h = res[2].to_i

# or making a method
def screen_res
    res_cmd =  %x[wmic desktopmonitor get screenheight, screenwidth]
    res = res_cmd.split
    return res[3].to_i, res[2].to_i
end

w, h = screen_res

p w
p h