自定义按钮控件-热点按钮

来源:互联网 发布:农村淘宝官方下载 编辑:程序博客网 时间:2024/05/15 04:07

1."HotButton.h"

指向基类的指针在操作它的多态类对象时,会根据不同的类对象,调用其相应的函数,这个函数就是虚函数。
class CHotButton : public CButton{public:CHotButton();CBitmap m_Bitmap;//按钮正常状态时的CBitmap对象CBitmap m_HotBitmap;//按钮热点状态时的CBitmap对象BOOLm_IsPressed;//按钮是否被按下BOOLm_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 defaultCPoint 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 classSetTimer(1,10,NULL); //设置定时器,每间隔10ms判断一次鼠标的位置以及鼠标和按钮的位置关系CButton::PreSubclassWindow();}void CHotButton::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultm_IsPressed = TRUE;CButton::OnLButtonDown(nFlags, point);}void CHotButton::OnLButtonUp(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultm_IsPressed = FALSE;CButton::OnLButtonUp(nFlags, point);}

3.对话框头文件声明CBitmap类对象,并引用HotButton.h

4.对话框OnInitDialog函数加载位图资源

// TODO: Add extra initialization herefor(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();}



0 0
原创粉丝点击