上面这位老兄讲了原理,下面我给了一个例子,看看吧!也许很有帮助。 class CMyClass : public CButton { // Construction public: CMyClass();// Attributes public:// Operations public:// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CMyClass) public: virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); //}}AFX_VIRTUAL// Implementation public: virtual ~CMyClass(); // Generated message map functions protected: //{{AFX_MSG(CMyClass) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG DECLARE_MESSAGE_MAP() }; #include "MyClass.h" CMyClass::CMyClass() { }CMyClass::~CMyClass() { } BEGIN_MESSAGE_MAP(CMyClass, CButton) //{{AFX_MSG_MAP(CMyClass) // NOTE - the ClassWizard will add and remove mapping macros here. //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CMyClass message handlersvoid CMyClass::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item CDC dc; dc.Attach(lpDrawItemStruct->hDC); //Get device context object CRect rt; rt = lpDrawItemStruct->rcItem; //Get button rect dc.FillSolidRect(rt, RGB(0, 0, 255)); //Fill button with blue color UINT state = lpDrawItemStruct->itemState; //Get state of the button if ( (state & ODS_SELECTED) ) // If it is pressed { //dc.SetBkColor(RGB(255,0,0)); //dc.SetTextColor(RGB(0,255,0)); dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT); // Draw a sunken face } else {//BF_RECT Entire border rectangle. dc.DrawEdge(rt,EDGE_RAISED,BF_RECT); // Draw a raised face } dc.SetTextColor(RGB(255,255,120)); // Set the color of the caption to be yellow CString strTemp; GetWindowText(strTemp); // Get the caption which have been set //DT_CENTER Centers text horizontally. dc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE); // Draw out the caption if ( (state & ODS_FOCUS ) ) // If the button is focused { // Draw a focus rect which indicates the user // that the button is focused int iChange = 2; rt.top += iChange; rt.left += iChange; rt.right -= iChange; rt.bottom -= iChange; dc.DrawFocusRect(rt); } dc.Detach(); }
class CMyClass : public CButton
{
// Construction
public:
CMyClass();// Attributes
public:// Operations
public:// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyClass)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL// Implementation
public:
virtual ~CMyClass(); // Generated message map functions
protected:
//{{AFX_MSG(CMyClass)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};
#include "MyClass.h"
CMyClass::CMyClass()
{
}CMyClass::~CMyClass()
{
}
BEGIN_MESSAGE_MAP(CMyClass, CButton)
//{{AFX_MSG_MAP(CMyClass)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyClass message handlersvoid CMyClass::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC dc;
dc.Attach(lpDrawItemStruct->hDC); //Get device context object
CRect rt;
rt = lpDrawItemStruct->rcItem; //Get button rect dc.FillSolidRect(rt, RGB(0, 0, 255)); //Fill button with blue color UINT state = lpDrawItemStruct->itemState; //Get state of the button
if ( (state & ODS_SELECTED) ) // If it is pressed
{
//dc.SetBkColor(RGB(255,0,0));
//dc.SetTextColor(RGB(0,255,0));
dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT); // Draw a sunken face
}
else
{//BF_RECT Entire border rectangle. dc.DrawEdge(rt,EDGE_RAISED,BF_RECT); // Draw a raised face
}
dc.SetTextColor(RGB(255,255,120)); // Set the color of the caption to be yellow
CString strTemp;
GetWindowText(strTemp);
// Get the caption which have been set
//DT_CENTER Centers text horizontally.
dc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE); // Draw out the caption
if ( (state & ODS_FOCUS ) ) // If the button is focused
{
// Draw a focus rect which indicates the user
// that the button is focused
int iChange = 2;
rt.top += iChange;
rt.left += iChange;
rt.right -= iChange;
rt.bottom -= iChange;
dc.DrawFocusRect(rt);
}
dc.Detach();
}
在MFC中同过消息反射机制,WM_DRAWITEM消息被送到控件中,从而实现了自绘代码的自包含。更加符合面向对象的标准