1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to make a color to Radio Button and Check Box

Discussion in 'MFC' started by tgm.arjun09, Mar 6, 2009.

  1. tgm.arjun09

    tgm.arjun09 New Member

    Joined:
    Mar 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi All!!!

    On a Dialog window i've made it a colored or Bitmap or Image. And Kept Radio Button and Check Box so We need to change the color of the Check Box and Radio Button according to Dialog Displayed.
    I've tried with OnCtlColor Event. but it is working for static control but remaining it's not working.
    So plz do the needful.

    Thanking You
    tgm.arjun
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Is OnCTLColor Event fired for your controls
     
  3. tgm.arjun09

    tgm.arjun09 New Member

    Joined:
    Mar 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    OnCtlColor is fired bur it is working for static control. For check box and radio buttons it is not displaying dialog back ground color. Plz can u try and give the information.

    Thanking You
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    I just realized you had the thread in the introduction forum and I moved it to MFC/Win32 forum.

    If the event is fired it should and just post the code you are working on and we can look into it.
     
  5. tgm.arjun09

    tgm.arjun09 New Member

    Joined:
    Mar 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Here my code is working if i did like this for radio button and check box and static controls. But i need to change the color of the text for check box and radio buttons.

    Code:
     
    BOOL CEx3Dlg::OnInitDialog()
    {
    br1.CreateSolidBrush(RGB(100,100,100));
    }
     
     
    HCURSOR CEx3Dlg::OnQueryDragIcon()
    {
    return static_cast<HCURSOR>(m_hIcon);
    }
     
    HBRUSH CEx3Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC here
    
    if(nCtlColor == CTLCOLOR_DLG)
    {
    return br1;
    }
    else if(pWnd->GetDlgCtrlID()==IDC_CHECK1)
    {
    //pDC->SetTextColor(RGB(150,150,0));
    //pDC->SetBkColor(RGB(100,100,100));
    return br1;
    }
    else if(pWnd->GetDlgCtrlID()==IDC_RADIO1)
    {
    //pDC->SetTextColor(RGB(150,150,0));
    //pDC->SetBkColor(RGB(100,100,100));
    return br1;
    }
    else if(pWnd->GetDlgCtrlID()==IDC_STATIC)
    {
    pDC->SetTextColor(RGB(100,0,0));
    pDC->SetBkColor(RGB(100,100,100));
    return br1;
    }
    // TODO: Return a different brush if the default is not desired
    return hbr;
    }
    
    q
    With
    warm regards
    tgm.arjun
     
    Last edited by a moderator: Mar 9, 2009
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    You code is working perfectly fine for me

    I did this

    Defined a public Brush

    CBrush *brush;

    in

    CTestDlg

    which is my Dialog Class

    Then Initialized this in constructor

    brush = new CBrush(RGB(49,49,49));

    And then this goes into

    Code:
    HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    	if(pWnd->GetDlgCtrlID()==IDC_CHECK1)
    	{
    
    		pDC->SetTextColor(RGB(0, 255, 0));
     		pDC->SetBkColor(RGB(0, 0, 0));
     		return (HBRUSH)(brush->GetSafeHandle());
    	}
    	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
    	return hbr;
    }
    
     
  7. tgm.arjun09

    tgm.arjun09 New Member

    Joined:
    Mar 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Thanking you for your concern...

    I'm not getting the color text for radio button and check box. Can u check out.
    Code:
     
    BOOL CEx4Dlg::OnEraseBkgnd(CDC* pDC)
    {
    // TODO: Add your message handler code here and/or call default
    CBitmap bmp;
    CBitmap* ptrBmpOld;
    CDC dcMem;
    BITMAP bm;
    CRect rect;
    int i,j;
    int nVer, nHor;
    bmp.LoadBitmap(IDB_BITMAP2);
    bmp.GetBitmap(&bm);
    GetClientRect(rect);
    nHor=rect.Width()/bm.bmWidth+1;
    nVer=rect.Height()/bm.bmHeight+1;
    dcMem.CreateCompatibleDC(pDC);
    ptrBmpOld=dcMem.SelectObject(&bmp);
    for(i=0;i<nHor;i++)
    {
    for(j=0;j<nVer;j++)
    {
    pDC->BitBlt(i*bm.bmWidth,j*bm.bmHeight,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
    }
    }
    dcMem.SelectObject(ptrBmpOld);
    return TRUE;// CDialog::OnEraseBkgnd(pDC);
    }
     
     
     
     
    HBRUSH CEx4Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    //HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    // TODO: Change any attributes of the DC here
    HBRUSH brush;
    switch(nCtlColor)
    {
    case CTLCOLOR_BTN:
    case CTLCOLOR_STATIC:
    {
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(RGB(0,0,255));
    brush=(HBRUSH)::GetStockObject(NULL_BRUSH);
    break;
    }
    case CTLCOLOR_EDIT:
    {
    pDC->SetBkMode(TRANSPARENT);
    pDC->SetBkColor(RGB(0,0,0));
    pDC->SetTextColor(RGB(0,0,255));
    
    brush=(HBRUSH)::GetStockObject(NULL_BRUSH);
    return br1;
    break;
    }
    case CTLCOLOR_LISTBOX:
    {
    pDC->SetBkMode(TRANSPARENT);
    brush=(HBRUSH)::GetStockObject(NULL_BRUSH);
    break;
    }
    case CTLCOLOR_SCROLLBAR:
    {
    pDC->SetBkMode(TRANSPARENT);
    brush=(HBRUSH)::GetStockObject(NULL_BRUSH);
    break;
    }
    default:brush=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
    
    }
    if(pWnd->GetDlgCtrlID()==IDC_CHECK1)
    {
    pDC->SetTextColor(RGB(0,0,0));
    //pDC->SetBkColor(RGB(0,0,0));
    pDC->SetBkMode(TRANSPARENT);
    //brush=(HBRUSH)::GetStockObject(NULL_BRUSH);
    return br1;
    }
    if(pWnd->GetDlgCtrlID()==IDC_RADIO1)
    {
    pDC->SetTextColor(RGB(255,0,0));
    //pDC->SetBkColor(RGB(255,0,0));
    pDC->SetBkMode(TRANSPARENT);
    //brush=(HBRUSH)::GetStockObject(NULL_BRUSH);
    return br1;
    }
    // TODO: Return a different brush if the default is not desired
    return brush;
    }
    
     
    Last edited by a moderator: Mar 10, 2009
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Looks like you have some DC messed up and try using the steps I mentioned and see if it works and slowly put in your code and see which one does not work when you put it.
     
  9. tgm.arjun09

    tgm.arjun09 New Member

    Joined:
    Mar 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I've tried the above code in VS 6.0 color of the text radio button and check box is changing properly.
    But color of the text for radio button and check box is not working.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Could not understand what you meant here.
     
  11. tgm.arjun09

    tgm.arjun09 New Member

    Joined:
    Mar 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi ,
    Above i've posted code is working in VS 6.0 properly. But not in VS 8.0. The reason is that manifest dependency.
    In VS 8.0 by default it will add manifest dependecy macro. but in VS 6.0 it's not there so that's why it is working. So our code should have to work on both 6.0 and 8.0.

    u can find manifest dependecy in stdafx.h
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")

    can u try it how to solve the problem.

     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    My code is working in VS 8.0 as I have no VS 6 now
     
  13. tgm.arjun09

    tgm.arjun09 New Member

    Joined:
    Mar 6, 2009
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I meant to say that my code is working in VS 6.0 but not in VS 8.0.
    In VS 8.0 stdafx.h one directive is added.
    #pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")

    If i comment this in stdafx.h my code is working in VS 8.0 also.

    So what i have to do??

     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Comment that. :D
     

Share This Page