Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Determine screen resolution (on Windows)

by mdog (Pilgrim)
on Jul 12, 2006 at 15:56 UTC ( [id://560727]=perlquestion: print w/replies, xml ) Need Help??

mdog has asked for the wisdom of the Perl Monks concerning the following question:

Brethern --

Is there any way to determine the resolution / dimensions of the screen where a perl script would be running...on Windows?

Ideally, it would detect the number of monitors and for each monitor report the resolution.

I've check the internets, super search, and CPAN (Win32::GUI) and can't find anything so far.

Any ideas?

Thanks,
mdog

  • Comment on Determine screen resolution (on Windows)

Replies are listed 'Best First'.
Re: Determine screen resolution (on Windows)
by shonorio (Hermit) on Jul 12, 2006 at 18:05 UTC
    Yes, you can do this, fellow my script :
    use Win32::API; Win32::API::Struct->typedef( RECT => qw{ LONG Left; LONG Top; LONG Right; LONG Bottom; }); Win32::API->Import("User32", "int GetDesktopWindow ()"); Win32::API->Import("User32", "int GetWindowRect ( int hWnd, LPRECT lpR +ect)"); my $h = GetDesktopWindow(); my $r = Win32::API::Struct->new('RECT'); GetWindowRect ( $h, $r ); print "Screen Resolution : ", $r->{Right} - $r->{Left}, ' x ', $r->{Bottom} - $r->{Top};
    ... or this code to enumerate all monitor. I didn't tested because I don't have host with more than one monitor >( !!
    use Win32::API; use Win32::API::Callback; Win32::API::Struct->typedef( RECT => qw{ LONG Left; LONG Top; LONG Right; LONG Bottom; }); Win32::API::Struct->typedef( MONITORINFO => qw{ LONG cbSize; RECT rcMonitor; RECT rcWork; LONG dwFlags; }); Win32::API->Import('User32', 'EnumDisplayMonitors', 'NPKN', 'N'); Win32::API->Import('User32', 'int GetMonitorInfoA (int hMonitor, LPMON +ITORINFO lpmi)'); my $MonitorEnumProc = Win32::API::Callback->new( sub { my( $hMonitor, $hdcMonitor, $lprcMonitor, $dwData) = @_; my $MI = Win32::API::Struct->new('MONITORINFO'); my $R = Win32::API::Struct->new('RECT'); $MI->{cbSize} = 40; GetMonitorInfoA ($hMonitor, $MI); print "Monitor resolution : ", $MI->{rcMonitor}->{Right} - $MI->{rcMonitor}->{Left}, ' x +', $MI->{rcMonitor}->{Bottom} - $MI->{rcMonitor}->{Top}, "\n" +; return 1; }, "NNPN", "N", ); EnumDisplayMonitors ( 0x0, 0x0, $MonitorEnumProc, 0x0 );
    Solli Moreira Honorio
    Sao Paulo - Brazil
Re: Determine screen resolution (on Windows)
by larryk (Friar) on Jul 12, 2006 at 16:06 UTC
    You should be able to get the size of the desktop window by using the desktop window handle which you can get from Win32::GUI. I'm sure I have done this before (to position a Win32::GUI window in the centre of the screen).

    As for the number of monitors... hmm - sounds like you're going to have to do something fancy with Win32::API - good luck :)

       larryk                                          
    perl -le "s,,reverse killer,e,y,rifle,lycra,,print"
    
Re: Determine screen resolution (on Windows)
by Ieronim (Friar) on Jul 12, 2006 at 18:07 UTC
    Win32::GUI can do this work for you :) The slightly modified code from its documentation:
    #!/usr/bin/perl use warnings; use strict; use Win32::GUI; my $desk = Win32::GUI::GetDesktopWindow(); my $dw = Win32::GUI::Width($desk); my $dh = Win32::GUI::Height($desk); print "${dw}x$dh\n";
    Getting the size of all monitors can be difficult, as the desktop can be stretched or duplicated, and you cannot determine this easily :)
Re: Determine screen resolution (on Windows)
by zentara (Archbishop) on Jul 13, 2006 at 15:34 UTC
    Well, I couldn't let this go without giving a simple Tk solution. It works on linux, and probably should work on Windows.
    #!/usr/bin/perl use Tk; my $mw=tkinit; $mw->withdraw; $height = $mw->screenheight; $width = $mw->screenwidth; $depth = $mw->screendepth; print "height->$height width->$width depth->$depth\n"; Tk::exit; MainLoop;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://560727]
Approved by socketdave
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-24 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found