Home > Blockchain >  cross-platform techniques to determine screen dimensions
cross-platform techniques to determine screen dimensions

Time:09-22

Is there a good cross-platform way of getting the screen dimensions? Frequently I do this with PerlTk:

 use Tk;
 my $mw = MainWindow->new;
 my $screen_width  = $mw->screenwidth();   
 my $screen_height = $mw->screenheight();  

But it'd be better to not have to load all of Tk just to do this.

This looks like a good X11 specific way of doing these things (GetRootWindow should work for screen dimensions):

Perl: Getting the geometry of a window with X11 WindowID

But I think a cross-platform approach would be better.

Specifically, I'm looking for ways to determine the monitor dimensions in pixels, which is what Tk's screenwidth and screenheight return.

CodePudding user response:

On most POSIX-y systems:

use Curses ();
my $screen_width = $Curses::COLS;
my $screen_height = $Curses::LINES;

These values don't update automatically when the screen is resized.

CodePudding user response:

The best I can see for getting monitor/display/screen resolution is to use OS-specific tools.

On X11 the best bet is probably xrandr while on Windows it'd be Win32::GUI or Win32::API.

Then wrap it in a sub that selects the OS and a tool to use.


Or of course use a GUI package, like Perl/Tk that OP is using

  •  Tags:  
  • perl
  • Related