[00010]-[2015-08-24]-[01]-[Windows 程序设计 ---MFC 基于对话框 按钮(Button)的美化]
在MFC编程中,希望按钮可以变得漂亮,自然是通过加载图片来使之more beautiful,但是GDI支持的位图有或多或少不尽人意,索性显示在对话框的背景上设置背景,同时在按钮
上加载基于背景的按钮图片,这样美化的效果做出来,还是比较可以的,也算是对GUI美化工作有一个交代了。
创建一个新类CNewButton Base on CButton
【1】为CNewButton添加成员变量 BOOL m_IsPressed; 用于表示按钮是否被按下,又标示自然需要我们写代码去检测m_IsPressed是否真的被按下;
【2】为CNewButton添加消息处理函数WM_LBUTTONDOWN WM_LBUTTONUP
void CNewButton::OnLButtonDown(UINT nFlags, CPoint point)
{
m_IsPressed = TRUE;
CButton::OnLButtonDown(nFlags, point);
}
void CNewButton::OnLButtonUp(UINT nFlags, CPoint point)
{
m_IsPressed = FALSE;
CButton::OnLButtonUp(nFlags, point);
}
【3】为CNewButton添加消息处理函数WM_ERASEBKGND
BOOL CNewButton::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return true;
return CButton::OnEraseBkgnd(pDC);
}
【4】为CNewButton添加DrawItem处理函数
void CNewButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
if(m_IsPressed)
{
CBitmap bmp;
CDC memdc;
memdc.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = memdc.SelectObject(&bmp);
bmp.LoadBitmap(IDB_BMP_BTN1);
memdc.SelectObject(&bmp);
pDC->BitBlt(0,0,135,62,&memdc,0,0,SRCCOPY);
memdc.SelectObject(pOldBitmap);
bmp.DeleteObject();
}
else
{
CBitmap bmp;
CDC memdc;
memdc.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = memdc.SelectObject(&bmp);
bmp.LoadBitmap(IDB_BMP_BTN0);
memdc.SelectObject(&bmp);
pDC->BitBlt(0,0,135,62,&memdc,0,0,SRCCOPY);
memdc.SelectObject(pOldBitmap);
bmp.DeleteObject();
}
}
程序运行效果:
算了,没法展示。。。。。