[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

How to determine color of a button programmatically?

I have a handle to a button. How do I get color of a botton?
0
petlev
Asked:
petlev
  • 5
  • 3
  • 2
  • +1
1 Solution
 
jkrCommented:
Assuming that you are talking Win32 here:

HDC hDC = GetDC ( hButton);

COLORREF cr = GetBkColor ( hDC);

ReleaseDC ( hDC);
0
 
jkrCommented:
BTW: You can retrieve the color information about any point of the screen using 'GetPixel()'
0
 
fl0ydCommented:
One thing to add: Do you want the color of a specific button (since you explicitly stated having a handle, one would have to assume so) or the color of buttons in general. If the latter is the case, you can use:
COLORREF crBtn = GetSysColor( COLOR_BTNFACE );
or if you want to use it as a brush:
HBRUSH hbrBtn = GetSysColorBrush( COLOR_BTNFACE );
Since this is a system brush you won't have to call DeleteObject( hbrBtn ); when you're done with it.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ZoppoCommented:
Addition to jkr's suggestion: If maybe the button's color is
modified by the app you may need to send a WM_CTLCOLORBTN to
the button's parent before the GetBkColor().

0
 
petlevAuthor Commented:
By using GetBkColor() I get same result no matter what background color is.
0
 
jkrCommented:
Thanks, but why did you accept my comment as an answer and graded a "C" then? If it did not help, it is not an answer...
0
 
petlevAuthor Commented:
jkr,
I accepted your comment as an answer because 'GetPixel()' function seems to be the only way to get a background color in my case. I gave you "C" because you did not offer any code.
Thanks!
0
 
fl0ydCommented:
WTF?!? How much more code do you need?!? And where exactly does it say so in your question, anyway? Sorry, I just got a bit angry and had to get it out.

.f
0
 
jkrCommented:
>>I gave you "C" because you did not offer any code

Err, what exacly is unclear about a function that works in a style of

int n = multiply ( 1, 2);

?

OK, so here is your code sample.

COLORREF cr;
int nXPos;
int nYPos;
HDC hdc;

// initialize parameters

cr = GetPixel ( hdc, nXPos, nYPos);
0
 
petlevAuthor Commented:
I'm sorry if anybody is a bit angry. I am not very familiar with C++; I was not sure how to use it.
0
 
jkrCommented:
Not angry - just irritated :o)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now