如何确保使用Perl的Imager :: Screenshot获取特定窗口的屏幕截图?

时间:2012-06-27 05:09:42

标签: windows perl internet-explorer browser

我在我的Perl代码中使用Imager::Screenshot,它确实有效,并获取屏幕截图。

现在,每次浏览器在不同位置打开时,意味着开始x和y位置可能不一样。

有没有办法从浏览器而不是桌面起始位置开始截屏。

如果没有(从编程中脱离主题)有一种方法可以将浏览器设置为仅以完整大小打开,无论它打开什么程序。用户在单击图标时打开,或者由Perl使用Win32::OLE模块打开。

1 个答案:

答案 0 :(得分:6)

您可以使用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');

此外,如果您使用Win32::OLE打开了"InternetExplorer.Application"窗口,则可以访问该对象的TopHeightWidth属性以确定它的位置和面积。此外,您可以获取其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;

或者,using OLE events

#!/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;
}