Click to See Complete Forum and Search --> : Eyedrop Color Picker help needed.


wolwil
February 15th, 2009, 02:09 PM
Could anyone point me in the right direction on an easy way to pick a color off the screen and return an RGB value for it. I made a color pallet using and now I want to know how I can mouse over the color and have it give me a value like (255,0,0) for red and so on.

Thanks.

I am using Visual C++ 2008 Express. I found this code here on the forums but I cant get it to work.


CDC *dc = GetDC();
COLORREF rgb = dc->GetPixel();
this->lblRGBNumber->Text = rgb;


Here are the errors I am getting in reference to the winuser.h:

1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(46) : error C2146: syntax error : missing ';' before identifier 'HDWP'
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(46) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(46) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(47) : error C2146: syntax error : missing ';' before identifier 'MENUTEMPLATEA'
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(47) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(47) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(54) : error C2146: syntax error : missing ';' before identifier 'LPMENUTEMPLATEA'
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(54) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(54) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(62) : error C2065: 'CALLBACK' : undeclared identifier
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(62) : error C2065: 'WNDPROC' : undeclared identifier
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(62) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(62) : fatal error C1903: unable to recover from previous error(s); stopping compilation




UPDATE:
Now I tried this:

#include <windows.h>
#include <winuser.h>
#include <fstream>



POINT pos;
GetCursorPos (&pos);

HDC hScreenDC = ::GetDC (NULL);
COLORREF rgb = ::GetPixel (hScreenDC, pos.x, pos.y);
::ReleaseDC (NULL, hScreenDC);

UINT red = GetRValue (rgb);
UINT green = GetGValue (rgb);
UINT blue = GetBValue (rgb);

char RedBuf[16];
sprintf(RedBuf, "%u", red);
String^ Red1 = Convert::ToString(RedBuf);
this->lblRGBNumber->Text = Red1;


Now I get this Error:

