0
点赞
收藏
分享

微信扫一扫

LINUX gdk/X11正确获取DPI/Resolution的函数

Go_Viola 2022-01-28 阅读 65


首先是屏幕的宽高是很容易获取的。所以就不多说了。

这里的Resolution,不是指宽高,而是指dpi。

  • 错误算法

  试图想自己计算的肯定不对,结果一直是96。典型的如下:

GTK:
GdkDisplay* display = gdk_display_get_default();
GdkScreen* screen = gdk_screen_get_default();

int width = gdk_screen_get_width( screen);
int widthmm = gdk_screen_get_width_mm(screen);
printf("width=%d, widthmm=%d\n", width, widthmm);


x11:
Display *display = XOpenDisplay(NULL);
if (display == NULL)
{
fprintf(stderr, "Cannot open display\n");
exit(1);
}

int screen = DefaultScreen(display);
double xres = ((((double) DisplayWidth(display,screen)) * 25.4)/
((double) DisplayWidthMM(display,screen)));
double yres = ((((double) DisplayHeight(display,screen)) * 25.4)/
((double) DisplayHeightMM(display,screen)));
  • 正确函数gdk

这个是反复在网上搜索,终于找到的。

gtk_init(NULL, NULL);

GdkScreen* screen = gdk_screen_get_default();
printf("resolution=%f\n", gdk_screen_get_resolution(screen));
  • 正确函数x11

XResourceManagerString是从友商学习到的。初次看到这个函数名,觉得难以置信。

Display* pDisplay = XOpenDisplay(NULL);
printf("%s\n", XResourceManagerString(pDisplay));

//会崩溃
//int screen = DefaultScreen(pDisplay);
//printf("%s\n", XScreenResourceString(screen));

输出的最后:
Xcursor.size: 24
Xcursor.theme: dark-sense
Xft.antialias: 1
Xft.dpi: 96
Xft.hinting: 1
Xft.hintstyle: hintslight
Xft.lcdfilter: lcddefault
Xft.rgba: rgb

Xft.dpi:    96就是我们迫切需要的东西。 


举报

相关推荐

0 条评论