CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  界面

如何显示一个圆形的按钮,多谢:)最好有源代码

楼主reverie128(清风)2003-09-04 10:49:54 在 VC/MFC / 界面 提问

如何显示一个圆形的按钮,多谢:)最好有源代码 问题点数:60、回复次数:4Top

1 楼papaya_stone(^_^)shentong(^_^)回复于 2003-09-04 11:36:08 得分 20

http://www.vckbase.com/code/listcode.asp?mclsid=3&sclsid=301&page=3Top

2 楼lif2000(阿飞)回复于 2003-09-04 19:59:23 得分 40

 
  //*.cpp  
  #include   "stdafx.h"  
  #include   "math.h"  
  #include   "RoundButton.h"  
   
  #ifdef   _DEBUG  
  #define   new   DEBUG_NEW  
  #undef   THIS_FILE  
  static   char   THIS_FILE[]   =   __FILE__;  
  #endif  
   
  //   prototypes  
  COLORREF   GetColour(double   dAngle,   COLORREF   crBright,   COLORREF   crDark);  
  void   DrawCircle(CDC*   pDC,   CPoint   p,   LONG   lRadius,   COLORREF   crColour,   BOOL   bDashed   =   FALSE);  
  void   DrawCircle(CDC*   pDC,   CPoint   p,   LONG   lRadius,   COLORREF   crBright,   COLORREF   crDark);  
   
   
  //   Calculate   colour   for   a   point   at   the   given   angle   by   performing   a   linear  
  //   interpolation   between   the   colours   crBright   and   crDark   based   on   the   cosine  
  //   of   the   angle   between   the   light   source   and   the   point.  
  //  
  //   Angles   are   measured   from   the   +ve   x-axis   (i.e.   (1,0)   =   0   degrees,   (0,1)   =   90   degrees   )  
  //   But   remember:   +y   points   down!  
   
  COLORREF   GetColour(double   dAngle,   COLORREF   crBright,   COLORREF   crDark)  
  {  
  #define   Rad2Deg 180.0/3.1415    
  #define   LIGHT_SOURCE_ANGLE -2.356 //   -2.356   radians   =   -135   degrees,   i.e.   From   top   left  
   
  ASSERT(dAngle   >   -3.1416   &&   dAngle   <   3.1416);  
  double   dAngleDifference   =   LIGHT_SOURCE_ANGLE   -   dAngle;  
   
  if   (dAngleDifference   <   -3.1415)   dAngleDifference   =   6.293   +   dAngleDifference;  
  else   if   (dAngleDifference   >   3.1415)   dAngleDifference   =   6.293   -   dAngleDifference;  
   
  double   Weight   =   0.5*(cos(dAngleDifference)+1.0);  
   
  BYTE   Red       =   (BYTE)   (Weight*GetRValue(crBright)   +   (1.0-Weight)*GetRValue(crDark));  
  BYTE   Green   =   (BYTE)   (Weight*GetGValue(crBright)   +   (1.0-Weight)*GetGValue(crDark));  
  BYTE   Blue     =   (BYTE)   (Weight*GetBValue(crBright)   +   (1.0-Weight)*GetBValue(crDark));  
   
  //TRACE("LightAngle   =   %0.0f,   Angle   =   %3.0f,   Diff   =   %3.0f,   Weight   =   %0.2f,   RGB   %3d,%3d,%3d\n",    
  //     LIGHT_SOURCE_ANGLE*Rad2Deg,   dAngle*Rad2Deg,   dAngleDifference*Rad2Deg,   Weight,Red,Green,Blue);  
   
  return   RGB(Red,   Green,   Blue);  
  }  
   
  void   DrawCircle(CDC*   pDC,   CPoint   p,   LONG   lRadius,   COLORREF   crColour,   BOOL   bDashed)  
  {  
  const   int   nDashLength   =   1;  
  LONG   lError,   lXoffset,   lYoffset;  
  int     nDash   =   0;  
  BOOL   bDashOn   =   TRUE;  
   
  //Check   to   see   that   the   coordinates   are   valid  
  ASSERT(   (p.x   +   lRadius   <=   LONG_MAX)   &&   (p.y   +   lRadius   <=   LONG_MAX)   );  
  ASSERT(   (p.x   -   lRadius   >=   LONG_MIN)   &&   (p.y   -   lRadius   >=   LONG_MIN)   );  
   
  //Set   starting   values  
  lXoffset   =   lRadius;  
  lYoffset   =   0;  
  lError       =   -lRadius;  
   
  do   {  
  if   (bDashOn)   {  
  pDC->SetPixelV(p.x   +   lXoffset,   p.y   +   lYoffset,   crColour);  
  pDC->SetPixelV(p.x   +   lXoffset,   p.y   -   lYoffset,   crColour);  
  pDC->SetPixelV(p.x   +   lYoffset,   p.y   +   lXoffset,   crColour);  
  pDC->SetPixelV(p.x   +   lYoffset,   p.y   -   lXoffset,   crColour);  
  pDC->SetPixelV(p.x   -   lYoffset,   p.y   +   lXoffset,   crColour);  
  pDC->SetPixelV(p.x   -   lYoffset,   p.y   -   lXoffset,   crColour);  
  pDC->SetPixelV(p.x   -   lXoffset,   p.y   +   lYoffset,   crColour);  
  pDC->SetPixelV(p.x   -   lXoffset,   p.y   -   lYoffset,   crColour);  
  }  
   
  //Advance   the   error   term   and   the   constant   X   axis   step  
  lError   +=   lYoffset++;  
   
  //Check   to   see   if   error   term   has   overflowed  
  if   ((lError   +=   lYoffset)   >=   0)  
  lError   -=   --lXoffset   *   2;  
   
  if   (bDashed   &&   (++nDash   ==   nDashLength))   {  
  nDash   =   0;  
  bDashOn   =   !bDashOn;  
  }  
   
  }   while   (lYoffset   <=   lXoffset); //Continue   until   halfway   point  
  }    
   
  void   DrawCircle(CDC*   pDC,   CPoint   p,   LONG   lRadius,   COLORREF   crBright,   COLORREF   crDark)  
  {  
  LONG   lError,   lXoffset,   lYoffset;  
   
  //Check   to   see   that   the   coordinates   are   valid  
  ASSERT(   (p.x   +   lRadius   <=   LONG_MAX)   &&   (p.y   +   lRadius   <=   LONG_MAX)   );  
  ASSERT(   (p.x   -   lRadius   >=   LONG_MIN)   &&   (p.y   -   lRadius   >=   LONG_MIN)   );  
   
  //Set   starting   values  
  lXoffset   =   lRadius;  
  lYoffset   =   0;  
  lError       =   -lRadius;  
   
  do   {  
  const   double   Pi   =   3.141592654,    
    Pi_on_2   =   Pi   *   0.5,  
    Three_Pi_on_2   =   Pi   *   1.5;  
  COLORREF   crColour;  
  double       dAngle   =   atan2(lYoffset,   lXoffset);  
   
  //Draw   the   current   pixel,   reflected   across   all   eight   arcs  
  crColour   =   GetColour(dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   +   lXoffset,   p.y   +   lYoffset,   crColour);  
   
  crColour   =   GetColour(Pi_on_2   -   dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   +   lYoffset,   p.y   +   lXoffset,   crColour);  
   
  crColour   =   GetColour(Pi_on_2   +   dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   -   lYoffset,   p.y   +   lXoffset,   crColour);  
   
  crColour   =   GetColour(Pi   -   dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   -   lXoffset,   p.y   +   lYoffset,   crColour);  
   
  crColour   =   GetColour(-Pi   +   dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   -   lXoffset,   p.y   -   lYoffset,   crColour);  
   
  crColour   =   GetColour(-Pi_on_2   -   dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   -   lYoffset,   p.y   -   lXoffset,   crColour);  
   
  crColour   =   GetColour(-Pi_on_2   +   dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   +   lYoffset,   p.y   -   lXoffset,   crColour);  
   
  crColour   =   GetColour(-dAngle,   crBright,   crDark);  
  pDC->SetPixelV(p.x   +   lXoffset,   p.y   -   lYoffset,   crColour);  
   
  //Advance   the   error   term   and   the   constant   X   axis   step  
  lError   +=   lYoffset++;  
   
  //Check   to   see   if   error   term   has   overflowed  
  if   ((lError   +=   lYoffset)   >=   0)  
  lError   -=   --lXoffset   *   2;  
   
  }   while   (lYoffset   <=   lXoffset); //Continue   until   halfway   point  
  }    
   
   
  /////////////////////////////////////////////////////////////////////////////  
  //   CRoundButton  
   
  CRoundButton::CRoundButton()  
  {  
  m_bDrawDashedFocusCircle   =   TRUE;  
  }  
   
  CRoundButton::~CRoundButton()  
  {  
  m_rgn.DeleteObject();  
  }  
  Top

