button控件属性如下,所有者描述设为TRUE
在类向导中添加消息函数afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
在消息函数中编辑如下
void CbaseMFCprojectDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
ASSERT(lpDrawItemStruct->CtlType = ODT_BUTTON);
CString strText;
//((CButton*)GetDlgItem(nIDCtl))->GetWindowText(strText);//获取控件的显示字符串
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
if (nIDCtl == IDC_BUTTON_BJSTATE) {//控件的ID
((CButton*)GetDlgItem(nIDCtl))->GetWindowText(strText);//获取控件的显示字符串
if (m_BJState == 0) {
CBrush brush(RGB(0, 240, 0));//button的背景画刷
//CBrush brush(RGB(240, 240, 240));
dc.FillRect(&(lpDrawItemStruct->rcItem), &brush);//用brush画刷填充此button
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_VCENTER | DT_SINGLELINE | DT_CENTER | DT_PATH_ELLIPSIS);//字体显示格式
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);//设置背景模式,透明的输出
}
else {
CBrush brush(RGB(240, 0, 0));
dc.FillRect(&(lpDrawItemStruct->rcItem), &brush);
DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_VCENTER | DT_SINGLELINE | DT_CENTER | DT_PATH_ELLIPSIS);
SetBkMode(lpDrawItemStruct->hDC, TRANSPARENT);
}
}
dc.DeleteDC();
CDialogEx::OnDrawItem(nIDCtl, lpDrawItemStruct);
}