1,创建一个类 继承自:CButtonl;
#pragma once
// CMyButton
class CMyButton : public CButton
{
DECLARE_DYNAMIC(CMyButton)
public:
CMyButton();
virtual ~CMyButton();
protected:
DECLARE_MESSAGE_MAP()
public:
virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
};
2,重写 DrawItem()方法:
// MyButton.cpp : 实现文件
//
#include "stdafx.h"
#include "DBN.h"
#include "MyButton.h"
// CMyButton
IMPLEMENT_DYNAMIC(CMyButton, CButton)
CMyButton::CMyButton()
{
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
END_MESSAGE_MAP()
// CMyButton 消息处理程序
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
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);
}
3,动态创建对象时注意控件的style必须有:
一个所有者描述的按钮具有
BS_OWNERDRAW
样式设置。