CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    May 2009
    Posts
    103

    Help Drawing Custom Radio Button

    I am working on an application that we are "customizing" the dialog controls by changing background colors, forecolors, etc.

    Have it working really well except we have a Radio Button with the PUSHLIKE option turned on. Because of that it "looks" and operates like a button. Problem is, it is using the windows defaults instead of our custom code for buttons.

    Most likely, I need to modify my code, somewhere, to get this to look like the other buttons. Because alot of this code was "borrowed" from the web, I am not sure where or how to modify it for these PUSHLIKE controls.

    Here is the code....

    The buttons are changed in the OnDrawItem for the dialog with:
    Code:
     CRect rect;
    HBRUSH hbr;
    HPEN hpen,holdpen;
    COLORREF crOldColor;
    
    switch(lpDrawItemStruct->CtlType)
    {
    	case ODT_BUTTON:
    		if(lpDrawItemStruct->hwndItem == m_help.m_hWnd)
    			break;
    		if(m_logo != NULL)
    			if(lpDrawItemStruct->hwndItem == m_logo->m_hWnd)
    				break;
    			UINT uStyle = DFCS_BUTTONPUSH;
    			// If drawing selected, add the pushed style to DrawFrameControl.
    		if (lpDrawItemStruct->itemState & ODS_SELECTED)
    			uStyle |= DFCS_PUSHED;
    		if (lpDrawItemStruct->itemState & ODS_DISABLED)
    		        uStyle |= DFCS_INACTIVE;
    		
                    // Draw the button frame.
    		::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,DFC_BUTTON, uStyle);
    		
         	       // Fill the button
    		rect=lpDrawItemStruct->rcItem;
    		rect.left+=2;
    		rect.right-=2;
    		rect.top+=2;
    		rect.bottom-=2;
    		if (lpDrawItemStruct->itemState & ODS_DISABLED)
    			::FillRect(lpDrawItemStruct->hDC,&rect,m_bkBrush);
    		else
    			::FillRect(lpDrawItemStruct->hDC,&rect,m_brushButton);
    		
    		// Get the button's text.
    		::GetDlgItemText(this->m_hWnd, lpDrawItemStruct->CtlID, sTemp.GetBuffer(255), 255);
    		sTemp.ReleaseBuffer();
    		
          	        // Draw the button text using the text color
    		if (lpDrawItemStruct->itemState & ODS_DISABLED)
    		{
    			crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(150,150,150));
    			::SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);
    		}
    		else
    			crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, m_colorButtonText);
    		::DrawText(lpDrawItemStruct->hDC, sTemp, sTemp.GetLength(),&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
    		::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
    		return;
            }
    	
    	CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
    The other controls are modified in the OnCtlColor with the following code:
    Code:
    HBRUSH hbr = NULL;
    switch(nCtlColor)
    {
    	case CTLCOLOR_MSGBOX:
    		break;
    	case CTLCOLOR_EDIT:
    		pDC->SetBkColor(m_colorListBoxBk);
    		pDC->SetTextColor(m_colorListBoxText);
    		hbr = m_brushListBox;
    		break;
    	case CTLCOLOR_SCROLLBAR:
    		break;
    	case CTLCOLOR_MAX:
    		break;
    	case CTLCOLOR_STATIC:
    		pDC->SetBkMode(TRANSPARENT);
    		pDC->SetTextColor(m_colorStaticText);
    		hbr = m_bkBrush;
    		break;
    	case CTLCOLOR_DLG:
    		hbr = m_bkBrush;
    		break;
    	case CTLCOLOR_LISTBOX:
    		pDC->SetBkColor(m_colorListBoxBk);
    		pDC->SetTextColor(m_colorListBoxText);
    		hbr = m_brushListBox;
    		break;
    	case CTLCOLOR_BTN:
    		pDC->SetBkColor(m_colorButtonBk);
    		hbr = m_brushButton;
    		break;
    	default:
    		break;
    	}
    	if(hbr == NULL)
    		hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }
    Any help would be greatly appreciated. If I can't get this to work I am going to have to turn of the PUSHLIKE feature and/or change the control altogether.

    Thanks ahead of time.

  2. #2
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Help Drawing Custom Radio Button

    Have you stepped through the OnDrawItem code? Is your code executed?
    Gort...Klaatu, Barada Nikto!

  3. #3
    Join Date
    May 2009
    Posts
    103

    Re: Help Drawing Custom Radio Button

    Yes... little tough because the code fires with every control repeatatively.

    But, we were able to see that radio buttons don't apparently use the OnDrawItem code... it uses the OnCtlColor:

    With a "regular" radio button, it is treated as a CTLCOLOR_STATIC. Set as a PUSHLIKE, it goes to the CTLCOLOR_BTN which steps through the code but doesn't appear to affect the buttons.



    A "regular" button does not use the OnCtlColor code but uses the OnDrawItem.

    Of course, that is unless I am missing something altogether.

    Pete

  4. #4
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Help Drawing Custom Radio Button

    Did you specify BS_OWNERDRAW for the button style?
    Gort...Klaatu, Barada Nikto!

  5. #5
    Join Date
    May 2009
    Posts
    103

    Re: Help Drawing Custom Radio Button

    Hmmmm.... good question... unfortunately I don't have that code here. I am assuming that is an option of a RADIO BUTTON as well?

    Will have to check that when I get back to that development system.

  6. #6
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Help Drawing Custom Radio Button

    Yes, a radiobutton is a style applied to a CButton, so, you'll need to make sure BS_OWNERDRAW is also applied.
    Gort...Klaatu, Barada Nikto!

  7. #7
    Join Date
    May 2009
    Posts
    103

    Re: Help Drawing Custom Radio Button

    Finally was able to check... the RADIOBUTTON Properties does not have a OwnerDrawn option. Just to clarify, this is a RADIO BUTTON with the PUSHLIKE property enabled, not a CButton.

    Guess I should state that this is VC++v6 (if that matters).

    Thanks

  8. #8
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Help Drawing Custom Radio Button

    Actually, it is a CButton. As I stated earlier, a radio button is a style (ie. BS_RADIOBUTTON) applied to a CButton. Just a matter of semantics.
    Gort...Klaatu, Barada Nikto!

  9. #9
    Join Date
    May 2009
    Posts
    103

    Re: Help Drawing Custom Radio Button

    Ok, that's fine except that the radio button properties are not the same as a cButton in the IDE. That is, there is no OWNERDRAWN option I can select, like I can with the cButton.

    Semantics aside, how do I get this radio button that "looks" like standard windows button to act like my other buttons being processed by "OnDrawItem"? That's the issue I am dealing with that I need to resolve or I need to get rid of the radio button altogether (which is not ideal) and do something else.

  10. #10
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Help Drawing Custom Radio Button

    You can modify the button style yourself and add BS_OWNERDRAW using ModifyStyle (). MFC will then provide you with the opportunity to render the radio button any way you like.
    Gort...Klaatu, Barada Nikto!

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Help Drawing Custom Radio Button

    1. It is not a good idea to draw dialog controls in the dialog class. The better way - derive bew class(es) for the controls and implement the painting in the derived class(es).
    2. Have a look at the CCheckSK - An Extended Check-box class
    Victor Nijegorodov

  12. #12
    Join Date
    May 2009
    Posts
    103

    Re: Help Drawing Custom Radio Button

    Seeing how this might be getting a bit above my head here....

    It looks like I need to put the modifystyle() command, somewhere, in the CPP file that has the radiobutton in it... I am assuming, possibly the OnInitDialog... I think?

    Doing some searching on MSDN and google, to add add the BS_OWNDERDRAWN I would do....

    ModifyStyle(0,BS_OWNERDRAWN);

    If that is correct, then once I have it programatically added, how do I get it to recognize the OnDrawItem cpde? What I've done in the past is click on the OWNERDRAWN option for a control in the ClassWizard and that has made the "connection" and let me modify that code snippet with my own code. So from the programmatic end instead of the IDE/CLASSWIZARD, I am getting a bit lost.


    Thanks again, any help is greatly appreciated.

  13. #13
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Help Drawing Custom Radio Button

    As Victor stated, you typically derive your own control class and modify the code accordingly. I usually chage styles within the PreSubclass event. Once you've changed the style, MFC will recognize that you want to render the radio button and call DrawItem () of the derived control class.
    Gort...Klaatu, Barada Nikto!

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