53上位机VC MF实现热点效果按钮
53上位机VC MF实现热点效果按钮
功能展示
一般我们人眼会追随动态的东西,当鼠标没过热点按钮时,按钮发生变化,就可以引起用户的注意,所以我们当前例程就来实现热点按钮功能,效果如图 要点提示 热点按钮的实现关键点用到了GetCursorPos()和PtInRect(); BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursorposition ); lpPoint 是一个指向[url=]POINT[/url] 结构体的指针,接收相对于桌面的鼠标位置 BOOL PtInRect( POINT point ) const;判断指定的point 是否在矩形范围内; 实现功能 1.新建基于对话框的应用程序 2.首先创建自己的按钮类;从Cbutton派生一个自己的类classCHotBtn : public CButton ,添加几个变量UINTm_nDownPic; //鼠标按下时显示的图片 UINT m_nNomalPic; //正常情况下显示的图片 UINT m_nDisablePic; //按钮失效时显示的图片 UINT m_nMovePic; //鼠标经过按钮时显示的图片 BOOL m_bIsInRect; //鼠标是否在按钮内;并在构造函数中初始化m_nDownPic = 0; m_nNomalPic = 0; m_nDisablePic = 0; m_nMovePic = 0; m_bIsInRect = FALSE;
3.添加函数PreTranslateMessage()reSubclassWindow(); OnTimer(); DrawItem(); - BOOL CHotBtn::PreTranslateMessage(MSG* pMsg)
- {
- if(pMsg->hwnd==this->GetSafeHwnd()&&pMsg->message==WM_KEYDOWN && pMsg->wParam==13)
- {
- pMsg->lParam = 0;
- pMsg->message = WM_LBUTTONDOWN;
- }
- if(pMsg->hwnd==this->GetSafeHwnd()&&pMsg->message==WM_KEYUP && pMsg->wParam==13)
- {
- pMsg->lParam = 0;
- pMsg->message = WM_LBUTTONUP;
- }
- return CButton::PreTranslateMessage(pMsg);
复制代码- void CHotBtn::PreSubclassWindow()
- {
- ModifyStyle(0,BS_OWNERDRAW,0);
- SetTimer(1,10,NULL); //设置定时器
- CButton::PreSubclassWindow();
- }
- void CHotBtn::OnTimer(UINT nIDEvent)
- {
- CPoint point; //声明Cpoint变量
- GetCursorPos(&point); //获得鼠标位置
- CRect rcWnd; //声明区域对象
- GetWindowRect(&rcWnd); //获得按钮区域
- if(rcWnd.PtInRect(point)) //判断鼠标是否在按钮上
- {
- if(m_bIsInRect == TRUE) //判断鼠标是否一直在按钮上
- goto END; //跳转到标记
- else //鼠标移动到按钮上
- {
- m_bIsInRect = TRUE; //设置m_IsInRect变量值
- Invalidate(); //重绘按钮
- }
- }
- else //不在按钮区域内
- {
- if(m_bIsInRect == FALSE) //判断鼠标一直在按钮外
- goto END; //跳转到标记
- else //鼠标移动到按钮外
- {
- Invalidate(); //重绘按钮
- m_bIsInRect = FALSE; //设置m_IsInRect变量值
- }
- }
- END: CButton::OnTimer(nIDEvent); //设置标记,调用基类方法
- }
复制代码- //设置背景透明
- dc.SetBkMode(TRANSPARENT);
- //创建一个位图画刷
- CBrush brush(m_color);
- dc.SelectObject(&brush);
- CPen pen(PS_NULL,1,m_color);
- dc.SelectObject(&pen);
- if(m_bSetToPol) dc.Polygon(m_arrays,m_nNum);
- else dc.Ellipse(0,0,rect.Width(),rect.Height());
- if(m_bIsPressed)
- {
- CPen pen(PS_DASHDOTDOT,2,RGB(0,0,0));
- dc.SelectObject(&pen);
- if(m_bSetToPol)
- {
- dc.MoveTo(m_arrays[0]);
- for(int i=1;i<m_nNum;i++)
- { dc.LineTo(m_arrays[i]);
- }
- dc.LineTo(m_arrays[0]);
- }
- else dc.Ellipse(0,0,rect.Width(),rect.Height());
- }
- else
- {
- CPen pen(PS_DASHDOTDOT,2,m_color);
- dc.SelectObject(&pen);
- if(m_bSetToPol)
- {
- dc.MoveTo(m_arrays[0]);
- for(int i=1;i<m_nNum;i++)
- { dc.LineTo(m_arrays[i]);
- }
- dc.LineTo(m_arrays[0]);
- }
- else dc.Ellipse(0,0,rect.Width(),rect.Height());
- }
-
复制代码- void CHotBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
- {
- CDC dc;
- dc.Attach(lpDrawItemStruct->hDC);
- UINT state = lpDrawItemStruct->itemState;
- CRect rect; //声明区域对象
- GetClientRect(rect);
- CString text;
- GetWindowText(text);
-
- if(state & ODS_DISABLED)
- {
- DrawBK(&dc,m_nDisablePic);
- dc.SetTextColor(RGB(0,0,0));
- }
- else if(state&ODS_SELECTED)
- {
- DrawBK(&dc,m_nDownPic);
- dc.SetTextColor(RGB(0,0,255));
- }
- else if(m_bIsInRect==TRUE)
- {
- DrawBK(&dc,m_nMovePic);
- dc.SetTextColor(RGB(255,0,0));
- }
- else //默认情况下
- {
- DrawBK(&dc,m_nNomalPic);
- dc.SetTextColor(RGB(0,0,0));
- }
- if(state&ODS_FOCUS)
- {
- CRect FocTect(rect);
- FocTect.DeflateRect(2,2,2,2);
- dc.DrawFocusRect(&FocTect);
- lpDrawItemStruct->itemAction = ODA_FOCUS ;
- }
- dc.SetBkMode(TRANSPARENT);
- dc.DrawText(text,&rect,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
- }
复制代码- <div style="text-align: center;"><font size="4">4再添加背景自绘函数DrawBK() 及对外接口函数SetBkBitmap()</font></div>void CHotBtn::DrawBK(CDC *pDC, UINT ResID)
- {
- if(m_nDownPic&&m_nNomalPic&&m_nDisablePic&&m_nMovePic)
- {
-
- CDC memDC;
- memDC.CreateCompatibleDC(pDC);
- CRect rect; //声明区域对象
- GetClientRect(rect); //获得编辑框客户区域
- CBitmap bitmap;
- BITMAP bitStruct;
- bitmap.LoadBitmap(ResID);
- bitmap.GetBitmap(&bitStruct);
- memDC.SelectObject(&bitmap);
- pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,bitStruct.bmWidth
- ,bitStruct.bmHeight,SRCCOPY);
- memDC.DeleteDC();
- bitmap.DeleteObject();
- }
- }
- void CHotBtn::SetBkBitmap(UINT nDownPic,UINT nNomalPic,UINT nDisablePic,UINT nMovePic)
- {
- m_nDownPic = nDownPic; //鼠标按下时显示的图片
- m_nNomalPic = nNomalPic; //正常情况下显示的图片
- m_nDisablePic = nDisablePic; //按钮失效时显示的图片
- m_nMovePic = nMovePic; //鼠标经过按钮时显示的图片
- }
复制代码5.最后是在对话框中添加按钮资源,关联变量,并进行背景位图设置如我们例程m_Btn.SetBkBitmap(IDB_BUTTONDOWN,IDB_BUTTONMOVE,IDB_BUTTONMOVE,IDB_BUTTONUP);
我们来实现功能实现过程
|