3926|5

2

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

CroundButton [复制链接]

刚刚看了个CroundButton类,注释不够清楚,图像处理的函数一点都没头绪
在这里个大家贴出来,有心人给我理理来龙去脉哈
 
____________________________________________________________________________________________________
____________________________________________________________________________________________________
#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
//
// Round buttons!
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Copyright (c) 1998.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in  this file is used in any commercial application
// then a simple email woulod be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CRoundButton window
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_)
__________________________________________________________________________________________________
__________________________________________________________________________________________________
 
// RoundButton.cpp : implementation file
//
// Round Buttons!
//
// Written by Chris Maunder (chrismaunder@codeguru.com)
// Copyright (c) 1997,1998.
//
// Modified: 2 Feb 1998 - Fix vis problem, CRgn resource leak,
//                        button reposition code redone. CJM.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is
// not sold for profit without the authors written consent, and
// providing that this notice and the authors name is included. If
// the source code in this file is used in any commercial application
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage to your
// computer, causes your pet cat to fall ill, increases baldness or
// makes you car start emitting strange noises when you start it up.
//
// Expect bugs.
//
// Please use and enjoy. Please let me know of any bugs/mods/improvements
// that you have found/implemented and I will fix/incorporate them into this
// file.
//
/////////////////////////////////////////////////////////////////////////////
#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();
}
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);
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________
重点讲解cpp中的函数的来龙去脉啊
 
 

最新回复

别急大家一起分析  详情 回复 发表于 2011-2-14 11:34
点赞 关注

回复
举报

2

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
:加上字母代表表情!:D
 
 

回复

6066

帖子

92

TA的资源

裸片初长成(初级)

板凳
 

回复 沙发 小韩 的帖子

有懂的朋友帮帮小韩看看这个函数吧!可能看上去有点复杂!
 
 
 

回复

888

帖子

3

TA的资源

五彩晶圆(初级)

4
 
很想帮你回答呀,可是你这个问题太重量级了,我不懂!给你顶一下!
 
个人签名邮箱:ternence.hsu@foxmail.com
 
 

回复

2735

帖子

0

TA的资源

一粒金砂(中级)

5
 
看地眼晕,理解的帮忙给解释下
 
 
 

回复

2131

帖子

0

TA的资源

至上芯片

6
 

回复 楼主 小韩 的帖子

别急大家一起分析
 
个人签名处处留心皆学问!
 
 

回复
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表