1>Linking...
1>Project.obj : error LNK2028: unresolved token (0A000018) "extern "C" int __stdcall ReleaseDC(struct HWND__ *,struct HDC__ *)" (?ReleaseDC@@$$J18YGHPAUHWND__@@PAUHDC__@@@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Project.obj : error LNK2028: unresolved token (0A000019) "extern "C" unsigned long __stdcall GetPixel(struct HDC__ *,int,int)" (?GetPixel@@$$J212YGKPAUHDC__@@HH@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Project.obj : error LNK2028: unresolved token (0A00001A) "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)" (?GetDC@@$$J14YGPAUHDC__@@PAUHWND__@@@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Project.obj : error LNK2028: unresolved token (0A00001B) "extern "C" int __stdcall GetCursorPos(struct tagPOINT *)" (?GetCursorPos@@$$J14YGHPAUtagPOINT@@@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Project.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall ReleaseDC(struct HWND__ *,struct HDC__ *)" (?ReleaseDC@@$$J18YGHPAUHWND__@@PAUHDC__@@@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Project.obj : error LNK2019: unresolved external symbol "extern "C" unsigned long __stdcall GetPixel(struct HDC__ *,int,int)" (?GetPixel@@$$J212YGKPAUHDC__@@HH@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Project.obj : error LNK2019: unresolved external symbol "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)" (?GetDC@@$$J14YGPAUHDC__@@PAUHWND__@@@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>Project.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall GetCursorPos(struct tagPOINT *)" (?GetCursorPos@@$$J14YGHPAUtagPOINT@@@Z) referenced in function "private: void __clrcall Project::Form1::Click_Me(class System::Object ^,class System::EventArgs ^)" (?Click_Me@Form1@Project@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
1>C:\Users\User\Documents\Visual Studio 2008\Projects\Project\Project\Debug\Project.exe : fatal error LNK1120: 8 unresolved externals

STLDude
February 15th, 2009, 02:33 PM
You are mixing C++ and Managed C++ (or as it know now C++/CLI)
String^ Red1 = Convert::ToString(RedBuf);
As far as how to fix it there is enough info on the web for you to find it.

wolwil
February 15th, 2009, 02:54 PM
Unfortunalely I have no Idea what managed vs unmanaged is. Could you explain to me a little more about it. I am not a programmer by trade and this is just a hobby of mine. And I have been at this for so long my eye has been twitching for 2 days now lol.

Also this code works:

String^ Red1 = Convert::ToString(RedBuf);


This is what is causing the problem:

HDC hScreenDC = ::GetDC (NULL);
COLORREF rgb = ::GetPixel (hScreenDC, pos.x, pos.y);
::ReleaseDC (NULL, hScreenDC);


Its almost as if VC++ Express can not find the library for it. I found something about:

#using <mscorlib.dll>


and that does nothing

STLDude
February 15th, 2009, 03:16 PM
I'm truly disappointed in you efforts here. Have you even tried to look it up on the web for it as I mentioned in my post. Here (http://en.wikipedia.org/wiki/C%2B%2B/CLI) it's a good start.

As far as your errors, I already told you, you are mixing C++/CLI and standard C++, so if you at least read the link I provided, it should give you understanding of those concepts.


This is C++/CLI code
String^ Red1 = Convert::ToString(RedBuf);
#using <mscorlib.dll>

This is C++:
HDC hScreenDC = ::GetDC (NULL);
COLORREF rgb = ::GetPixel (hScreenDC, pos.x, pos.y);
::ReleaseDC (NULL, hScreenDC);

wolwil
February 15th, 2009, 03:50 PM
When I take everything out of the mix and just try to compile this code I get the same errors.

#include <windows.h>

int main(void)
{
POINT pos;
GetCursorPos (&pos);

HDC hScreenDC = ::GetDC(NULL);
COLORREF rgb = ::GetPixel(hScreenDC, pos.x, pos.y);
::ReleaseDC(NULL, hScreenDC);
}


I have to be missing an include or something if this is just pure C++.

STLDude
February 15th, 2009, 04:14 PM
As everything in life it is a good idead to start in small steps.

I will assume that you use Microsoft Visual Studio (2003, 2005 or 2008), so first create simple project and compile that first by Selecting menu File/New/Project and choose Visual C++/Win32 Console Application. This will create new project with appropriate settings and file.
Now, can you compile that cleanly?

As far as your latest code, you are using win32 GetDC(NULL), GetPixel(...) which need to have actual GUI windows, console app do not provide this functionality. This (http://www.codeguru.com/cpp/w-d/console/article.php/c3957) has decent explanation of difference between GUI and console programming on windows.

wolwil
February 15th, 2009, 04:28 PM
I did just do that sorry about the int main example. Here is what I got:

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

POINT pos;
GetCursorPos (&pos);

HDC hScreenDC = ::GetDC(NULL);
COLORREF rgb = ::GetPixel(hScreenDC, pos.x, pos.y);
::ReleaseDC(NULL, hScreenDC);

}


I put it on the form load event just to see if it would work. I also put a picbox in the form and placed the code in the paint event and still same thing.

I am using VC++ 2008 Express

I also have this included otherwise it does not get to linking:

#include <windows.h>


and just for fun here is everything:

#pragma once
#include <windows.h>

namespace test {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(45, 28);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(220, 193);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->Paint += gcnew System::Windows::Forms::PaintEventHandler(this, &Form1::pictureBox1_Paint);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
this->ResumeLayout(false);

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {


}
private: System::Void pictureBox1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) {

POINT pos;
GetCursorPos (&pos);

HDC hScreenDC = GetDC(NULL);
COLORREF rgb = GetPixel(hScreenDC, pos.x, pos.y);
ReleaseDC(NULL, hScreenDC);
}
};
}

STLDude
February 15th, 2009, 05:20 PM
I don't understand why you are not paying attention. You are mixing TWO different languages. Pick one (C++ or CLI/C++) or in your case you should use C# and then proceed.

This is C++, you need to look it up exact syntax and what objects to use with CLI/C++
POINT pos;
GetCursorPos (&pos);

HDC hScreenDC = ::GetDC(NULL);
COLORREF rgb = ::GetPixel(hScreenDC, pos.x, pos.y);
::ReleaseDC(NULL, hScreenDC);


Links to both versions of GetPixel:
CLI/C++ (http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.getpixel.aspx)
Win32 (C) (http://msdn.microsoft.com/en-us/library/dd144909(VS.85).aspx)

Read both docs.

wolwil
February 15th, 2009, 06:49 PM
OK back to my original question because I have spent way to much time on this and I am going no where and the fun is over.

HOW CAN I GET THE COLOR OFF OF A PICTURE BOX AND SHOW THE COLOR VALUE IN A LABEL USING C++.

I am not a student nor am I a programmer. This is a hobby project that I would much like to finish sometime this century. So please do not sit on the answers and waste more of my time by trying to teach me something I will never understand. Once I see the code I will understand it.

STLDude
February 15th, 2009, 07:02 PM
...So please do not sit on the answers and waste more of my time by trying to teach me something I will never understand.
Well, with that attitude you are wasting my time, so somebody else will need to help you.

Once I see the code I will understand it.
If you bothered to read links I provided and do your research you would find allot fo samples.

This is a hobby project that I would much like to finish sometime this century.
If you want to get something fast, start with C#,rather then C++.

wolwil
February 15th, 2009, 07:13 PM
Bro I have been at this for 3 days do you really think I am interested in reading any more or trying any more of my theories on how to make it work? The answer is NO. The reason why I came on the Forum was to get answers not to be tought!

I only come on these Forums after I have exhausted myself trying what I can understand. So now tell me do you really think I am in the mood for your treasure hunt.

Arjay
February 16th, 2009, 01:40 AM
Bro I have been at this for 3 days do you really think I am interested in reading any more or trying any more of my theories on how to make it work? The answer is NO. The reason why I came on the Forum was to get answers not to be tought!

I only come on these Forums after I have exhausted myself trying what I can understand. So now tell me do you really think I am in the mood for your treasure hunt.You've posted this question multiple times in multiple forums. Please don't do that.

To be frank, this program is probably over your head at the present time. You've been posting in the Visual C++ forum, but included managed C++ code. Now you are posting in the Managed C++ forum, but are including [non-managed] C++ code.

You need to start small and understand what project type you want (i.e. C++ or managed C++) and how to create a project.

If you don't understand this basic starting point, then folks here can't help you make the eyedropper project.

Also in the other forum, you were given a link to an example in C++ of a project that is similar to what you need. Sure, it didn't use an eyedropper, but it had code that captured screen colors.

What you need to do is to download the code for that article, compile the code and start stepping through it in a debugger to see how it works.

Once you understand the code, then come back and post here with specific questions on how to display an eyedropper and leverage the code to capture the colors.

Mikla
July 12th, 2009, 08:45 AM
This is so typical of responses I get to questions when I ask them on forums such as this.... "Go research this and read that...." Many of the folks that ask questions are looking for a code snippet to help them out... we are not professional programmers, nor do we want to be. I do appreciate the fact that folks respond, but, as is the case here, it quickly degenerates to frustrated comments.

If this site does not want to cater to hobby coders, than just say so when an account is created... otherwise you need to understand the mindset of those that fall in this category.

I too am looking for the answer to the original question. I am sure that the code to do this is less than all the text that was typed in the responses.

Arjay
July 12th, 2009, 03:37 PM
This is so typical of responses I get to questions when I ask them on forums such as this.... "Go research this and read that...." Many of the folks that ask questions are looking for a code snippet to help them out... we are not professional programmers, nor do we want to be. I do appreciate the fact that folks respond, but, as is the case here, it quickly degenerates to frustrated comments.

If this site does not want to cater to hobby coders, than just say so when an account is created... otherwise you need to understand the mindset of those that fall in this category.

I too am looking for the answer to the original question. I am sure that the code to do this is less than all the text that was typed in the responses.Understand that everyone here volunteers their time to answer posts. We are not paid, we all have other jobs and answer questions here because we enjoy helping.

Most of the time we are quite helpful, but we expect that the person asking a question does a minimum amount of work to try to work through the problem. We expect that when we suggest a direction that the OP will try the suggestion. We also expect that if we ask questions, that the OP will answer them. Remember, we try to help, but you can't help someone if they only keep stating indicating that there is a problem, but can't tell you what the problem is.

We also expect either a minimum level of understanding or at least determination for solving a problem. We are not a tutorial site that can hold someones hand through the complete process. If you need that level of hand holding, then programming may not be for you.

You might be sure that the code to do what the OP was asking required less posting than our replies. I can tell you that this wasn't the case here. What the OP lacked was a basic understanding of the type of project he was creating. He didn't understand the difference between managed C++ and C# or even what Visual Studio project he had created. We can't be expected to explain the difference between these two languages, why you would choose one language over the other, and go one to describe in detail how to create a new project for each language. As I said, we are volunteers.

As far as pointing to someone to research something, please understand that a big part of programming is understanding how to use the resources like msdn or google to find what you are looking for. If we suggest to search msdn or google for "xxxx yyyyy zzz", we aren't being lazy, we are giving you the search terms that will answer your question (and the top 5 hits probably include sample code). How do we know this? Because, we've just searched it and have looked through the results. While we could post the results directly (and we often do), but we are trying to help the person become programming self sufficient by teaching them how to do their own research.

As far as your specific question, it would help to post the code you have so far and what issues you are running into. If you don't have any code, then at least let us know what language you want the code in.

I see you are new here - welcome to the forum.

yo1dog
August 20th, 2009, 02:12 PM
I know this is old but this worked for me. Make sure you have:

#include <windows.h>
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "User32.lib")