Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading color of a pixel outside of a VFP all

Status
Not open for further replies.

dkean4

Programmer
Feb 15, 2015
282
US
Is there a way to read the color of a pixel outside of a VFP app. Let's say that I am in the VFP Command window and I position the mouse at a location outside of the _SCREEN. VFP sits at x,y [200,200] and I issue this command to position the cursor.

Code:
 MOUSE AT _screen.top-100,_screen.left-100 pixels

Can anyone think of a way to read the pixel color at such a Windows screen location? I did something of the sort with an API years ago, but lost the code. The OS API returns a certain number of bytes/doublewords... whatever...

Any help would be appreciated.


Dennis


 
Hi Dennis,

I know that this is possible, because I've seen other applications and utilities that do precisely what you are asking. Unfortunately, I can't tell you how to do it programmatically, but I'm sure there is at least one regular forum member here who can.

That said, do you want to build this ability into an application for other people to use? Or is it something you want to do yourself? If the latter, there is a utility could Color Cop that will meet your need. Once activated, you simply drag the mouse to the pixel in question, and the tool displays the colour code (and optionally copies it to the clipboard), in hex, RGB, or one of several other format.

There are other similar tools available.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike,

Thanks for the quick reply. Very interesting utility "Color Cop". I will play with it for other uses.

I do need this to be a part of an application, though. I have several uses for reading outside pixels, one being for merging a picture with a secondary picture on the fly, without having to open Photoshop or other utilities. .

Another is for image recognition to automate some processes adaptively.


Dennis
 
Well, googling windows api get pixel color I find This needs a HDC, but that's not overly complicated:
Put together:
Code:
Declare Integer GetDC In Win32api Integer HWnd
Declare Integer GetPixel In Win32Api Integer HDC, Integer X, Integer Y
Declare Integer GetCursorPos In WIN32API String @ Point

Local lnDesktopDC, lcPoint, lnMouseX, lnMouseY
lnDesktopDC = GetDC(0)
lcPoint = Space(8)

Do While Lastkey()<>32
   If GetCursorPos(@lcPoint)=1
      lnMouseX = CToBin(Left(lcPoint,4),"4RS")
      lnMouseY = CToBin(Right(lcPoint,4),"4RS")
      lnColor = GetPixel(lnDesktopDC,lnMouseX, lnMouseY)
      _Screen.BackColor = lnColor
   Endif
   DoEvents
Enddo

Move your mouse around - anywhere on the desktop also outside of VFPs _screen and the VFP _screen back color will be set to the color under the mouse. Press SPACE to end the while loop.

Bye, Olaf.
 
About 15 years ago I recall a fellow programmer saw some of us playing online games during breaks. The goal was to build up points and then each day the points would be used to select random winners, the more points, the better your odds. We lamented how strange that a few players listed huge points counts each day. He wandered away and came back later with code he wrote in VFP to let VFP automatically play the games based on the screen colors in the game window, we had fun racking up millions of points each day, running in the background while we worked. We never won with our ridiculously high point counts of course. Quickly the terms were modified to prohibit computer auto-playing.
 
Nice story, I bet you can do that with games like Tetris, where you have just a few colors and a simple grid of pixels is enough to let VFP "see" the game situation and react to it. Or mine sweeper.

Bye, Olaf.
 
Are you yet close to retirement or already retired now, Mike?

I once programmed a Tetris on one day in BASIC on a 386 DOS laptop. I had my math background help me with figuring out matrix multiplication to do the 90° turns of tiles. The difficulty level of this tetris clone was increased by having wait loop with a decreasing number of iterations. It was doing nothing, really an empty loop body, but an upper limit of 1000 was enough to slow the game down to playable speed. If you'd do that today, 1000 would be many magnitudes too low to be a good value to start with.

Doing an automatic Tetris playing is a bit more challenging, though I also often thought I could do such a thing while playing. It's not that hard to do a strategy and it could even play better than oneself, as it doesn't fail one off sometimes, you could even vary strategies for best points or keeping the pile as low as possible. Today you can find source code for such things.

I also once had the idea to make a physically correct tetris, but somebody has dnoe that already:
Well, but that's just about Tetris, maybe you had a more general idea.

Bye, Olaf.
 
Olaf,

Thanks for the great reply. Been very busy. I never saw that API. Of course C++ APIs are so many and I tend to look everywhere else, first. Casting of variables and what not, in C++ confuses me at times... Your example was clear, though.

Anyway, it gave me many ideas. Terrific solution.

Thank you Olaf,


Dennis

 
Are you yet close to retirement or already retired now, Mike?

I decline to answer that. Let's just say that I am not exactly at the start of my career, but that there is still a small amount of power that I am eking out of my declining brain.

Regarding tetris, I did once think of creating a tetris game (the game itself, as opposed to a program that plays the game automatically) as a training exercise in object orientation. I had in mind that there would be a tile class, with properties like colour, position and speed of descent, and with each tile being an instance of the class. I had it all worked out in my head, but needless to say never got round to do anything about it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
As I said I did it in BASIC and that obviously wasn't object oriented. I used a 4,2 array for the active tile and an array for the playfield. Unfortunately, the code is lost with that laptop, I would also need to start from scratch, but it really is not much work to do...

Bye, Olaf.



 
Back in the days of the old 8-bit Atari BASIC and tape recorders I wrote a game to play Mastermind, instead of colors I used digits so the game allowed from 4 to 10 columns.
 
Mike Lewis said:
as a training exercise in object orientation

That, and testing instantiation speed, is one reason VFP ships with Minesweeper. [pipe]

(During Alpha 1 there was a Solitaire implementation too but it never saw the light of day.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top