-
October 16th, 2019, 02:03 AM
#1
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
-
October 16th, 2019, 03:08 AM
#2
Re: Changing background color of CButton
Victor Nijegorodov
-
October 16th, 2019, 03:12 AM
#3
Re: Changing background color of CButton
No.Compiling successfully but color not reflected
-
October 16th, 2019, 03:15 AM
#4
Re: Changing background color of CButton
Did you debugged this code? Is the OnDrawItem called?
Victor Nijegorodov
-
October 16th, 2019, 04:15 AM
#5
Re: Changing background color of CButton
I debugged. onDrawItem() in not getting called.Not able to figure where i am doing wrong
-
October 16th, 2019, 05:48 AM
#6
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.
Victor Nijegorodov
-
October 16th, 2019, 06:14 AM
#7
Re: Changing background color of CButton
Ok.After doing that ,does i need to map that CMyButton Variable in DDX_CONTROL() too ?
-
October 16th, 2019, 06:15 AM
#8
Re: Changing background color of CButton
Originally Posted by Beginner_MFC
Ok.After doing that ,does i need to map that CMyButton Variable in DDX_CONTROL() too ?
Yes, of course!
Victor Nijegorodov
-
October 16th, 2019, 07:19 AM
#9
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?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|