Wednesday, June 27, 2012

On Windows, how can I maximize a window created using Win32::OLE?

To maximize a window, you need to send a WM_SYSCOMMAND message to its window handle. If the window you created does not provide an accessor for finding out its window handle, you can use Win32::GuiTest::FindWindowLike to find it. In the example below, I use Internet Explorer which does provide its window handle via the HWND property.

Once you have the window handle, you use Win32::GuiTest::SendMessage with SC_MAXIMIZE as the wParam:

#!/usr/bin/env perl

use strict; use warnings;
use feature 'say';

use Const::Fast;
use Win32::GuiTest qw( SetForegroundWindow SendMessage);
use Win32::OLE;
$Win32::OLE::Warn = 3;

const my %SC => (
    MAXIMIZE => 0xF030,
    RESTORE => 0xF120,
    MINIMIZE => 0xF020,
);

const my $WM_SYSCOMMAND => 0x0112;
const my $READYSTATE_COMPLETE => 4;

my $browser = Win32::OLE->new(
    "InternetExplorer.Application", sub { $_[0]->Quit }
);

$browser->{Visible} = 1;
$browser->Navigate2('about:blank');

my $hwnd = $browser->{HWND};

SetForegroundWindow $hwnd;

sleep 1 until $browser->{ReadyState} == $READYSTATE_COMPLETE;

for my $cmd (qw(MAXIMIZE RESTORE MINIMIZE RESTORE)) {
    SendMessage($hwnd, $WM_SYSCOMMAND, $SC{$cmd}, 0);
    sleep 1;
}

$browser->Quit;

No comments:

Post a Comment