仅仅更换图片,不具有可移植性,可继承性,则可使用以下方法。
1 创建MFC工程
2 添加按钮和图片(如果需要图片)
3 按钮属性Owner Draw = TRUE
4 消息映射WM_DRAWITEM
5 添加如下代码(文字改为红色)
void CTestButtonDlg::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(lpDrawItemsStruct->CtlType == ODT_BUTTON);
//If drawing selected, add the pushed style to DrawFrameControl
if (lpDrawItemsStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
//draw the button frame
::DrawFrameControl(lpDrawItemsStruct->hDC, &lpDrawItemsStruct->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(lpDrawItemsStruct->hDC, RGB(255, 0, 0));
::DrawText(lpDrawItemsStruct->hDC, strText, strText.GetLength(),
&lpDrawItemsStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
::SetTextColor(lpDrawItemsStruct->hDC, crOldColor);
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}
例程下载地址(0积分):http://download.csdn.net/detail/u013469110/9265517