www.gusucode.com > 一个VC++ GUI测试程序-源码程序 > 一个VC++ GUI测试程序-源码程序/code/IconButton.cpp
//Download by http://www.NewXing.com // IconButton.cpp : implementation file // #include "stdafx.h" #include "IconButton.h" #include "DlgBaseClass.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CIconButton CIconButton::CIconButton() { m_nIconSize = 0; m_nStyle = 0; m_cIconBack = 0xFFFFFFFFL; m_fontColor=RGB(255,255,255); m_pFont=new CFont; m_pFont->CreateFont(15,0,0,0,FW_BOLD,0,0,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,"宋体"); m_bBold=FALSE; m_bHasMenu=FALSE; m_nIconIndex=-1; } CIconButton::~CIconButton() { m_pFont->DeleteObject(); delete m_pFont; } BEGIN_MESSAGE_MAP(CIconButton, CButton) //{{AFX_MSG_MAP(CIconButton) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CIconButton message handlers void CIconButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC dc; dc.Attach(lpDrawItemStruct->hDC); CRect rt,rc(lpDrawItemStruct->rcItem); UINT uStyle = DFCS_BUTTONPUSH; // If drawing selected, add the pushed style to DrawFrameControl. if (lpDrawItemStruct->itemState & ODS_SELECTED) uStyle |= DFCS_PUSHED; // Draw the button frame. //dc.DrawFrameControl(&rc, DFC_BUTTON, uStyle); BOOL bPushed=(uStyle & DFCS_PUSHED); CBrush b1(RGB(255,255,255)); CBrush b2(RGB(85,170,255)); CBrush b3(RGB(43,86,129)); CBrush b4(RGB(0,0,0)); CBrush b5(RGB(11,22,33)); //Draw center background CBrush bc(lpDrawItemStruct->itemState & ODS_DISABLED?RGB(64,128,192):RGB(56,112,168)); rt=rc; rt.InflateRect(-2,-2); dc.FillRect(&rt,&bc); //Draw border if(lpDrawItemStruct->itemState & ODS_FOCUS){//focused if(bPushed){ rt=rc; dc.FrameRect(rt,&b5); rt.DeflateRect(1,1); dc.FrameRect(rt,&b3); } else{// normal focus rt=rc; rt.OffsetRect(2,2); dc.FrameRect(rt,&b2); rt.OffsetRect(-4,-4); dc.FrameRect(rt,&b3); rt.OffsetRect(3,3); dc.FrameRect(rt,&b1); rt.OffsetRect(-2,-2); dc.FrameRect(rt,&b4); rt.OffsetRect(1,1); dc.FrameRect(rt,&b5); } rt=rc; rt.DeflateRect(4,4); dc.DrawFocusRect(rt); } else{//not focused rt=rc; rt.OffsetRect(1,1); dc.FrameRect(rt,&b2); rt.OffsetRect(-2,-2); dc.FrameRect(rt,&b3); rt.OffsetRect(1,1); dc.FrameRect(rt,&b1); rt.InflateRect(1,1); rt.OffsetRect(-1,-1); dc.FrameRect(rt,&b4); } CPoint ptIcon; CRect rcText; CString str; GetWindowText(str); CSize sz=dc.GetTextExtent(str); //Draw image & text dc.SetTextColor((lpDrawItemStruct->itemState & ODS_DISABLED)?RGB(192,192,192):m_fontColor); CFont *pFont=NULL; if(m_bBold) pFont=dc.SelectObject(m_pFont); dc.SetBkMode(TRANSPARENT); int off; switch(m_nStyle){ case ICON_NONE: rt=rc; if(bPushed) rt.OffsetRect(1,1); dc.DrawText(str,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE); break; case ICON_HORZ: ptIcon=rc.TopLeft(); off=(rc.Height()-m_nIconSize)/2; ptIcon.Offset(off+2,off); if(bPushed) ptIcon.Offset(1,1); if(m_cIconBack!=0xFF000000L){ CRect r(ptIcon.x,ptIcon.y,ptIcon.x+12,ptIcon.y+12); r.OffsetRect(6,7); CBrush b(m_cIconBack); dc.FillRect(r,&b); b.DeleteObject(); } else CDlgBaseClass::DrawIcon(&dc,m_nIconIndex,&ptIcon); rcText=rc; rcText.left+=m_nIconSize+off+4; if(bPushed)rcText.OffsetRect(1,1); if(str!="") dc.DrawText(str,rcText,DT_CENTER|DT_VCENTER|DT_SINGLELINE); if(m_bHasMenu){ ptIcon=rcText.BottomRight(); ptIcon.Offset(-8,-14); COLORREF clr(RGB(255,255,255)); int x=ptIcon.x,y=ptIcon.y; for(int i=0;i<3;i++){ for(int j=i;j<5-i;j++){ dc.SetPixel(x+i,y+j,clr); } } } break; case ICON_VERT: if(str!=""){ int gap=(rc.Height()-m_nIconSize-15)/3; ptIcon=rc.TopLeft(); ptIcon.Offset((rc.Width()-m_nIconSize)/2,gap); if(bPushed) ptIcon.Offset(1,1); CDlgBaseClass::DrawIcon(&dc,m_nIconIndex,&ptIcon); rcText=rc; rcText.top+=gap+m_nIconSize; if(bPushed)rcText.OffsetRect(1,1); dc.DrawText(str,rcText,DT_CENTER|DT_VCENTER|DT_SINGLELINE); } else{ ptIcon=rc.TopLeft(); ptIcon.Offset((rc.Width()-(int)m_nIconSize)/2,(rc.Height()-(int)m_nIconSize)/2); if(bPushed) ptIcon.Offset(1,1); CDlgBaseClass::DrawIcon(&dc,m_nIconIndex,&ptIcon); } break; } b1.DeleteObject(); b2.DeleteObject(); b3.DeleteObject(); b4.DeleteObject(); b5.DeleteObject(); bc.DeleteObject(); if(pFont)dc.SelectObject(pFont); dc.Detach(); } void CIconButton::SetIconSize(int nIconIndex,LONG nStyle,LONG nSize,DWORD ibg) { m_nIconIndex=nIconIndex; m_nIconSize = nSize; m_nStyle = nStyle; m_cIconBack=ibg; if(m_cIconBack!=0xFF000000L){ CRect rc; GetClientRect(rc); CPoint ptIcon=rc.TopLeft(); int off=(rc.Height()-m_nIconSize)/2; ptIcon.Offset(off+2,off); CRect r(ptIcon.x,ptIcon.y,ptIcon.x+12,ptIcon.y+12); r.OffsetRect(6,7); InvalidateRect(r,FALSE); } else{ Invalidate(FALSE); } UpdateWindow(); } void CIconButton::SetFontColor(COLORREF clr) { m_fontColor=clr; Invalidate(FALSE); UpdateWindow(); } void CIconButton::SetFontBold(BOOL bBold) { m_bBold=bBold; Invalidate(FALSE); UpdateWindow(); } void CIconButton::SetHasMenu() { m_bHasMenu=TRUE; }