Solved

How to determine color of a button programmatically?

Posted on 2002-07-16
11
197 Views
Last Modified: 2010-04-01
I have a handle to a button. How do I get color of a botton?
0
Comment
Question by:petlev
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Assuming that you are talking Win32 here:

HDC hDC = GetDC ( hButton);

COLORREF cr = GetBkColor ( hDC);

ReleaseDC ( hDC);
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
Comment Utility
BTW: You can retrieve the color information about any point of the screen using 'GetPixel()'
0
 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
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
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
 

Author Comment

by:petlev
Comment Utility
By using GetBkColor() I get same result no matter what background color is.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
 

Author Comment

by:petlev
Comment Utility
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
 
LVL 8

Expert Comment

by:fl0yd
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>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
 

Author Comment

by:petlev
Comment Utility
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
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Not angry - just irritated :o)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now