I love your example on the colored button. Its easy to understand. I was wondering if you could explain how to do the same thing but for a button that is an icon? I looked on here for bitmapped button. I simply could not understand the guy cause evidently some steps were skipped.
I try almost all exemples on this thread and I'm still not able to use a custom button on a CFormView class.
So I add a button in my CFormView ressource, set the ownerdraw to true and give a new ID. After, I add a variable in my CFormView with class wizard( So the DDX stuff appear).
When I start my program, I have an assertion faillure in the CButton::DrawItem function.
Have an hint?
I also tried that: don't put a control button in the ressource editor and "Create" my button in the CMyFormView::OnCreate(...) fonction. like:
m_myButton.Create("C",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, rect, this, IDC_MYBUTTON);
But, nothing appear!
Posted by Mahmoud
on 04/01/2016 01:52pm
if you set the "Owner Draw" flag as true and forget to overweight "DrawItem" virtual function, You will get "assertion failure" error. go ahead and overwrite the function.
Didnt work for me. Simply followed what you said. No errors. Nothing. But no result. But I didnt add a new button. But used for a button that was already existing and was functional. Any problem??
Then modified the project to use the class with a newly added button resource. Even then no expected result obtained. What can be the reason?
Thank you for this wonderful example. It has saved me much time. I am trying to expand it by allowing a button to be one color under certain circumstances, and under others, look like all the other normal buttons. So, at runtime I know which way I want it, and its easy to use your code to set the text and background color of the button on the fly, but I can not figure out how to set the background color back to the original color. What I have tried is ::GetDC()->GetBKColor() but this seems to alwyas be white. Any help is much appreciated.
if ( bPreviousIncidents )
m_odbtnPreviousIncidents.m_clrBackground = RGB(255, 0,0); // Red
m_odbtnPreviousIncidents.m_clrText = RGB(0,0,255); // Blue
// Get from another button
COLORREF clrBackground = m_btnSameIncident.GetDC ( )->GetBkColor ( );
m_odbtnPreviousIncidents.m_clrBackground = clrBackground;
COLORREF clrText = m_btnSameIncident.GetDC ( )->GetTextColor ( );
m_odbtnPreviousIncidents.m_clrText = clrText;
//Get button rect
rt = lpDrawItemStruct->rcItem;
// Fill button with passed in color
//Get state of the button
UINT state = lpDrawItemStruct->itemState;
// If it is pressed
if ( (state & ODS_SELECTED) )
// Draw a sunken face
// Draw a raised face
// Set the color of the caption to be yellow
// Get the caption which have been set
// Draw out the caption
// If the button is focused
if ( (state & ODS_FOCUS ) )
// Draw a focus rect which indicates the user
// that the button is focused
int iChange = 3;
rt.top += iChange;
rt.left += iChange;
rt.right -= iChange;
rt.bottom -= iChange;