请帮助这个初学者的程序获取gdk屏幕属性。我有一个小的C ++程序来找出连接的显示单元。我在Linux Debian上使用c ++。 gdk_screen_get_default()
不会返回Screen
个对象。如果我没有检查屏幕对象,则会发生以下错误。
错误 (进程:8023):Gdk-CRITICAL **:gdk_screen_get_monitor_geometry:断言'GDK_IS_SCREEN(屏幕)'失败
我浏览了相关帖子并提到了this以获取以下代码段。
感谢您的帮助。任何解决此问题的指针/指南都会有所帮助。
我连接了一台显示器,显示设置为
$ echo $XDG_CURRENT_DESKTOP
GNOME
$ echo $DISPLAY
:0
CODE
#include <gdk/gdk.h>
#include <iostream>
/*
GTK version 3.14.5
g++ getScreenInfo.cpp -o getScreenInfo `pkg-config gtk+-3.0 --cflags --libs`
*/
int main()
{
GdkScreen *screen;
screen = gdk_screen_get_default();
int num_monitors;
int i;
if (screen)
{
num_monitors = gdk_screen_get_n_monitors(screen);
for (i = 0; i < num_monitors; i++)
{
GdkRectangle rect;
gdk_screen_get_monitor_geometry (screen, i, &rect);
std::cout << "monitor " << i << ": coordinates (" << rect.x << ","
<< rect.y << ", size (" << rect.width << "," << rect.height << ")"
<< std::endl;
}
}else
{
std::cout << "Couldn't obtain default screen object" << std::endl;
}
}
2017年4月27日编辑:已解决
#include <iostream>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
/*
GTK version 3.14.5
To compile:
g++ getScreenInfo.cpp -o getScreenInfo `pkg-config gtk+-3.0 --cflags --libs`
*/
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GdkScreen *screen = gdk_screen_get_default();
int num_monitors;
int i;
if (screen)
{
num_monitors = gdk_screen_get_n_monitors(screen);
for (i = 0; i < num_monitors; i++)
{
GdkRectangle rect;
gdk_screen_get_monitor_geometry (screen, i, &rect);
std::cout << "monitor " << i << ": offsets (" << rect.x << ","
<< rect.y << ", size (" << rect.width << "," << rect.height << ")"
<< std::endl;
}
}
else
{
std::cout << "Couldn't obtain default screen object" << std::endl;
}
// To query primary display properties
guint monitor = gdk_screen_get_primary_monitor(screen);
GdkRectangle screen_geometry = { 0, 0, 0, 0 };
gdk_screen_get_monitor_geometry(screen, monitor, &screen_geometry);
std::cout << screen_geometry.x << std::endl;
std::cout << screen_geometry.y << std::endl;
std::cout << screen_geometry.width << std::endl;
std::cout << screen_geometry.height << std::endl;
}
答案 0 :(得分:2)
回答我自己的问题。
最后,找出决议。在获取屏幕之前,缺少gtk_init()。添加了gtk / gtk.h的相应包含,现在代码看起来像
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv); // <---- added this
GdkScreen *screen = gdk_screen_get_default();
:
followed by rest of the code shared in the problem description above.