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.


In reply to Re^3: Getting Screenresolution using ONLY Win32::GUI package by Anonymous Monk
in thread Getting Screenresolution using ONLY Win32::GUI package by Ace128

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.