Solved

Get screen Size

Posted on 2002-05-04
3
652 Views
Last Modified: 2008-02-01
I am evaluating the use Win32::GUI module.
Does anyone know how can I get the size of the Screen (the resolution) in order to position my new window in the center of the screen?

Some example would be appriciated.

thanks.
0
Comment
Question by:sstouk
3 Comments
 
LVL 1

Expert Comment

by:baltman
ID: 6989911
I think you could best do so in JavaScript...
With JavaScript you can get the screensize and there are also scripts available that will center the new window, regardless of the screen size. JavaScript can also very easily be programmed in your Perl script.

Use the following JavaScript code to open a new window centered on the screen, regardless of the screensize:

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos)
     {
     if(pos=="random")
          {
          LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
          }
     if(pos=="center")
          {
          LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;
          }
     else if((pos!="center" && pos!="random") || pos==null)
          {
          LeftPosition=0;TopPosition=20
          }

     settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
     win=window.open(mypage,myname,settings);
     }

// -->
</SCRIPT>

If you want more scripts for popup windows, have a look at http://www.dynamicdrive.com/dynamicindex8/popwin.htm

Hope this helps...

BA
0
 
LVL 10

Accepted Solution

by:
rj2 earned 50 total points
ID: 6990268
The tutorial on Win::GUI has an example demonstrating this.

http://search.cpan.org/doc/ACALPINI/Win32-GUI-0.0.558/docs/tut/guitut1.html#Centring_the_window_and_the_text

However, when I try this on my computer (Win2K, ActiveState Perl 5.6.1 build 631) the examples provided there crashes. This suggests that the module Win32::GUI is not yet ready for primetime.

I suggest you check out Tk instead. It has been around for a while, and is probably more stable than Win32::GUI. It also has the advantage of being platform independent.
Here is a link to the Perl/Tk FAQ, this might help you getting started with Perl/Tk.
http://w4.lns.cornell.edu/~pvhp/ptk/ptkTOC.html
Below is an example of centering a window with Perl/Tk.


#!/usr/bin/perl
use Tk;
my $m = new MainWindow;
CenterWindow($m,200,200);
MainLoop;


####################################################
sub CenterWindow {
####################################################
# Args: (0) window to center
#       (1) [optional] desired width
#       (2) [optional] desired height
#
# Returns: *nothing*
####################################################
    my($window, $width, $height) = @_;

    $window->idletasks;
    $width  = $window->reqwidth  unless $width;
    $height = $window->reqheight unless $height;
    my $x = int(($window->screenwidth  / 2) - ($width  / 2));
    my $y = int(($window->screenheight / 2) - ($height / 2));
    $window->geometry($width . "x" . $height . "+" . $x . "+" . $y);
}

0
 
LVL 6

Author Comment

by:sstouk
ID: 6991662
You gave me a link to the page, which had the answer I need.

I tested TK and found out that you can create nice Windows with it, but they will look like an emulation of Unix Desktop on Windows platform.
I want a professionally looking Windows GUI Application, which Win32::Gui perfectly provides with, because it accesses the native Windows dlls for doing it.
That's why I stick with Win32-Gui and am happy with.
All the basic controls are there, enough for working with.

I am thankful you pointed out the link where I got the answer and since I am using version 0.665 of Win32::GUI this function might had been fixed and worked perfectly for me. I simply did not know it.

Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now