Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 8th, 2007, 2:06 PM   #1
kipale
Newbie
 
Join Date: Nov 2007
Posts: 2
Rep Power: 0 kipale is on a distinguished road
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.
kipale is offline   Reply With Quote
Old Nov 8th, 2007, 2:11 PM   #2
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
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/
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Nov 8th, 2007, 2:23 PM   #3
InfiNate
Newbie
 
Join Date: Nov 2007
Location: Waterloo, ON
Posts: 20
Rep Power: 0 InfiNate is on a distinguished road
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.
InfiNate is offline   Reply With Quote
Old Nov 8th, 2007, 2:27 PM   #4
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: How to do this...

Were you using the DC of the application window?? Try getting the DC of the desktop window instead
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Nov 8th, 2007, 3:33 PM   #5
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 294
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Re: How to do this...

java.awt.Robot
__________________
http://www.kevinherron.com/
andro is offline   Reply With Quote
Old Nov 8th, 2007, 4:08 PM   #6
InfiNate
Newbie
 
Join Date: Nov 2007
Location: Waterloo, ON
Posts: 20
Rep Power: 0 InfiNate is on a distinguished road
Re: How to do this...

Quote:
Originally Posted by andro View Post
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 is offline   Reply With Quote
Old Nov 8th, 2007, 5:47 PM   #7
InfiNate
Newbie
 
Join Date: Nov 2007
Location: Waterloo, ON
Posts: 20
Rep Power: 0 InfiNate is on a distinguished road
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;
        }
}
InfiNate is offline   Reply With Quote
Old Nov 9th, 2007, 12:28 AM   #8
kipale
Newbie
 
Join Date: Nov 2007
Posts: 2
Rep Power: 0 kipale is on a distinguished road
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.
kipale is offline   Reply With Quote
Old Nov 9th, 2007, 12:30 AM   #9
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
Re: How to do this...

Is it a good idea to keep keep on recreating hdcScreen each time you call getColorAt ?
__________________
JG-Webdesign
Wizard1988 is offline   Reply With Quote
Old Nov 9th, 2007, 1:50 AM   #10
InfiNate
Newbie
 
Join Date: Nov 2007
Location: Waterloo, ON
Posts: 20
Rep Power: 0 InfiNate is on a distinguished road
Re: How to do this...

Quote:
Originally Posted by Wizard1988 View Post
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.
InfiNate is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:24 PM.

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