Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   How to do this... (http://www.programmingforums.org/showthread.php?t=14374)

kipale Nov 8th, 2007 3:06 PM

How to do this...
 
Hi, I want to write a program that reads a pixel on the screen and then does A if the pixel is of certain color and B if it's not. So the change of the color of the pixel (x,y) on my screen can trigger things. Can this be done easily? I'm a newbie programmer so I don't really have any idea of where to start. I just really need a program that can do this. Also if you know that it can be done more easily with some other language than C++ then feel free to tell me because I can always learn the language enough to write the effect-part.

Wizard1988 Nov 8th, 2007 3:11 PM

Re: How to do this...
 
This is in C# but you should be able to use the information contained in the article to write a similar program in C++

http://sharpinsights.wordpress.com/2...r-under-mouse/

InfiNate Nov 8th, 2007 3:23 PM

Re: How to do this...
 
I recently did this in C#, I had to use a pinvoke to use some windows methods.

The only difficulty's i ran into was to read the pixels that were 'outside' of my current program. I cant remember exactly how I did it, ( i think there was a default handle to use ).

I will post my code tonight when I have access to it.

Wizard1988 Nov 8th, 2007 3:27 PM

Re: How to do this...
 
Were you using the DC of the application window?? Try getting the DC of the desktop window instead :)

andro Nov 8th, 2007 4:33 PM

Re: How to do this...
 
java.awt.Robot

InfiNate Nov 8th, 2007 5:08 PM

Re: How to do this...
 
Quote:

Originally Posted by andro (Post 136507)

It's really simple to do this in java. All you need to do is call getPixelColor(int,int) and it returns the color.

It's slightly more complicated to do it in c++/c# because of the window handle stuff.

InfiNate Nov 8th, 2007 6:47 PM

Re: How to do this...
 
Here it is in c#, should be easy to convert it to c++.

:

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

class ScreenReader
{
        #region Imported Functions
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateDC(string strDriver, string strDevice,
                                            string strOutput, IntPtr pData);
        [DllImport("gdi32.dll")]
        public static extern bool DeleteDC(IntPtr hdc);

        [DllImport("gdi32.dll")]
        public static extern int GetPixel(IntPtr hdc, int x, int y);
        #endregion

        public static Color getColorAt(int x, int y)
        {
            IntPtr hdcScreen = CreateDC("Display", null, null, IntPtr.Zero);
            Color clr = ColorTranslator.FromWin32(GetPixel(hdcScreen, x, y));
            DeleteDC(hdcScreen);
            return clr;
        }
}


kipale Nov 9th, 2007 1:28 AM

Re: How to do this...
 
Thank you, everyone. Great help :) I'm gonna try some of this later today or tomorrow, I'm sure I'll get something done.

Wizard1988 Nov 9th, 2007 1:30 AM

Re: How to do this...
 
Is it a good idea to keep keep on recreating hdcScreen each time you call getColorAt ?

InfiNate Nov 9th, 2007 2:50 AM

Re: How to do this...
 
Quote:

Originally Posted by Wizard1988 (Post 136540)
Is it a good idea to keep keep on recreating hdcScreen each time you call getColorAt ?

Probably not. In my actual code i used theres another method where I passed in a hdcScreen for that reason. I suppose I could have just made a member variable for the class.


All times are GMT -5. The time now is 3:17 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC