I need a little bit of help with a program I am making. I have most of the program made except for this part:

procedure Button1Click (Sender: TObject);
begin
   Button1.Click := public int getPixel(int x, int y);
      if (R=R+10) or (G=G+10) or (B=B+10) then
		// Simulate a key press
			keybd_event(VK_NUMPAD4,0xcb,0 , 0);

      // Simulate a key release
			keybd_event(VK_NUMPAD4,0xcb, KEYEVENTF_KEYUP,0);
		else
			null // do nothing (can null be used?)...
end;

I know that this isn't right because I get errors when I try to compile the program (errors related to this). This code is suppose to do something like this:

Click on button 1
When button 1 is clicked search for "pixel x, y"'s color 
If the red, green, or blue value has increased by 10 then
Simulate a keypress
Else
Do nothing

I can't figure out how to get something equivalent to what I just said. Also during the whole "if RGB increased by 10" bit, the color starts out as black (0,0,0) and if any of the RGB values increase by 10 then the key is pressed but do I need to specify that the color changes from black to another color or can Delphi conclude that if there are any changes then click? I also just wanted to add that this is suppose to find the color of a pixel on a swf loaded up in Delphi made web browser. I am very new to Delphi (I used to use scripting based programs instead of actual programming languages). Please help me anyway you can. If you need me to explain something differently please ask me.

What object is getpixel part of?

Also, if you mean the (X, Y) under the cursor, you'll have to use the MouseDown event.

Are you trying to get the color of a pixel in an image you loaded in your program? Or are you trying to get the color of a pixel of another program?

I'm sorry, this whole thing is really wrong. I'm just trying to get the color of a pixel on the screen from another program. I kind of put bits and pieces together from source codes I've found and put them all into that thing. Is there any way for delphi to read a certain pixel and have it do something if it is not a certain color?

Certainly you can, but the way to do it varies depending on where the pixel you want is.

You can get a specific pixel thus:

function GetPixelColor( win: HWND; x, y: integer ): tColor;
  var
    dc: HDC;
  begin
  if win = 0  // entire screen
    then begin
         win := GetDesktopWindow;
         dc  := GetDC( win )
         end
    else dc := GetWindowDC( win );
  result := GetColor( dc, x, y )
  end;

If you know what window you want to get the pixel from, pass it as the first argument.
If you want the pixel from anywhere on the display specify 0 as the first argument.

The x and y arguments are always relative to the target window (or the whole screen if win is zero).

If x or y are invalid the color returned is CLR_INVALID.

Hope this helps.

Wow thank you. I maybe have to come back here and ask you a bit more about this if I can't get it to work. But it looks like you helped me a ton. Thank you. :)

Edit: I do have a couple of questions about this.
1. What is the dc: HDC; thing? I don't understand what that is.
2. Does this win := GetDesktopWindow; get only my desktop or can I make it to get an open window?
3. What is the dc (& DC) thing? dc := GetDC( win )

Sorry for these very "newbie" questions.

1 and 3:
All graphics operations in Windows are done using a "device context", or DC. If you want to draw lines or pictures or images or icons or whatever, you must have a valid DC to do it. Delphi's VCL provides a pretty powerful abstraction over the Win32 GDI ("Graphics Device Interface", which gives you the DC, etc), so you don't usually have to mess with the GDI directly.

2:
Everything in Windows is identified by a "handle". Each window has a handle. The desktop window, which is the window that contains all other windows you'll ever see (or not see), is created and managed by the system, but you can still get its handle and do stuff with it. The desktop window is the parent of all top-level windows.

In Delphi, you can access your own application's windows by using the Handle property of each TForm or the TApplication. You don't typically need to because the VCL abstracts all that stuff away for you.

If you want to find the handle of another application's window(s), you have two choices:

  1. If you know the name and/or class of the target window, you can use the FindWindow() function --so long as there is only one matching window to find.
  2. If you don't know the name or class of the target window(s), you must use the EnumWindows() Win32 function.

Both those functions are for finding top-level windows. Child windows (buttons, panels, textboxes, etc) require the child window versions of those functions (which you can find with the documentation for the others).

Hope this helps. Don't worry too much about understanding all this esoteric stuff at once. The more you dink with it the more sense it makes.

Thank you. :) You've helped me a great deal.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.