3 楼lif2000(阿飞)回复于 2003-09-04 19:59:34 得分 0

 
  BEGIN_MESSAGE_MAP(CRoundButton,   CButton)  
  //{{AFX_MSG_MAP(CRoundButton)  
  //}}AFX_MSG_MAP  
  END_MESSAGE_MAP()  
   
  /////////////////////////////////////////////////////////////////////////////  
  //   CRoundButton   message   handlers  
   
  void   CRoundButton::PreSubclassWindow()    
  {  
  CButton::PreSubclassWindow();  
   
  ModifyStyle(0,   BS_OWNERDRAW);  
   
  CRect   rect;  
  GetClientRect(rect);  
   
  //   Resize   the   window   to   make   it   square  
  rect.bottom   =   rect.right   =   min(rect.bottom,rect.right);  
   
  //   Get   the   vital   statistics   of   the   window  
  m_ptCentre   =   rect.CenterPoint();  
  m_nRadius     =   rect.bottom/2-1;  
   
  //   Set   the   window   region   so   mouse   clicks   only   activate   the   round   section    
  //   of   the   button  
  m_rgn.DeleteObject();    
  SetWindowRgn(NULL,   FALSE);  
  m_rgn.CreateEllipticRgnIndirect(rect);  
  SetWindowRgn(m_rgn,   TRUE);  
   
  //   Convert   client   coords   to   the   parents   client   coords  
  ClientToScreen(rect);  
  CWnd*   pParent   =   GetParent();  
  if   (pParent)   pParent->ScreenToClient(rect);  
   
  //   Resize   the   window  
  MoveWindow(rect.left,   rect.top,   rect.Width(),   rect.Height(),   TRUE);  
  }  
   
  void   CRoundButton::DrawItem(LPDRAWITEMSTRUCT   lpDrawItemStruct)    
  {  
  ASSERT(lpDrawItemStruct   !=   NULL);  
   
  CDC*   pDC       =   CDC::FromHandle(lpDrawItemStruct->hDC);  
  CRect   rect   =   lpDrawItemStruct->rcItem;  
  UINT   state   =   lpDrawItemStruct->itemState;  
  UINT   nStyle   =   GetStyle();  
  int   nRadius   =   m_nRadius;  
   
  int   nSavedDC   =   pDC->SaveDC();  
   
  pDC->SelectStockObject(NULL_BRUSH);  
  pDC->FillSolidRect(rect,   ::GetSysColor(COLOR_BTNFACE));  
   
  //   Draw   the   focus   circle   around   the   button  
  if   ((state   &   ODS_FOCUS)   &&   m_bDrawDashedFocusCircle)  
  DrawCircle(pDC,   m_ptCentre,   nRadius--,   RGB(0,0,0));  
   
  //   Draw   the   raised/sunken   edges   of   the   button   (unless   flat)  
  if   (nStyle   &   BS_FLAT)   {  
  //DrawCircle(pDC,   m_ptCentre,   nRadius--,   RGB(0,0,0));  
  //DrawCircle(pDC,   m_ptCentre,   nRadius--,   ::GetSysColor(COLOR_3DHIGHLIGHT));  
  }   else   {  
  if   ((state   &   ODS_SELECTED)) {  
  //DrawCircle(pDC,   m_ptCentre,   nRadius--,    
        //::GetSysColor(COLOR_3DDKSHADOW),   ::GetSysColor(COLOR_3DHIGHLIGHT));  
  //DrawCircle(pDC,   m_ptCentre,   nRadius--,    
      //   ::GetSysColor(COLOR_3DSHADOW),   ::GetSysColor(COLOR_3DLIGHT));  
  }   else   {  
  DrawCircle(pDC,   m_ptCentre,   nRadius--,    
        ::GetSysColor(COLOR_3DHIGHLIGHT),   ::GetSysColor(COLOR_3DDKSHADOW));  
  DrawCircle(pDC,   m_ptCentre,   nRadius--,    
        ::GetSysColor(COLOR_3DLIGHT),   ::GetSysColor(COLOR_3DSHADOW));  
  }  
  }  
   
  //   draw   the   text   if   there   is   any  
  CString   strText;  
  GetWindowText(strText);  
   
  if   (!strText.IsEmpty())  
  {  
  CRgn   rgn;  
  rgn.CreateEllipticRgn(m_ptCentre.x-nRadius,   m_ptCentre.y-nRadius,    
      m_ptCentre.x+nRadius,   m_ptCentre.y+nRadius);  
  pDC->SelectClipRgn(&rgn);  
   
  CSize   Extent   =   pDC->GetTextExtent(strText);  
  CPoint   pt   =   CPoint(   m_ptCentre.x   -   Extent.cx/2,   m_ptCentre.x   -   Extent.cy/2   );  
   
  if   (state   &   ODS_SELECTED)   pt.Offset(1,1);  
   
  pDC->SetBkMode(TRANSPARENT);  
   
  if   (state   &   ODS_DISABLED)  
  pDC->DrawState(pt,   Extent,   strText,   DSS_DISABLED,   TRUE,   0,   (HBRUSH)NULL);  
  else  
  pDC->TextOut(pt.x,   pt.y,   strText);  
   
  pDC->SelectClipRgn(NULL);  
  rgn.DeleteObject();  
  }  
   
  //   Draw   the   focus   circle   on   the   inside   of   the   button  
  if   ((state   &   ODS_FOCUS)   &&   m_bDrawDashedFocusCircle)  
  //DrawCircle(pDC,   m_ptCentre,   nRadius-2,   RGB(0,0,0),   TRUE);  
   
  pDC->RestoreDC(nSavedDC);  
  }  
   
   
   
   
   
  #if   !defined(AFX_ROUNDBUTTON_H__5254170E_59CF_11D1_ABBA_00A0243D1382__INCLUDED_)  
  #define   AFX_ROUNDBUTTON_H__5254170E_59CF_11D1_ABBA_00A0243D1382__INCLUDED_  
   
  #if   _MSC_VER   >=   1000  
  #pragma   once  
  #endif   //   _MSC_VER   >=   1000  
  //   RoundButton.h   :   header   file  
   
  class   CRoundButton   :   public   CButton  
  {  
  //   Construction  
  public:  
  CRoundButton();  
   
  //   Attributes  
  public:  
   
  //   Operations  
  public:  
   
  //   Overrides  
  //   ClassWizard   generated   virtual   function   overrides  
  //{{AFX_VIRTUAL(CRoundButton)  
  public:  
  virtual   void   DrawItem(LPDRAWITEMSTRUCT   lpDrawItemStruct);  
  protected:  
  virtual   void   PreSubclassWindow();  
  //}}AFX_VIRTUAL  
   
  //   Implementation  
  public:  
  virtual   ~CRoundButton();  
   
  CRgn       m_rgn;  
  CPoint   m_ptCentre;  
  int         m_nRadius;  
  BOOL       m_bDrawDashedFocusCircle;  
   
  //   Generated   message   map   functions  
  protected:  
  //{{AFX_MSG(CRoundButton)  
  //}}AFX_MSG  
   
  DECLARE_MESSAGE_MAP()  
  };  
   
  /////////////////////////////////////////////////////////////////////////////  
   
  //{{AFX_INSERT_LOCATION}}  
  //   Microsoft   Developer   Studio   will   insert   additional   declarations   immediately   before   the   previous   line.  
   
  #endif   //   !defined(AFX_ROUNDBUTTON_H__5254170E_59CF_11D1_ABBA_00A0243D1382__INCLUDED_)  
   
   
   
  Top

