Yup, you've got a good one here. I haven't solved the problem by any means but I think I found an important piece of the puzzle.
FindWindowLike is returning a handle to the window and not the window object. The
new method of
Win32::GUI::DC is expecting more.
Here's some code that demonstrates what I think is missing:
Update: You'll have to clode the window by clicking the x in the upper right hand corner before the script will finish. But then again you already knew that.
use Win32::GUI;
use Win32::GUI::DIBitmap;
use Win32::GuiTest qw(FindWindowLike GetWindowText);
my $W = new Win32::GUI::Window (
-title => "Test Window",
-pos => [100, 100],
-size => [400, 400],
-name => "Window"
);
$W->AddButton (
-pos => [100, 100],
-size => [200, 200],
-text => 'tstbtn',
-name => "Button",
-visible => 1
);
$W->Show();
Win32::GUI::Dialog();
my $hdc = Win32::GUI::DC->new($W);
print "hcd is $hdc\n";
print "Window name is '" . GetWindowText($W->{-handle}) . "'\n";
for my $x(keys %{$hdc})
{
print"$x --> $hdc->{$x}\n";
}
print "ERROR: $!\n"
unless my $bmap = Win32::GUI::DIBitmap->newFromDC($hdc);
print "bmap2 is $bmap\n";
my $test = $bmap->SaveToFile("c:/z.bmp");
print "TEST: $test\n";
print "==============================\n";
my @windows = FindWindowLike(0, qr/^search.cpan.org/);
my $hdc2 = Win32::GUI::DC->new($windows[0]);
print "hcd2 is $hdc2\n";
print "Window name is '" . GetWindowText($windows[0]) . "'\n";
for my $x(keys %{$hdc2})
{
print"$x --> $hdc2->{$x}\n";
}
print "HDC2 ERROR: $^E\n"
unless my $bmap2 = Win32::GUI::DIBitmap->newFromDC($hdc2);
print "bmap2 is $bmap2\n";
my $test2 = $bmap2->SaveToFile("c:/z2.bmp");
print "TEST2: $test2\n";
You can see that the window created by perl has many keys and the other window has none. The
CD from the perl window has two keys
-handle and
-window and everything works just fine. The cpan window's
DC has just the
-handle and according to the error I got, it's invalid. Here's the output I got:
W - Button --> Win32::GUI::Button=HASH(0x205c158)
W - -type --> 0
W - -name --> Window
W - -handle --> 4001032
W - -accel --> 0
hcd is Win32::GUI::DC=HASH(0x183f17c)
Window name is 'Test Window'
-window --> 4001032
-handle --> -805233257
bmap2 is Win32::GUI::DIBitmap=SCALAR(0x1fbb988)
TEST: 1
==============================
window[0] = 6490244
Can't call method "SaveToFile" on an undefined value at C:\projects\Pe
+rl-9.pl line 51.
hcd2 is Win32::GUI::DC=HASH(0x1fb8980)
Window name is 'search.cpan.org: Win32::GuiTest - Alternate distributi
+on of Perl GUI Test Utilities. - Microsoft Internet Explorer'
-handle --> 6490244
HDC2 ERROR: The handle is invalid
bmap2 is
I've been looking for a way to return the window object the way
Win32::GUI::DC->new wants it but haven't found it yet.
I know this didn't solve your problem but maybe it help you find the answer.
Chris
Update: I found a little more info.
I looked at GUI.pm and added some debug statements in there to get a better picture of what's going on. From what I can see it only works for a perl generated window. I have tried to make it work for windows from IE, Secure CRT, Outlook, etc. and nothing worked. I tried to cheat it like this:
use Win32::GUI;
use Win32::GUI::DIBitmap;
use Win32::GuiTest qw(FindWindowLike GetWindowText GetClassName);
my @windows = FindWindowLike(0, qr/^Linux/);
print "windows[0] = $windows[0]\n";
my $fakeit = {};
bless($fakeit,GetClassName($windows[0]));
$fakeit->{-handle} = $windows[0];
$fakeit->{-name} = GetWindowText($windows[0]);
print "FAKEIT = " . $fakeit->{-handle} . "\n";
my $hdc3 = Win32::GUI::DC->new($fakeit);
print "hcd3 is $hdc3\n";
print "Window name is '" . GetWindowText($windows[0]) . "'\n";
for my $x(keys %{$hdc3})
{
print"$x --> $hdc3->{$x}\n";
}
print "HDC3 ERROR: $^E\n"
unless my $bmap3 = Win32::GUI::DIBitmap->newFromDC($hdc3);
print "bmap3 is $bmap3\n";
my $test3 = $bmap3->SaveToFile("c:/z3.bmp");
print "TEST3: $test3\n";
And here's the output:
CLASS: Win32::GUI::DC
WINDOW: Win32::GUI::Window=HASH(0x21b8f20)
WINDOW HANDLE: 1576184
W - Button --> Win32::GUI::Button=HASH(0x205bb0c)
W - -type --> 0
W - -name --> Window
W - -handle --> 1576184
W - -accel --> 0
hcd is Win32::GUI::DC=HASH(0x183f17c)
Window name is 'Test Window'
-window --> 1576184
-handle --> -1727981239
bmap2 is Win32::GUI::DIBitmap=SCALAR(0x1fb620c)
TEST: 1
==============================
window[0] = 6490244
CLASS: Win32::GUI::DC
WINDOW:
WINDOW HANDLE:
hcd2 is Win32::GUI::DC=HASH(0x1fba4c0)
handle is
Window name is 'search.cpan.org: Win32::GuiTest - Alternate distributi
+on of Perl GUI Test Utilities. - Microsoft Internet Explorer'
-window -->
-handle --> 402723759
bmap2 is Win32::GUI::DIBitmap=SCALAR(0x1fac690)
TEST2: 1
==============================
windows[0] = 329430
Can't call method "SaveToFile" on an undefined value at C:\projects\Pe
+rl-9.pl line 83.
FAKEIT = 329430
CLASS: Win32::GUI::DC
WINDOW: Van Dyke Technologies - SecureCRT=HASH(0x1fac660)
WINDOW HANDLE: 329430
hcd3 is Win32::GUI::DC=HASH(0x1fa45e0)
Window name is 'LinuxVPN - SecureCRT'
-window --> 329430
-handle --> -2113858809
HDC3 ERROR:
bmap3 is
The only thing that looks out of place is that the window is not
Win32::GUI::Window. I tried to cheat it even more and specify it like this
bless($fakeit,Win32::"GUI::Window"); but that didn't work either.
Good Luck