如何以编程方式获取 Linux 中窗口和系统的分辨率?
我试图获取 Linux 系统上屏幕的分辨率以及特定窗口(其中运行程序)的分辨率。 我不需要修改分辨率,我只需要当前值。 据我所知,在Windows上我们可以调用一些系统函数来做到这一点,那么在Linux上我们如何做到这一点,最好使用C/C++语言? 提前致谢。
更新:实际上,我不需要做 GUI,虽然我知道 Qt 和 GTK+ 可以做到,但我不愿意包含外部库来获得分辨率。
I'm trying to get the resolution of the screen as well as the resolution of a specific window (in which a program is running) on Linux system. I don't need to modify the resolution, I only need the current values. As far as I know, we can call some system functions to do so on Windows, how can we do that on Linux, preferably using C/C++ language? Thanks in advance.
update: Actually, I don't need to do a GUI, although I know Qt and GTK+ can do it, I'm reluctant to include an external library for just getting the resolution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要获取屏幕分辨率,您可以使用 XRandR 扩展程序,如 xrandr 源:
您可以在 xterm 中输入“xrandr”看到输出。
或者,更好的方法是使用 xdpyinfo 方法:
To get screen resolution, you can use XRandR extension, like in xrandr sources:
You can see the output typing "xrandr" in your xterm.
Or, better, use the xdpyinfo method:
在 X11 中,您需要调用 Xlib 的 XGetWindowAttributes 来获取各种窗口信息,包括相对于父窗口的大小和位置。 有关如何使用它的示例,您可以在 google 上搜索“xwininfo.c”。
也就是说,您可能会使用一些更高级的框架来进行窗口编程 - 并且它很可能已经为此提供了一些其他原语,请参阅 Qt - 所以您可能想提供有关该问题的更多背景信息。
In X11, you'd need to call the Xlib's XGetWindowAttributes to get the various window info, including the size and position relative to parent. For an example of how it is used, you can google for 'xwininfo.c'.
That said, probably you are going to use some more highlevel framework to do your window programming - and the chances are high that it already has some other primitives for this, see an example for Qt - so you might want to give a bit more background about the question.
取决于:
(参见 http://doc.qt.digia.com/4.0/qdesktopwidget.html )
Depends:
(see http://doc.qt.digia.com/4.0/qdesktopwidget.html )
命令行工具 xdpyinfo 为您提供此信息; 要以编程方式完成此操作,您需要使用 Xlib,正如 Andrew Y 所解释的那样。
The command line tool xdpyinfo provides this information for you; to do it programmatically you need to to use Xlib, as Andrew Y explains.