Changing background color of CButton
I want to change the background ground color of CButton, i dont want to change it to CMFCButton and than use SetFaceColor() .For changing background color of CButton my code is as follows:
MyDialog.h
Code:
afx_msg void onDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
MyDialog.cpp
Code:
BEGIN_MESSAGE_MAP (MyDialog, CDialog)
ON_WM_DRAWITEM()
END_MESSAGE_MAP
void MyDialog :: OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
If(nIDCtl == IDOK)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
RECT rect;
rect = lpDrawItemStruct->rcItem;
dc.FillSolidRect(&rect,RGB(0,255,0));
UINT state=lpDrawItemStruct->itemState;
if((state & ODS_SELECTED))
{
dc.DrawEdge(&rect,EDGE_SUNKEN,BF_RECT);
}
else
{
dc.DrawEdge(&rect,EDGE_RAISED,BF_RECT);
}
dc.SetBkColor(RGB(100,100,255));
dc.SetTextColor(RGB(255,0,0));
dc.SetBkMode( TRANSPARENT );
TCHAR buffer[MAX_PATH];
ZeroMemory(buffer,MAX_PATH );
::GetWindowText(lpDrawItemStruct->hwndItem,buffer,MAX_PATH);
dc.DrawText(buffer,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
dc.Detach();
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
MyDialog.rc
DEFPUSHBUTTON "OK" IDOK,10,200,60,18 | BS_OWNERDRAW
Re: Changing background color of CButton
Re: Changing background color of CButton
No.Compiling successfully but color not reflected
Re: Changing background color of CButton
Did you debugged this code? Is the OnDrawItem called?
Re: Changing background color of CButton
I debugged. onDrawItem() in not getting called.Not able to figure where i am doing wrong
Re: Changing background color of CButton
I'd recommend you to derive your own class of the CButton (say, CMyButton), declare your button member to be of CMyButton type.
Then implement the custom or owner drawing within this class.
Re: Changing background color of CButton
Ok.After doing that ,does i need to map that CMyButton Variable in DDX_CONTROL() too ?
Re: Changing background color of CButton
Quote:
Originally Posted by
Beginner_MFC
Ok.After doing that ,does i need to map that CMyButton Variable in DDX_CONTROL() too ?
Yes, of course!
Re: Changing background color of CButton
I showed you how to do it with CMFCButton. There was obviously something simple wrong with your code and suggested you use the debugger to figure out what. Did you?