Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to change the CButton back color?

443 views
Skip to first unread message

Landon

unread,
Jul 10, 2008, 9:28:02 PM7/10/08
to
I use MFC Visual C++ 4.2.

I want to change a CButton background color to AQUA color.

How to do this?

Thank you very much.

David Ching

unread,
Jul 10, 2008, 10:30:49 PM7/10/08
to
"Landon" <Lan...@discussions.microsoft.com> wrote in message
news:9E936C20-E98E-4751...@microsoft.com...

Derive class

class CMyButton: public CButton
{
};

and override OnEraseBkgnd(). In that function call CDC:: FillSolidRect()
with the AQUA color . The rect should be the one returned by
GetClientRect().

-- David


Landon

unread,
Jul 10, 2008, 11:23:01 PM7/10/08
to
Does deriving the class will keep the CButton NORMAL behaviour? Because I
have tried to derive one class but when I tested the TAB order, and clicking
the button, it BEHAVE different from the NORMAL CButton.

When I clicked it, the flat style is different and when I tried TAB order,
it DID NOT accept the focus.

Does your advice also affect like that?

If yes, how to solve this?

Thank you.

Tom Serface

unread,
Jul 10, 2008, 11:59:11 PM7/10/08
to
Yes, the derived class will keep all the attributes of a normal button so
long as you don't override other functionality. To use it you could just
put in a normal button (onto the dialog) then edit the .h file after the
fact and put in your own button class name instead. That way you could set
the tab order in the dialog easily.

Tom

"Landon" <Lan...@discussions.microsoft.com> wrote in message

news:1485AD87-D82E-42C9...@microsoft.com...

Ajay Kalra

unread,
Jul 11, 2008, 11:08:20 AM7/11/08
to
On Jul 10, 11:23 pm, Landon <Lan...@discussions.microsoft.com> wrote:
> Does deriving the class will keep the CButton NORMAL behaviour?

Yes; unless the derived class changes it.

> Because I have tried to derive one class but when I tested the TAB order, and clicking
> the button, it BEHAVE different from the NORMAL CButton. When I clicked it, the flat style is different and when I tried
> TAB order, it DID NOT accept the focus.

If you are owner drawing it, then you may have some effect. However
behavior of tab key will not change unless you change it in derived
class. I have used the following with good success:

http://www.codeproject.com/KB/buttons/cbuttonst.aspx


--
Ajay

Joseph M. Newcomer

unread,
Jul 11, 2008, 11:40:08 AM7/11/08
to
If your derived class has an OnEraseBkgnd and therefore does a FillSolidRect, this could
be expected to interact with the default OnEraseBkgnd (since you now override it) and
therefore change the appearance of the button in any number of ways. As to accepting the
focus, how did you tell this? If you have a DrawItem for an owner-draw button, it is your
responsbility to draw whatever you need to draw to indicate that focus has been set;
there's no magic here. If you do owner-draw and don't draw a focus rectangle or give
other indication of focus, the button has the focus, but there is no visible manifestation
of it, but that's just an error in the display.

Once you take over the drawing, you and you alone are responsible for what the button
looks like.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

0 new messages