Solved

How to determine color of a button programmatically?

Posted on 2002-07-16
11
218 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
ID: 7157314
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
ID: 7157320
BTW: You can retrieve the color information about any point of the screen using 'GetPixel()'
0
 
LVL 8

Expert Comment

by:fl0yd
ID: 7158292
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 31

Expert Comment

by:Zoppo
ID: 7158839
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
ID: 7194013
By using GetBkColor() I get same result no matter what background color is.
0
 
LVL 86

Expert Comment

by:jkr
ID: 7275593
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
ID: 7277740
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
ID: 7277749
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
ID: 7281093
>>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
ID: 7281251
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
ID: 7282529
Not angry - just irritated :o)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Implementing a ResolveEventHandler in C++ 13 133
multi threaded debug dll option in visual studio 2 108
computer science syllabus 3 83
Path to  STL Map header file 1 58
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

773 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