4 楼lif2000(阿飞)回复于 2003-09-04 20:01:22 得分 0

吧上面两个回复是一个按钮类,包括.cpp,.h文件,复制下来到Vc中试一下Top

相关问题

  • 如何用SpeedButton做一个椭圆形按钮,最好有源代码
  • 中秋姐问题: 如何将一个按钮变成圆形的??(附源代码)
  • 这是源代码,请指点有什么不妥,多谢!!
  • ★★★那位仁兄有Apache的源代码?小弟急用!多谢!!
  • 留言版'提交'按钮的源代码哪位有,急用!!!
  • 求带关闭按钮的TabControl控件(要求有源代码)!
  • 求MFC WINDOWS程序设计第二版随书源代码,多谢!
  • 请问各位兄弟,在哪能找到加密算法DES,RSA的源代码,多谢多谢????
  • 哪里有背景透明的VB按钮源代码(SSCOMMAND有BUG除外)?急。
  • 谁提供自画 单选按钮 复选按钮 控件 的源代码(100分)

关键词

  • axis
  • crcolour
  • lyoffset
  • lxoffset
  • crbright
  • crdark
  • setpixelv
  • getcolour
  • lradius
  • dangle

得分解答快速导航

  • 帖主:reverie128
  • papaya_stone
  • lif2000

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo