views:

399

answers:

4

I'm trying to build an owner-drawn check box using CButton, but since I only want to change the text color, I'd like the check-box marks to remain the same.

Is there a command that allows me to retrieve the default check box bitmaps for the platform where the program is running?

(alternatively: how could I change only the text color, preserving the check box marks?)

+1  A: 

Your best strategy would be to override the OnCtlColor handler:

BEGIN_MESSAGE_MAP(CBaseDialog, CDialog)
{
    ON_WM_CTLCOLOR()
}

HBRUSH CXXX:OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hBkgrBrush= CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    pDC->SetTextColor(RGB(255,0,0)); // red
    pDC->SetBkMode   (TRANSPARENT );
    return hBkgrBrush;
}

See http://msdn.microsoft.com/en-us/library/0wwk06hc(VS.80).aspx|

Lior Kogan
Yeah, but that doesn't work with CButtons... Although I am able to set the background color, the SetTextColor command doesn't work.
djeidot
It works when you implement it in the containing dialog level.
Lior Kogan
It is not working for me. I'm using CMFCVisualManager from MFC Feature Pack to set the application look, maybe that's why.
djeidot
Found out it works on VS2003 but not on VS2008.
djeidot
+1  A: 

If you only want to change the text color, implement a handler for OnCtlColor in your containing dialog. Like this:

HBRUSH CDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    if(pWnd->GetDlgCtrlID() == IDC_CHECK_BOX) //check for your check box control ID
    {
        pDC->SetTextColor(RGB(255,0,0));
    }
    return hbr;
}

Beware that this works not for regular push buttons, but for check boxes there should be no problem. No need to implement an owner-drawn control.

EDIT:

You have to make sure, your check box uses the BS_AUTOCHECKBOX style. Also make sure the BS_OWNERDRAW style is not set.

EDIT #2: DrawFrameControl() with DFCS_BUTTONCHECK will let you draw the default check box bitmaps.

Frank Bollack
Check the comment I made for @Lior Kogan's answer
djeidot
I build an example an made the changes I suggested and it works fine. Maybe you could post some more of your code. As Lior Kogan suggested, you have to implement your handler in you containing dialog and register it properly with the message map.
Frank Bollack
Found out it works in VS2003 but not in VS2008. Still checking DrawFrameControl().
djeidot
DrawFrameControl() works but it doesn't handle the Common Controls 6.0 manifest. Thanks anyway :)
djeidot
A: 

You can see a full implementation of an owner drawn button (with bitmap any many other features) in OXBitmapButton.h in dundas ultimate toolbox.

http://www.codeproject.com/KB/MFC/UltimateToolbox.aspx

Lior Kogan
+4  A: 

I use UxTheme.dll to draw my custom checkbox.

First I draw the check-box marks using: DrawThemeBackground passing it a modified rect (checkboxRect.right = pCustomDraw->rc.left + 15;)

And then I draw the text by myself using ::DrawText.

I hope it helps.

Javier De Pedro
That works, plus I found out that CMFCVisualManager::GetInstance()->DrawCheckBox does the same thing.
djeidot
Good to know you could solve your problem!
Javier De Pedro
Yeah I did, though it's still a lot of work just to change the text color...
djeidot
Welcome to MFC's world!
Javier De Pedro