VC实现按钮的3D效果
来源:优易学  2011-12-9 21:14:19   【优易学:中国教育考试门户网】   资料下载   IT书店
  运行AppWizard生成一个基于对话框的test工程,在对话框中加入一个CButton控件。在CButton控件的General属性页将控件的 ID改为IDC_3DTEXTBTN,Caption改为“谁与争疯”,在控件Styles属性页选中OwnerDraw,其余设置保持默认。
  用classwizard创建一个新类:C3dTextButton,基类为CButton。为C3dTextButton类添加一个protected 的函数void Draw(CDC* pDC, const CRect& rect, UINT state)。如下所示编写代码:
  void C3dTextButton::Draw(CDC *pDC, const CRect &rect, UINT state)
  {
  CString text; GetWindowText(text);
  int l=text.GetLength();
  CRect rectClient=rect;
  //获得控件的字体
  CFont* pFont=GetFont();
  //确定所选字体有效高度和宽度
  LOGFONT logfont;
  pFont->GetObject(sizeof(LOGFONT),&logfont);
  if(logfont.lfHeight==0)logfont.lfHeight=20;
  logfont.lfWidth=0;//宽度设为0,宽度值由高度确定
  logfont.lfWeight=1000;
  logfont.lfEscapement=logfont.lfOrientation=0;
  CFont tryfont; VERIFY(tryfont.CreateFontIndirect(&logfont));
  CFont* pFontOld=pDC->SelectObject(&tryfont);
  //根据控件大小,调整字体的高度,使文本与控件协调
  CSize textSizeClient=pDC->GetTextExtent(text,l);
  if(rectClient.Width()*textSizeClient.cy>rectClient.Height()*textSizeClient.cx)
  {
  logfont.lfHeight=::MulDiv(logfont.lfHeight,rectClient.Height(),textSizeClient.cy);
  }
  else{
  logfont.lfHeight = ::MulDiv(logfont.lfHeight,rectClient.Width(),textSizeClient.cx);
  }
  //创建并选择协调后的字体
  CFont font; font.CreateFontIndirect(&logfont);
  pDC->SelectObject(&font);
  textSizeClient=pDC->GetTextExtent(text,l);
  //确定文本与控件边界的距离minx,miny
  int minx=rectClient.left+(rectClient.Width()-textSizeClient.cx)/2;
  int miny=rectClient.top+(rectClient.Height()-textSizeClient.cy)/2;
  int oldBkMode=pDC->SetBkMode(TRANSPARENT);
  COLORREF textcol=::GetSysColor(COLOR_BTNTEXT);
  COLORREF oldTextColor=pDC->SetTextColor(textcol);
  int cx = minx;
  int cy = miny;
  int s=(state&ODS_SELECTED)?-1:+1;
  cx+= 3; cy+= 3;
  //实现3D效果
  pDC->SetTextColor(::GetSysColor(COLOR_3DDKSHADOW));
  pDC->TextOut(cx-s*2,cy+s*2,text);
  pDC->TextOut(cx+s*2,cy-s*2,text);
  pDC->TextOut(cx+s*2,cy+s*2,text);
  pDC->SetTextColor(::GetSysColor(COLOR_3DHILIGHT));
  pDC->TextOut(cx+s*1,cy-s*2,text);
  pDC->TextOut(cx-s*2,cy+s*1,text);
  pDC->TextOut(cx-s*2,cy-s*2,text);
  pDC->SetTextColor(::GetSysColor(COLOR_3DSHADOW));
  pDC->TextOut(cx-s*1,cy+s*1,text);
  pDC->TextOut(cx+s*1,cy-s*1,text);
  pDC->TextOut(cx+s*1,cy+s*1,text);
  pDC->SetTextColor(::GetSysColor(COLOR_3DLIGHT));
  pDC->TextOut(cx,cy-s*1,text);
  pDC->TextOut(cx-s*1,cy,text);
  pDC->TextOut(cx-s*1,cy-s*1,text);
  pDC->SetTextColor(textcol);
  //输出标题
  pDC->TextOut(cx,cy,text);
  //恢复设备描述表
  pDC->SetTextColor(oldTextColor);
  pDC->SetBkMode(oldBkMode);
  pDC->SelectObject(pFontOld);
  }
  用classwizard重载C3dTextButton类的DrawItem函数。编写代码如下所示:
  void C3dTextButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
  {
  CDC* pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
  ASSERT_VALID(pDC);
  CRect rectClient=lpDrawItemStruct->rcItem;
  Draw(pDC,rectClient,lpDrawItemStruct->itemState);
  }
  用classwizard为IDC_3DTEXTBTN建立一个C3dTextButton控件变量m_3dTextButton1。
  把“3dTextButton.h”加入testDlg头文件。编译并测试应用程序。OK!看看效果吧。

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训