You forgot the call to ReleaseDC(). You might just have created a huge memory leak. And what on earth are you trying to pass on as a hWnd? That's the ID (window handle) of the control/window you're trying to access, so try to get a valid value. If you want to access the desktop as a window, you can use the GetDesktopWindow() API call, which will return the hWnd of the mother of all windows, the desktop. You can proceed from there. These are the additional/changed code snippets:
#Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long,
+ByVal hDC As Long) As Long
Win32::API->Import(
'user32', 'long ReleaseDC(long hWnd, long hDC)',
);
#Private Declare Function GetDesktopWindow Lib "user32" () As Long
Win32::API->Import(
'user32', 'long GetDesktopWindow()',
);
sub PixelColor {
my ($hwnd, $x, $y) = @_;
my $dc = GetDC($hwnd);
my $rgb = GetPixel($dc, $x, $y);
ReleaseDC($hwnd, $dc);
return $rgb < 0 ? undef: sprintf "%06x", $rgb;
}
print PixelColor(GetDesktopWindow(), 100, 100);
Well, that's the theory. In practice I always get "ffffff" undef back, which means this pixel can't be read — likely because the pixel data isn't buffered. Oh well. Perhaps it works better with a hWnd of an actual window...
p.s. This hWnd should be a property of your control or canvas, or whatever terminology you might be used to.
p.p.s. For people who feel like trying this out, make sure you have at least version 0.40 of Win32::API, or you'll get a deep recursion error on the Import method — something to do with AUTOLOAD, because that method doesn't exist. Needless to say, it won't work, either.
| [reply] [d/l] |
HDC GetDC(<br />
HWND hWnd // handle to window<br />
);
<
Parameters
hWnd
[in] Handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire screen.
I missed the part about ReleaseDC so thanks for the heads up. I suspect that you are getting undef because of your call to GetDesktopWindow.
Here is the code I used to test with the ReleaseDC and check for negative numbers added in.:
#!/usr/bin/perl
use strict;
use warnings;
use Win32::API;
Win32::API::Struct->typedef( POINT => qw(
LONG x;
LONG y;
) );
Win32::API->Import( 'user32' => 'BOOL GetCursorPos(LPPOINT pt)' );
Win32::API->Import( 'user32' => 'long GetDC(long hWnd)' );
Win32::API->Import( 'gdi32' => 'long GetPixel(long hWnd, long x, long
+ y)' );
Win32::API->Import( 'user32' => 'long ReleaseDC(long hWnd, long hDC)'
+);
sub PixelColor {
# if $hwnd is null or '', act on the entire screen
my ($hwnd, $x, $y) = @_;
my $dc = GetDC($hwnd);
my $rgb = GetPixel($dc, $x, $y);
ReleaseDC($hwnd, $dc);
return $rgb < 0 ? undef: sprintf "%06x", $rgb;
}
my $pt = Win32::API::Struct->new( 'POINT' );
while (1) {
GetCursorPos($pt) or die "GetCursorPos failed: $^E";
print "Cursor is at: $pt->{x}, $pt->{y}\n";
print "The pixel color is: ", PixelColor('',$pt->{x}, $pt->{y}), "\n
+";
sleep(2);
}
Updated: Added code and suspicion of cause of bart's problem with the code, so go back and reread this entire post. :-) | [reply] [d/l] [select] |