in reply to Re^2: Getting Screenresolution using ONLY Win32::GUI package
in thread Getting Screenresolution using ONLY Win32::GUI package

How do you figure george?

2.4 Centering Windows and Text

To center the main window on the screen, we need to know the screen size. We get this using the desktop window. You can get the handle of the desktop window using Win32::GUI::GetDesktopWindow(). One point to note is that window handles are not Win32::GUI::Window objects, so they cannot be used to call Win32::GUI methods like Height(). However, these methods are overloaded so that they can be called directly (as Win32::GUI::Height()) with a window handle as an extra first parameter. See the code below for an example.

The rest of the work is just arithmetic. To reposition the main window, use the Move() method.

        # Assume we have the main window size in ($w, $h) as before
        $desk = Win32::GUI::GetDesktopWindow();
        $dw = Win32::GUI::Width($desk);
        $dh = Win32::GUI::Height($desk);
        $x = ($dw - $w) / 2;
        $y = ($dh - $h) / 2;
        $main->Move($x, $y);

Now, we will look at centering the label in the window (you will notice that if you increase the size of the window, the text stays at the top left of the window).

The process is similar to the calculations for centering the window above. There are two main differences. First of all, the label needs to be recentred every time the window changes size - so we need to do the calculations in a Resize event handler. The second difference (which in theory applies to the window as well, but which we ignored above), is that we need to watch out for the case where the window is too small to contain the label. Just to make things interesting, we'll stop the main window getting that small, by resizing it in the event handler if that is about to happen. As the width could be too small while the height is OK, we have to reset the width and height individually. We can do this using variations on the Width() and Height() methods, with a single parameter which specifies the value to set.

Replies are listed 'Best First'.
Re^4: Getting Screenresolution using ONLY Win32::GUI package
by Ace128 (Hermit) on Jun 15, 2005 at 15:01 UTC
    Thanks!