Solved

Get screen Size

Posted on 2002-05-04
3
636 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

11 Experts available now in Live!

Get 1:1 Help Now