Environment: MS Visual Studio 2008. (Please transfer this thread to where it belongs.)
Project type: Windows Forms Control Library.
Goal: Get the pixel color from a given process' main window.
Already achieved: I got the desired process's pointer by using

cli::array<Process ^>^ Proc = System::Diagnostics::Process::GetProcessesByName("Notepad");

I believe I got the window handler of the process' main window using:

IntPtr hWnd = Proc[0]->MainWindowHandle;

Do I need the process' hDC(Device Context Handler)? If so, how do I do it. Then How do I get the color of a pixel in a given location?
Thank you in advance!

Recommended Answers

All 6 Replies

You need a handle to a device context since GetPixel needs one as it's first argument.It returns a COLORREF object which consists of the amount of red green and blue that give the pixel it's color.

Ok, but how do I "get" the hDc? I'm tired of searching. The google answers doesnt work on my compiler/version/project.
How do I point out which window/hWND do I want to get its hDC?
What type is the function that gets me the hDC?

Microsoft function names are not very creative (which is a good thing).


Hope this helps.

If you got the window handle you can get the DC inside a WM_PAINT message using HDC BeginPaint(HWND,PAINTSTRUCT) or simply HDC GetDC(HWND)...else i might not get what your question is.

GetDC is my function. How do I include the "Header: Winuser.h (include Windows.h)"?
Is the header the only thing I must include for the GetDC() function to work?
My project begins with

#pragma once

namespace MyProject{

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

	public ref class Form1 : public System::Windows::Forms::Form
			//TODO: Add the constructor code here

I dont even know what "#pragma once" stands for. These Windows Forms are not like the projects I've been studying on.

Thank you very much. You helped me a lot.
From this code:

IntPtr hWnd = Proc[0]->MainWindowHandle;
				 void *handle = hWnd.ToPointer();
				 HWND hand = reinterpret_cast<HWND>(handle);

				 HDC h = GetDC(hand);
				 ReleaseDC(hand, h);
				 h = NULL;

				 HDC myHDC = GetDC(hand);
				 GetPixel(myHDC, 100, 100);

GetPixel() does not take 3 arguments. Whats the model of the GetPixel() that I need?
In the library for Windows Forms I find only GetPixel(POINT point) or GetPixel( int coordinate_1, int coordinate_2 )
You've been very helpful!

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.