1."HotButton.h"
class CHotButton : public CButton
{
public:
CHotButton();
CBitmap m_Bitmap;//按钮正常状态时的CBitmap对象
CBitmap m_HotBitmap;//按钮热点状态时的CBitmap对象
BOOL m_IsPressed; //按钮是否被按下
BOOL m_IsInRect;//判断鼠标是否在按钮区域内
class CHotButton : public CButton
{
public:
CHotButton();
CBitmap m_Bitmap;
CBitmap m_HotBitmap;
BOOL m_IsPressed; //按钮是否被按下
BOOL m_IsInRect;
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
virtual void PreSubclassWindow();
public:
void SetCBitmap(CBitmap* Bitmap,CBitmap* HotBitmap);
virtual ~CHotButton();
protected:
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
2.HotButton.cpp
// HotButton.cpp : implementation file
//
#include "stdafx.h"
#include "HotspotButton.h"
#include "HotButton.h"
CHotButton::CHotButton()
{
m_IsPressed = FALSE;
}
CHotButton::~CHotButton()
{
}
BEGIN_MESSAGE_MAP(CHotButton, CButton)
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
void CHotButton::SetCBitmap(CBitmap* Bitmap, CBitmap* HotBitmap)
{
m_Bitmap.Attach(*Bitmap);
m_HotBitmap.Attach(*HotBitmap);
}
void CHotButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC);
UINT state = lpDrawItemStruct->itemState; //获取状态
CRect rect; //声明区域对象
GetClientRect(rect); //获得编辑框客户区域
CRect focusRect(rect);
focusRect.DeflateRect(2,2,2,2);
CDC memDC; //设备上下文
memDC.CreateCompatibleDC(&dc); //创建内存设备上下文
//按钮被选中或者获得焦点时
if((state&ODS_SELECTED)||(state&ODS_FOCUS))
{
memDC.SelectObject(&m_HotBitmap); //将位图选人设备上下文
BITMAP m_Bmp; //声明BITMAP对象
m_HotBitmap.GetBitmap(&m_Bmp); //获得位图信息
int x = m_Bmp.bmWidth; //获得位图的宽度
int y = m_Bmp.bmHeight; //获得位图的高度
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,x,y,SRCCOPY); //绘制位图背景
memDC.DeleteDC();
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&brush);
//绘制焦点矩形
dc.DrawFocusRect(focusRect);
//绘制立体效果
dc.DrawEdge(rect,BDR_RAISEDINNER|BDR_RAISEDOUTER,
BF_BOTTOMLEFT|BF_TOPRIGHT);
//获得焦点时绘制黑色边框
dc.Draw3dRect(rect,RGB(0,0,0),RGB(0,0,0));
}
else //默认情况下
{
if(m_IsInRect==TRUE) //鼠标在按钮上
{
memDC.SelectObject(&m_HotBitmap); //将位图选人设备上下文
BITMAP m_Bmp; //声明BITMAP对象
m_HotBitmap.GetBitmap(&m_Bmp); //获得位图信息
int x = m_Bmp.bmWidth; //获得位图的宽度
int y = m_Bmp.bmHeight; //获得位图的高度
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,x,y,SRCCOPY); //绘制位图背景
memDC.DeleteDC();
}
else
{
memDC.SelectObject(&m_Bitmap); //将位图选人设备上下文
BITMAP m_Bmp; //声明BITMAP对象
m_Bitmap.GetBitmap(&m_Bmp); //获得位图信息
int x = m_Bmp.bmWidth; //获得位图的宽度
int y = m_Bmp.bmHeight; //获得位图的高度
dc.StretchBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,x,y,SRCCOPY); //绘制位图背景
memDC.DeleteDC();
dc.DrawEdge(rect,BDR_RAISEDINNER|BDR_RAISEDOUTER,
BF_BOTTOMLEFT|BF_TOPRIGHT);//绘制立体效果
}
}
if(m_IsPressed) //在按钮被按下时绘制按下效果
{
dc.DrawFocusRect(focusRect);
dc.DrawEdge(rect,BDR_SUNKENINNER|BDR_SUNKENOUTER,BF_BOTTOMLEFT|BF_TOPRIGHT);
}
}
void CHotButton::OnTimer(UINT nIDEvent) //处理WM_TIMER消息,在该消息的处理函数中获得鼠标的位置,并判断鼠标是否在按钮控件区域
{
// TODO: Add your message handler code here and/or call default
CPoint point;//声明cpoint变量
GetCursorPos(&point); //获得鼠标位置
CRect rcWnd;
GetWindowRect(&rcWnd); //获得按钮区域
if(rcWnd.PtInRect(point)) //判断鼠标是否在按钮上
{
if(m_IsInRect == TRUE) //判断鼠标是移动到按钮上还是一直在按钮上
goto END;
else
{
m_IsInRect = TRUE;
Invalidate(); //重绘按钮
}
}
else
{
if(m_IsInRect == FALSE) //判断鼠标是移动到按钮外还是一直在按钮外
goto END;
else
{
Invalidate(); //重绘按钮
m_IsInRect = FALSE;
}
}
END: CButton::OnTimer(nIDEvent);
}
void CHotButton::PreSubclassWindow() //虚方法中设置定时器
{
// TODO: Add your specialized code here and/or call the base class
SetTimer(1,10,NULL); //设置定时器,每间隔10ms判断一次鼠标的位置以及鼠标和按钮的位置关系
CButton::PreSubclassWindow();
}
void CHotButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_IsPressed = TRUE;
CButton::OnLButtonDown(nFlags, point);
}
void CHotButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_IsPressed = FALSE;
CButton::OnLButtonUp(nFlags, point);
}
3.对话框头文件声明CBitmap类对象,并引用HotButton.h
4.对话框OnInitDialog函数加载位图资源
// TODO: Add extra initialization here
for(int i=0;i<2;i++)
{
m_Bitmap[i].LoadBitmap(IDB_BITMAP1+i*2);
m_HotBitmap[i].LoadBitmap(IDB_BITMAP2+i*2);
}
m_Save.SetCBitmap(&m_Bitmap[0],&m_HotBitmap[0]);
m_Exit.SetCBitmap(&m_Bitmap[1],&m_HotBitmap[1]);
for(int i=0;i<2;i++)
{
m_Bitmap[i].Detach();
m_HotBitmap[i].Detach();
}