kesterkester has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Knowledgeable Monks:

I am trying to write a minesweeper-playing program (a program to play the Windows game for me, and hopefully rack up some great times :) ), using Win32::GuiTest.

After a lot of searching in the Win32 and TK modules, I'm stuck for a way to detect the color of a pixel in a windows application. (I'm trying to detect if there is a mine in the square I've just clicked, or if a colored number has appeared).

Any help or pointers would be much appreciated!

-Kester

Replies are listed 'Best First'.
Re: Detect Pixel Color on WinXP
by Mr. Muskrat (Canon) on Sep 15, 2003 at 15:46 UTC

    I'm trying to expand my knowledge of the Windows API. Why? I'm the curious type and I like a good challenge. So I went ahead and read the docs and came up with this.

    #!/usr/bin/perl use strict; use warnings; use Win32::API; #Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As L +ong Win32::API->Import( 'user32', 'long GetDC(long hWnd)', ); #Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As +Long, ByVal x As Long, ByVal y As Long) As Long Win32::API->Import( 'gdi32', 'long GetPixel(long hdc, long x, long y)', ); sub PixelColor { my ($x, $y) = @_; my $dc = GetDC(''); my $rgb = sprintf "%06x", GetPixel(GetDC(''), $x, $y); return $rgb; } print PixelColor(100, 100), "\n";

    Update: Changed "%x" to "%06x".

      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.

        I'm just following the MS documentation on Get DC (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_4esj.asp):
        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. :-)

Re: Detect Pixel Color on WinXP
by hardburn (Abbot) on Sep 15, 2003 at 15:52 UTC

    This isn't related directly to Perl, but I was researching Minesweeper with the idea of creating a program like that, and I discovered that someone proved it to be an NP-complete problem (external link). If you manage to make an efficient algorithm for it, you'll also be able to break a whole lot of current encryption algorithms.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      An /efficient/ algorithm isn't needed, just one that is good enough for the available hardware.

      There are a few programs like this already out there. They are able to solve the "Expert" mode in 1 or 2 seconds most of the time.

Re: Detect Pixel Color on WinXP
by tachyon (Chancellor) on Sep 15, 2003 at 15:05 UTC

    If you do a Google for 'GetPixel VB' you will find plenty of VB to do it. Then all you have to do is convert it to Perl :-)

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Detect Pixel Color on WinXP
by kesterkester (Hermit) on Sep 16, 2003 at 12:56 UTC
    Thanks for the help, all! As always, this site is an excellent source of valuable information!