How to programmatically get the resolution of a window and that of the system in Linux?

前端 未结 4 483
庸人自扰
庸人自扰 2021-02-06 00:06

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 resol

相关标签:
4条回答
  • 2021-02-06 00:37

    To get screen resolution, you can use XRandR extension, like in xrandr sources:

      SizeID        current_size;
      XRRScreenSize *sizes;
      dpy = XOpenDisplay (display_name);
      // ...
      root = RootWindow (dpy, screen);
      sc = XRRGetScreenInfo (dpy, root);
      current_size = XRRConfigCurrentConfiguration (sc, &current_rotation);
      sizes = XRRConfigSizes(sc, &nsize);
      for (i = 0; i < nsize; i++) {
        printf ("%c%-2d %5d x %-5d  (%4dmm x%4dmm )",
             i == current_size ? '*' : ' ',
             i, sizes[i].width, sizes[i].height,
             sizes[i].mwidth, sizes[i].mheight);
         // ...
      }
    

    You can see the output typing "xrandr" in your xterm.

    Or, better, use the xdpyinfo method:

    Display *dpy;
    // dpy = ...
    int scr = /* ... */
    printf ("  dimensions:    %dx%d pixels (%dx%d millimeters)\n",
            DisplayWidth (dpy, scr), DisplayHeight (dpy, scr),
            DisplayWidthMM(dpy, scr), DisplayHeightMM (dpy, scr));
    
    0 讨论(0)
  • 2021-02-06 00:42

    The command line tool xdpyinfo provides this information for you; to do it programmatically you need to to use Xlib, as Andrew Y explains.

    0 讨论(0)
  • 2021-02-06 00:44

    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.

    0 讨论(0)
  • 2021-02-06 00:54

    Depends:

    • If you're hardcore and you want to use Xlib, have a look at XDisplayWidth() and XDisplayHeight().
    • If you use Qt4 (my preference), try
       QApplication::desktop()->screenGeometry() 
      (see http://doc.qt.digia.com/4.0/qdesktopwidget.html )
    • Or in Gtk, see http://library.gnome.org/devel/gdk/stable/GdkScreen.html
    0 讨论(0)
提交回复
热议问题