求助!创建新类Round1Button(100分)

  • 主题发起人 主题发起人 陈莹
  • 开始时间 开始时间

陈莹

Unregistered / Unconfirmed
GUEST, unregistred user!
请各位帮我!在书上看到的一个例子,但看不懂!
建立一个基于对话框的工程。
(1)添加类
name:Round1Button
baseClass:Cbutton
这步没什么问题
(2)向类Round1Button添加函数GetColour,来获取当前窗口的颜色,
代码我就不列出了,因为这部分也没什么问题。
(3)向类Round1Button添加两个重载成员函数DrawCircle.
问题是为什么要两个重载成员函数DrawCircle?
void CRound1Button::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;
//校验
ASSERT((p.x+lRadius<=LONG_MAX)&amp;&amp;(p.y+lRadius<=LONG_MAX));
ASSERT((p.x-lRadius<=LONG_MIN)&amp;&amp;(p.y-lRadius<=LONG_MIN));
//
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);
}
//错误控制
lError+=lYoffset++;
//若发生错误
if((lError+=lYoffset)>=0)
lError-=--lXoffset*2;
if(bDashed&amp;&amp;(++nDash==nDashLength)){
nDash=0;
bDashOn!=bDashOn;
}
}while(lYoffset<=lXoffset);
}
void CRound1Button::DrawCircle(CDC *pDC, CPoint p, LONG lRadius, COLORREF crBright, COLORREF crDark)
{
long lError,lXoffset,lYoffset;
//校验
ASSERT((p.x+lRadius<=LONG_MAX)&amp;&amp;(p.y+lRadius<=LONG_MAX));
ASSERT((p.x-lRadius<=LONG_MIN)&amp;&amp;(p.y-lRadius<=LONG_MIN));
//
lXoffset=lRadius;
lYoffset=0;
lError=-lRadius;
do{
constdo
uble Pi=3.141592654,
Pi_on_2=Pi*0.5,
Three_Pi_on_2=Pi*1.5;
COLORREF crColour;
double dAngle=atan2(lYoffset,lXoffset);
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+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-lXoffset,p.y+lYoffset,crColour);
crColour=GetColour(Pi_on_2+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_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);
//
lError+=lYoffset++;
if((lError+=lYoffset)>=0)
lError-=--lXoffset*2;
}while(lYoffset<=lXoffset);
}
(4)CRound1Button添加成员函数DrawItem和PreSubclassWindow()。
问题是m_nRadius和m_bDrawDashedFocusCircle是什么?
void CRound1Button::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
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));
//
//
if((state&amp;ODS_FOCUS)&amp;&amp;m_bDrawDashedFocusCircle)
{
DrawCircle(pDC,m_ptCentre,nRadius--,RGB(255,0,0));
}
//
if(nStyle&amp;BS_FLAT){
DrawCircle(pDC,m_ptCentre,nRadius--,RGB(255,0,0));
DrawCircle(pDC,m_ptCentre,nRadius--,::GetSysColor(COLOR_3DHIGHLIGHT));
}
else
{
if((state&amp;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(3DHIGHLIGHT),
::GetSysColor(COLOR_3DDKSHADOW));
DrawCircle(pDC,m_ptCentre,nRadius--,::GetSysColor(COLOR_3DLIGHT),
::GetSysColor(COLOR_3DSHADOW));
}
}
//
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(&amp;rgn);
CSize Extent=pDC->GetTextExtent(strText);
CPoint pt=CPoint(m_ptCentre.x-Extent.cx/2,
m_ptCentre.x-Extent.cy/2);
if(state&amp;ODS_SELECTED)pt.Offset(1,1);
pDC->SetBkMode(TRANSPARENT);
if(state&amp;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();
}
//
if((state&amp;ODS_FOCUS)&amp;&amp;m_bDrawDashedFocusCircle)
DrawCircle(pDC,m_ptCentre,nRadius-2,RGB(0,0,0),TRUE);
pDC->RestoreDC(nSavedDC);


}
void CRound1Button::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class

CButton::PreSubclassWindow();
ModifyStyle(0,BS_OWNERDRAW);
CRect rect;
GetClientRect(rect);
rect.bottom=rect.right=min(rect.bottom,rect.right);
//
m_ptCentre=rect.CenterPoint();
m_nRadius=rect.bottom/2-1;
m_rgn.DeleteObject();
SetWindowRgn(NULL,FALSE);
m_rgn.CreateEllipticRgnIndirect(rect);
SetWindowRgn(m_rgn,TRUE);
ClienToScreen(rect);
Cwnd* pParent=GetParent();
if(pParent)pParent->ScreenToClient(rect);
MoveWindow(rect.left,rect.top,rect.Width(),rect.Height(),TRUE);
}
 
后退
顶部