#include "stdafx.h" #include "MyBttom.h" #include "GlobalDefine.h" IMPLEMENT_DYNAMIC(CMyBottom, CMFCButton) CMyBottom::CMyBottom() { m_font.CreatePointFont(120,"宋体",NULL); m_TextColor = RGB_BLACK; m_DownColor = RGB_GRAY1; m_UpColor = RGB_GRAY2;//默认颜色 } CMyBottom::~CMyBottom() { } BEGIN_MESSAGE_MAP(CMyBottom, CMFCButton) END_MESSAGE_MAP() // m_MyImgBtn 消息处理程序 //设置btn 的显示方式,Tooltip 是鼠标放在按钮上的提示内容 void CMyBottom::SetButtonStyle(CString Tooltip) { EnableFullTextTooltip(true); // Use the application menu font at the button text font. EnableMenuFont(); // Use the current Windows theme to draw the button borders. EnableWindowsTheming(false); // Set the button to auto-repeat mode. SetAutorepeatMode(); // Set the background color for the button text. //m_CtrlDoorBtn.SetFaceColor(RGB(255,0,0),true); SetFontStyle(); // Set the tooltip of the button. SetTooltip(Tooltip); SizeToContent(); } void CMyBottom::SetFontStyle() { SetFont(&m_font); } void CMyBottom::Refreash() { //SetFontStyle(); } void CMyBottom::SetDownColor(COLORREF color) {//CMyButton类的函数 m_DownColor = color; } void CMyBottom::SetUpColor(COLORREF color) { if(color != m_UpColor)//颜色变化的时候刷新显示 { m_UpColor = color; SetFontStyle(); } } void CMyBottom::SetTextColor(COLORREF color) { m_TextColor = color; } void CMyBottom::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC dc; dc.Attach(lpDrawItemStruct->hDC);//得到绘制的设备环境CDC VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON); //得当Button上文字,这里的步骤是:1,先得到在资源里编辑的按钮的文字, //然后将此文字重新绘制到按钮上, //同时将此文字的背景色设为透明,这样,按钮上仅会显示文字 const int bufSize = 512; TCHAR buffer[bufSize]; GetWindowText(buffer, bufSize); int size=strlen(buffer);//得到长度 DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);//绘制文字 SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);//透明 if (lpDrawItemStruct->itemState&ODS_SELECTED)//当按下按钮时的处理 {////重绘整个控制 CBrush brush(m_DownColor); dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//利用画刷brush,填充矩形框 //因为这里进行了重绘,所以文字也要重绘 DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP); SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT); } else//当按钮不操作或者弹起时 { CBrush brush(m_UpColor); dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);// dc.SetTextColor(m_TextColor); DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP); SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT); } //绘制边框 { COLORREF fc=RGB(255-GetRValue(m_UpColor),255-GetGValue(m_UpColor),255-GetBValue(m_UpColor)); CBrush brush(fc); dc.FrameRect(&(lpDrawItemStruct->rcItem),&brush);//用画刷brush,填充矩形边框 } }