all groups > dotnet academic > july 2003 >
You're in the

dotnet academic

group:

Change the color of a button


Change the color of a button Marium A.
7/14/2003 9:11:15 AM
dotnet academic: In my program, I have a button that allows the user to
change the color of an object by bringing up the standard
color selection box. I want this button to reflect the
color selected. How may I change the color of the button?

Re: Change the color of a button Alfred C Thompson II
7/14/2003 7:19:35 PM
OK really you can't do this as easily as you'd like. The color of a button
is a system wide value. Change one button and you change them all. This is
by design to make windows programs look consistatly the same. What you can
do is use another object as a button. In other words you can use a picture
box and have it respond to clicks. What many of my students have done is to
create several images and use them in a picture box. One image for up and
one for down. Change them on the mouse down and up events. You can do all
sorts of variations of this to siimulate buttons that ahve what ever
features you want.

[quoted text, click to view]

Re: Change the color of a button Jay B. Harlow [MVP - Outlook]
7/15/2003 1:59:14 PM
Marium,
To qualify what Alfred & Peter stated.

If you set the Button's FlatStyle property to System, you will not be able
to change the BackColor property of the button, as the System will control
it. Just as Alfred stated.

If you set the Button's FlatStyle to Standard, Flat, or Sunken, you will be
able to change the BackColor property of the Button. Just as Peter stated.

You can change the BackColor with FlatStyle.System, its just the button will
ignore you.

You need to use FlatStyle.System if you want to enable Windows XP themes on
your form, via EnableVisualStyles or manifest file.

Hope this helps
Jay

[quoted text, click to view]

Re: Change the color of a button Peter van der Goes
7/16/2003 7:10:46 AM

[quoted text, click to view]
Thanks, Jay!
It certainly helps me, as I was not aware of this effect of the FlatStyle
property setting.


Change the color of a button Joseph
7/16/2003 11:01:06 PM

..BackColor property set to a new color.
Eg.

butAdd.BackColor = Color.Red;
Thanks,
Joseph.



[quoted text, click to view]
Change the color of a button Marium A.
7/17/2003 10:08:59 AM
Hi, Thank you so much for your answers. I'm using C++. I
guess the controls are a bit different from VB, because
CButton does not have a .BackColor option (or anything
else like it). I'm now using the custom control option to
create a CStatic control, but it seems like there's no
function to specify its color either.

Change the color of a button Andrew Downum
7/17/2003 3:02:45 PM
CButton is an MFC class (the reason that you are getting
confusing answers is that you posted in a .NET forum).

To get a custom-colored button in MFC you will have to
owner-draw it. Basically instead of leaving it up to the
system to draw the button for you, you will do it
yourself. Relax, it's not as hard as it sounds.

Basically you will just need to set the BS_OWNERDRAW style
(do this in your call to Create), and then override
DrawItem. There are a lot of utility methods that are
available to you that will draw whichever parts of the
button that you don't want to override, check out this
example for some pointers in the right direction:
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/wcemfc/htm/cbutton_4.asp

(in the future, MFC questions would be better posted in
the vc.mfc newsgroup)

~ Andrew
[quoted text, click to view]
Re: Change the color of a button Peter van der Goes
7/17/2003 11:01:17 PM

[quoted text, click to view]

I'm glad we got that clarified, and that you've received a good answer from
Andrew. FWIW, it's not a C++ vs. VB difference, but a MFC vs. .NET framework
difference. The procedure I described can be done with equal ease in .NET
VB, C# or C++ (in the .NET 2003 Windows Form project template).

Re: Change the color of a button Marium A.
7/26/2003 1:26:50 PM
Thank you so much! And I will post in MFC in the future.
[quoted text, click to view]
AddThis Social Bookmark Button