CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Jul 2003
    Posts
    260

    How to subclass CButton class for Checkbox?

    Hi Friends,

    I need to subclass a checkbox, and that is of CButton class. I tried that, and overwrite OnPaint(), however the check box disappears, I guess that is because I would need to paint the checkbox as well, well, that is beyond my knowledge, so I tried to overwrite OnCtlColor, however that doesn't get called.

    One post said that I would need to set the ownerdraw flag, I tried that it doesn't work with the following:

    Code:
     
    m_chk.SubclassDlgItem(IDC_CHK1, this);
    
    DWORD style = m_chk.GetStyle();
    style |= BS_OWNERDRAW;
    ::SetWindowLong(m_chk, GWL_STYLE, style);
    I placed this in OnInitDlg.

    Please help, am I understanding this correctly and how to fix this.

    Basically I need to change the font size/color of the checkbox text but still retain the checkbox bitmap itself since I don't know how to drawt that.

    Thanks.
    Jiac

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Here is an example of a WM_PAINT handler for a class
    derived from CButton. I called it CHECK_EX.

    I wrote this on the spot, so I help it works.

    Basically I just added a check box as normal, Set a control
    variable to CHECK_EX with class wizard. The only thing I did
    was handle WM_PAINT.

    PHP Code:
    void CHECK_EX::OnPaint() 
    {
        
    CPaintDC dc(this); // device context for painting
        
    dc.SaveDC();
        
    CRect rect(0,0,0,0);
        
    GetClientRect(&rect);
        
    dc.FillSolidRect(&rect, ::GetSysColor(COLOR_BTNFACE));

        
    CRect check_rect(rect.leftrect.top rect.Height()/6rect.left4*rect.Height()/6rect.bottom rect.Height()/6);
        
    CRect text_rect(check_rect.right rect.Height()/6rect.toprect.rightrect.bottom);

        
    dc.FillSolidRect(&check_rectRGB(255,255,255));
        
    dc.DrawEdge(&check_rectEDGE_SUNKENBF_RECT);

        
    //CHANGE YOUR FONT HERE
        
    CString text("MY STINKIN CHECK BOX");
        
    dc.SetBkMode(TRANSPARENT);
        
    dc.DrawText(text, &text_rectDT_SINGLELINE|DT_LEFT|DT_VCENTER);

        
    dc.RestoreDC(-1);
        
    // TODO: Add your message handler code here
        
        // Do not call CButton::OnPaint() for painting messages

    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    OOPS... I forgot to draw the check. you need to add that, but
    that just amount to drawing some lines
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  4. #4
    Join Date
    Jul 2003
    Posts
    260
    Can you also tell me how to draw the check boxes? That is the part that I have the least idea of.

    Thanks.
    Jiac

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    You could use DrawFrameControl() with DFCS_BUTTONCHECK.
    However, couldn't you just set a font and color, and NOT do ownerdraw stuff?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    I didn't know you could change the color of the text of a CButton
    without making it owner drawn.
    Can you? If you can then just do that.

    If you do go the owner drawn route, then don't do what I did
    above.

    It seems that BS_AUTOCHECKBOX and BS_OWNERDRAW
    do not go together well, but I did not try creating the button
    outside of the resource editor

    The easiest way to do owner drawn is to create a regular owner
    drawn button and use DrawFrameControl() control draw the
    check and box. Or you can draw the box and check yourself.
    With this method you have to keep track of the state of the check yourself.
    Let me know if you need it and I will give you the code
    Last edited by souldog; March 9th, 2004 at 05:11 PM.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Jul 2003
    Posts
    260
    Can you tell me how to change the font size and font color?

    In addition, for custom draw, if I were to use DFCS_BUTTONCHECK, would user's check /uncheck still be trapped? How to do that?

    Thanks.
    Jiac

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Make a CFont data member in your dialog.
    In OnInitDialog(), create the font you like (look up CreateFont(...)), and set it to be used for your checkbox (using SetFont(...)).
    Create OnCtlColor() handler and check if the window pointer passed in is your CheckBox, then set the text color to whatever using pDC->SetTextColor(...);
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Darn

    I didn't realize OnCtrlColor worked when a CButton has the
    BS_AUTOCHECKBOX style.

    That is the way to go.
    Last edited by souldog; March 11th, 2004 at 02:23 PM.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Originally posted by souldog
    OnCtrlCOlor does not work for CButton objects. That is the problem
    What do you mean? It works fine for me...
    Code:
    HBRUSH CDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
        if(pWnd == &m_Check)
        {
            pDC->SetTextColor(RGB(255,0,0));
        }
        return hbr;
    }
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  11. #11
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    Yes, I corrected myself.
    Try using that method to change the text color for a regular button.

    That is why i got confused
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  12. #12
    Join Date
    Mar 2004
    Posts
    1
    BS_AUTOCHECKBOX and BS_OWNERDRAW can not work together
    if you change the buttong to BS_OWNERDRAW style, it is not CheckBox any more. after you have changed the style to BS_OWNERDRAW , the function SetCheck(), GetCheck() will not work.

  13. #13
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    That is correct, you have to manage that yourself.
    But in this case it is not needed.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  14. #14
    Join Date
    May 2004
    Posts
    28
    Sorry for bumping this, but I have the very same problem. How can I change the color of the font and background in a CheckBox using _only_ Windows API functions/calls/messages/etc/etc.. ?

    I'd apprecitate any help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured