Win32 :: GuiTest :: FindWindowLikeを使用して、ブラウザに関連付けられているウィンドウハンドルを検索し、次のように指定できますscreenshot
。
#!/usr/bin/env perl
use strict; use warnings;
use Const::Fast;
use Imager;
use Imager::Screenshot qw( screenshot );
use Win32::GuiTest qw( FindWindowLike SetForegroundWindow );
const my $TYPE => 'bmp';
my @windows = FindWindowLike(0,
'(?:Mozilla Firefox)|(?:Internet Explorer)|(?:Opera)'
);
for my $hwnd (@windows) {
warn "$hwnd\n";
SetForegroundWindow $hwnd;
sleep 1;
my $img = screenshot(hwnd => $hwnd, decor => 1);
die Imager->errstr unless $img;
$img->write(file => "$hwnd.$TYPE", type => $TYPE)
or die $img->errstr;
}
上記のコードは、IEウィンドウ全体と現在のタブを保持する子ウィンドウに対して別々のスクリーンショットを撮ります。トップレベルのIEウィンドウのみに関心がある場合は、my @windows = FindWindowLike(0, 'Internet Explorer', '^IEFrame');
さらに、を使用して「InternetExplorer.Application」ウィンドウを開いた場合は、オブジェクトのTop、Height、およびWidthプロパティにWin32::OLE
アクセスして、オブジェクトの場所と領域を確認できます。さらに、HWNDを取得して、フォアグラウンドウィンドウとして設定できます。
#!/usr/bin/env perl
use strict; use warnings;
use Const::Fast;
use Imager;
use Imager::Screenshot qw( screenshot );
use Win32::GuiTest qw( SetForegroundWindow );
use Win32::OLE;
$Win32::OLE::Warn = 3;
const my $TYPE => 'bmp';
const my $READYSTATE_COMPLETE => 4;
my $browser = Win32::OLE->new("InternetExplorer.Application");
$browser->Navigate('http://www.example.com/');
sleep 1 while $browser->{ReadyState} != $READYSTATE_COMPLETE;
$browser->{Visible} = 1;
my $hwnd = $browser->{HWND};
SetForegroundWindow $hwnd;
sleep 1;
my $img = screenshot(hwnd => $hwnd, decor => 1) or die Imager->errstr;
my $title = $browser->{LocationName};
$browser->Quit;
$title =~ s/[^A-Za-z0-9_-]/-/g;
$img->write(file => "$title.$TYPE", type => $TYPE) or die $img->errstr;
または、OLEイベントを使用します。
#!/usr/bin/env perl
use strict; use warnings;
use feature 'say';
use Const::Fast;
use Imager;
use Imager::Screenshot qw( screenshot );
use Win32::GuiTest qw( SetForegroundWindow );
use Win32::OLE qw(EVENTS valof);
$Win32::OLE::Warn = 3;
const my $TYPE => 'bmp';
const my $READYSTATE_COMPLETE => 4;
my ($URL) = @ARGV;
die "Need URL\n" unless defined $URL;
my $browser = Win32::OLE->new(
"InternetExplorer.Application", sub { $_[0]->Quit }
);
Win32::OLE->WithEvents($browser, \&Event, 'DWebBrowserEvents2');
$browser->{Visible} = 1;
$browser->Navigate2($URL);
Win32::OLE->MessageLoop;
Win32::OLE->SpinMessageLoop;
$browser->Quit;
sleep 3;
sub Event {
my ($browser, $event, @argv) = @_;
say $event;
if ($event eq 'DocumentComplete') {
$browser->{ReadyState} == $READYSTATE_COMPLETE
or return;
my $hwnd = $browser->{HWND};
SetForegroundWindow $hwnd;
my $img = screenshot(hwnd => $hwnd, decor => 1)
or die Imager->errstr;
my $url = valof( $argv[1] );
$url =~ s{^https?://}{};
$url =~ s{[^A-Za-z0-9_-]}{-}g;
$img->write(file => "$url.$TYPE", type => $TYPE)
or die $img->errstr;
Win32::OLE->QuitMessageLoop;
}
return;
}