Click here to Skip to main content
15,902,112 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am doing a program to change the colour of push button, edit, check box by choosing the colour from combo box by clicking push button. My problem is the push button colour is not changing. Please help me to change the colour of the push button using owner draw, I have no error but the output is not displaying,, It shows a pop up window stating
"Debug Assertion Failed"
Prog: D:\output\assign\debug\combotest.exe
File:winctrl1.cpp
Line 72
For information on how your program can cause an assertion failure, see the visual c++ doc on asserts (Press retry to debug the app)

Why it is coming like this? I am new to MFC. If I am very silly sorry for wasting your time, please correct my mistake.

here is my code
C++
void CAboutDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	// TODO: Add your message handler code here and/or call default
    UINT uStyle = DFCS_BUTTONPUSH;
   // This code only works with buttons.
   ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
   // If drawing selected, add the pushed style to DrawFrameControl.
   if (lpDrawItemStruct->itemState & ODS_SELECTED)
      uStyle |= DFCS_PUSHED;
   // Draw the button frame.
   ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, 
      DFC_BUTTON, uStyle);
   // Get the button's text.
   CString strText;
   GetWindowText(strText);
   // Draw the button text using the text color red.
   COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
   ::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(), 
      &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
   ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}

thank you
AJ
Posted
Updated 19-Mar-11 1:55am
v2

1 solution

Isn't it self-describing? CtlType is not ODT_BUTTON. Why writing assertion if first place is you don't know what the failure of assertion during run time means. You're trying to write MCF code which applies owner draw to all buttons on the windows without sub-classing of control types. You should either adapt this code to handle not only buttons, or make sure that only buttons use *OWNERDRAW* (for buttons, it is BS_OWNERDRAW) style.

Another (better) option is to sub-class control which need custom rendering.

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900