sagar
sagar

Reputation: 97

How to change the foreground colour (ie text or caption) of a push button in MFC/VC++ dialog application

Am doing a calculator programme in vc++/MFC dialog application. Thier, i want to change the foreground and background colour of a push button in dialog. I have no idea, how to change. Please suggests me with relevent code or example if any body have idea.

basu_sagar

Upvotes: 1

Views: 3008

Answers (2)

sergiol
sergiol

Reputation: 4337

You can use a CMFCButton. Although you can directly say in your resources file a button is of this type, I do not recommend it, because it adds an unmaintainable hexadecimal piece of text on the rc file. And if you use several rc files, one for each language, it's really devilish!

So lets go. In your form class, declare a member

CMFCButton m_button1;

The DoDataExchange should look like:

void MyDialog::DoDataExchange(CDataExchange* pDX)
{
    __super::DoDataExchange(pDX);

    DDX_Control(pDX, IDC_BUTTON1, m_button1);

    // ...
}

Then the OnInitDialog should be something like:

BOOL CMyDialog::OnInitDialog()
{
    if(!__super::OnInitDialog())
         return FALSE;

    m_button1.SetFaceColor(RGB(0,0,255));
    m_button1.SetTextColor(RGB(0,255,0));
    m_button1.SetHotTextColor(RGB(255,0,0));

    return TRUE;
}

The code I posted will draw a blue button, with green text, and when cursor hovers the button, its text will turn red.

Upvotes: 1

Bob Moore
Bob Moore

Reputation: 6894

There's no easy way to do this in a classical VC/MFC application, button colours are always system-defined. You either have to use a custom control, or create an owner-draw button. Handling WM_CTLCOLOR and returning a different brush doesn't work for buttons.

Edit:

This is an example replacement button control someone has built to solve this problem by encapsulating the owner-draw code into a class.

Upvotes: 1

Related Questions