1

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?)

djeidot
  • 4,542
  • 4
  • 42
  • 45

4 Answers4

4

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
  • 2,219
  • 4
  • 32
  • 49
  • 2
    That works, plus I found out that CMFCVisualManager::GetInstance()->DrawCheckBox does the same thing. – djeidot Sep 28 '09 at 14:57
1

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
  • 19,919
  • 6
  • 53
  • 85
  • Yeah, but that doesn't work with CButtons... Although I am able to set the background color, the SetTextColor command doesn't work. – djeidot Sep 28 '09 at 12:33
  • It works when you implement it in the containing dialog level. – Lior Kogan Sep 28 '09 at 12:45
  • It is not working for me. I'm using CMFCVisualManager from MFC Feature Pack to set the application look, maybe that's why. – djeidot Sep 28 '09 at 12:52
1

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
  • 24,478
  • 5
  • 49
  • 58
  • Check the comment I made for @Lior Kogan's answer – djeidot Sep 28 '09 at 12:43
  • 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 Sep 28 '09 at 12:54
  • Found out it works in VS2003 but not in VS2008. Still checking DrawFrameControl(). – djeidot Sep 28 '09 at 14:02
  • DrawFrameControl() works but it doesn't handle the Common Controls 6.0 manifest. Thanks anyway :) – djeidot Sep 28 '09 at 14:25
0

To get the windows system checkbox images (which is I think what was asked):

LoadBitmap(0,OBM_CHECKBOXES);

will return a bitmap handle to a 4x3 bitmap of all the checkboxes (includes radio buttons, and also enabled and disabled)

Robbie Matthews
  • 1,404
  • 14
  • 22