设计XP风格的按钮,设计XP风格的按钮
【 tulaoshi.com - C语言心得技巧 】
设计XP风格的按钮
作者:郑恒(lbird)
BOOL TrackMouseEvent( LPTRACKMOUSEEVENT lpEventTrack );参数:
void CButtonXp::OnMouseMove(UINT nFlags, CPoint point) {if(m_bOver ==FALSE){//鼠标在按钮之上m_bOver =TRUE;//按钮重绘InvalidateRect(NULL,FALSE);//跟踪鼠标//当鼠标离开按钮区域会收到WM_MOUSELEAVE,该消息直接调用OnMouseOut()TRACKMOUSEEVENTtme;tme.cbSize =sizeof(TRACKMOUSEEVENT);tme.dwFlags =TME_LEAVE;tme.dwHoverTime=0;tme.hwndTrack =m_hWnd;::TrackMouseEvent(&tme);}CButton::OnMouseMove(nFlags, point);}再添加一成员函数OnMouseOut(),
在OnMouseOut()中写入以下代码
void CButtonXp::OnMouseOut (){//鼠标已离开按钮区域 m_bOver =FALSE;//重绘按钮InvalidateRect(NULL,FALSE);}4、添加成员函数 MouseOver()
//返回鼠标是否在按钮区域内BOOL CButtonXp::MouseOver(){return m_bOver;}5、最后重载DrawItem(LPDRAWITEMSTRUCT lpDIS)
void CButtonXp::DrawItem(LPDRAWITEMSTRUCT lpDIS) {CDC*pDC =CDC::FromHandle(lpDIS->hDC);CRectrtControl(lpDIS->rcItem);CPenpen,*old_pen;CBrushbrush,*old_brush;CString strText;HFONT hOldFont = (HFONT)pDC->SelectObject ((HFONT)::GetStockObject (DEFAULT_GUI_FONT));UINTstate =lpDIS->itemState;if(state & ODS_FOCUS){rtControl.DeflateRect(1,1);//拥有焦点矩形变小}if((state & ODS_DISABLED) ||(!MouseOver() &&!(state & ODS_SELECTED))){//普通状态、禁用、拥有焦点三种情况下pen.CreatePen (PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));brush.CreateSolidBrush(HLS_TRANSFORM(::GetSysColor(COLOR_3DFACE),-10,0));}else{COLORREFcrBorder =::GetSysColor(COLOR_HIGHLIGHT);pen.CreatePen(PS_SOLID, 1, crBorder);if( state & ODS_SELECTED){//按钮按下时brush.CreateSolidBrush(HLS_TRANSFORM(crBorder,+50,-50));pDC->SetTextColor(RGB(240,240,240));}else{//鼠标在区域内brush.CreateSolidBrush(HLS_TRANSFORM(crBorder,+80,-66));pDC->SetTextColor(::GetSysColor(COLOR_BTNTEXT));}}if(state &ODS_DISABLED)pDC->SetTextColor(::GetSysColor(COLOR_GRAYTEXT));//灰色字:禁用状态else if(state & ODS_SELECTED)pDC->SetTextColor(RGB(240,240,240));//白色字:PUSH状态else if(MouseOver())pDC->SetTextColor(0);//黑色字:热感应状态elsepDC->SetTextColor(::GetSysColor(COLOR_BTNTEXT));//黑色字:普通状态old_brush=pDC->SelectObject(&brush);old_pen =pDC->SelectObject(&pen);pDC->Rectangle(rtControl);pDC->SetBkMode(TRANSPARENT);GetWindowText(strText);pDC->DrawText(strText,rtControl,DT_SINGLELINE|DT_CENTER|DT_VCENTER);if(state & ODS_FOCUS){rtControl.Def
来源:http://www.tulaoshi.com/n/20160129/1484497.html