GetBkColor

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having trouble getting the background color of a control in another app.
I use FindWindow() to get the handle of a button on a form. This works fine
because I can successfully send a mouse click to the button (via Sendmessage).

When trying to get the background color I am using the .NET Graphics class:

Color color = Color.Empty;
Graphics graphics = Graphics.FromHwnd(hWnd);
IntPtr hDC = graphics.GetHdc();
color = ColorTranslator.FromWin32(GetBkColor(hDC));
graphics.ReleaseHdc();

For some reason this always returns 'White" rather than the background color
of the control. I have also tried using straight GDI32 and USER32 calls:

IntPtr hDC = GetDC(this.HWnd);
int bkColor = GetBkColor(hDC);
ReleaseDC(this.HWnd, hDC);
color = ColorTranslator.FromWin32(bkColor);

and this also seems to return "White". Any help is appreciated.

Matt
 
Hi Matt,

That is because when you create the device context object (HDC) in the
begining its background color is initilized with whatever is set in the
windows' class (WNDCLASS structure). By default it is white. Because
nowadays applications and controls are build using some framework (I don't
believe anybody writes programs using API only) those frameworks share the
same WNDCLASS they don't really care of what is set in the WNDCLASS. Rather
on the WM_PAINT the frameowrk does some preparation of the HDC and sets the
desired background color. That's why what you get using your code is the
default white background, which no one actually uses.
 
Back
Top