画按钮.docx
- 文档编号:23800891
- 上传时间:2023-05-20
- 格式:DOCX
- 页数:10
- 大小:17.07KB
画按钮.docx
《画按钮.docx》由会员分享,可在线阅读,更多相关《画按钮.docx(10页珍藏版)》请在冰豆网上搜索。
画按钮
/*-------------RoundButton.cpp--------------*/
//RoundButton.cpp:
implementationfile
//
//RoundButtons!
//
//WrittenbyChrisMaunder(Chris.Maunder@cbr.clw.csiro.au)
//Copyright(c)1997,1998.
//
//Modified:
2Feb1998-Fixvisproblem,CRgnresourceleak,
//buttonrepositioncoderedone.CJM.
//
//Thiscodemaybeusedincompiledforminanywayyoudesire.This
//filemayberedistributedunmodifiedbyanymeansPROVIDINGitis
//notsoldforprofitwithouttheauthorswrittenconsent,and
//providingthatthisnoticeandtheauthorsnameisincluded.If
//thesourcecodeinthisfileisusedinanycommercialapplication
//thenasimpleemailwouldbenice.
//
//Thisfileisprovided"asis"withnoexpressedorimpliedwarranty.
//Theauthoracceptsnoliabilityifitcausesanydamagetoyour
//computer,causesyourpetcattofallill,increasesbaldnessor
//makesyoucarstartemittingstrangenoiseswhenyoustartitup.
//
//Expectbugs.
//
//Pleaseuseandenjoy.Pleaseletmeknowofanybugs/mods/improvements
//thatyouhavefound/implementedandIwillfix/incorporatethemintothis
//file.
//
/////////////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"math.h"
#include"RoundButton.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
//prototypes
COLORREFGetColour(doubledAngle,COLORREFcrBright,COLORREFcrDark);
voidDrawCircle(CDC*pDC,CPointp,LONGlRadius,COLORREFcrColour,BOOLbDashed=FALSE);
voidDrawCircle(CDC*pDC,CPointp,LONGlRadius,COLORREFcrBright,COLORREFcrDark);
//Calculatecolourforapointatthegivenanglebyperformingalinear
//interpolationbetweenthecolourscrBrightandcrDarkbasedonthecosine
//oftheanglebetweenthelightsourceandthepoint.
//
//Anglesaremeasuredfromthe+vex-axis(i.e.(1,0)=0degrees,(0,1)=90degrees)
//Butremember:
+ypointsdown!
COLORREFGetColour(doubledAngle,COLORREFcrBright,COLORREFcrDark)
{
#defineRad2Deg180.0/3.1415
#defineLIGHT_SOURCE_ANGLE-2.356//-2.356radians=-135degrees,i.e.Fromtopleft
ASSERT(dAngle>-3.1416&&dAngle<3.1416);
doubledAngleDifference=LIGHT_SOURCE_ANGLE-dAngle;
if(dAngleDifference<-3.1415)dAngleDifference=6.293+dAngleDifference;
elseif(dAngleDifference>3.1415)dAngleDifference=6.293-dAngleDifference;
doubleWeight=0.5*(cos(dAngleDifference)+1.0);
BYTERed=(BYTE)(Weight*GetRValue(crBright)+(1.0-Weight)*GetRValue(crDark));
BYTEGreen=(BYTE)(Weight*GetGValue(crBright)+(1.0-Weight)*GetGValue(crDark));
BYTEBlue=(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);
returnRGB(Red,Green,Blue);
}
voidDrawCircle(CDC*pDC,CPointp,LONGlRadius,COLORREFcrColour,BOOLbDashed)
{
constintnDashLength=1;
LONGlError,lXoffset,lYoffset;
intnDash=0;
BOOLbDashOn=TRUE;
//Checktoseethatthecoordinatesarevalid
ASSERT((p.x+lRadius<=LONG_MAX)&&(p.y+lRadius<=LONG_MAX));
ASSERT((p.x-lRadius>=LONG_MIN)&&(p.y-lRadius>=LONG_MIN));
//Setstartingvalues
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);
}
//AdvancetheerrortermandtheconstantXaxisstep
lError+=lYoffset++;
//Checktoseeiferrortermhasoverflowed
if((lError+=lYoffset)>=0)
lError-=--lXoffset*2;
if(bDashed&&(++nDash==nDashLength)){
nDash=0;
bDashOn=!
bDashOn;
}
}while(lYoffset<=lXoffset);//Continueuntilhalfwaypoint
}
voidDrawCircle(CDC*pDC,CPointp,LONGlRadius,COLORREFcrBright,COLORREFcrDark)
{
LONGlError,lXoffset,lYoffset;
//Checktoseethatthecoordinatesarevalid
ASSERT((p.x+lRadius<=LONG_MAX)&&(p.y+lRadius<=LONG_MAX));
ASSERT((p.x-lRadius>=LONG_MIN)&&(p.y-lRadius>=LONG_MIN));
//Setstartingvalues
lXoffset=lRadius;
lYoffset=0;
lError=-lRadius;
do{
constdoublePi=3.141592654,
Pi_on_2=Pi*0.5,
Three_Pi_on_2=Pi*1.5;
COLORREFcrColour;
doubledAngle=atan2(lYoffset,lXoffset);
//Drawthecurrentpixel,reflectedacrossalleightarcs
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);
//AdvancetheerrortermandtheconstantXaxisstep
lError+=lYoffset++;
//Checktoseeiferrortermhasoverflowed
if((lError+=lYoffset)>=0)
lError-=--lXoffset*2;
}while(lYoffset<=lXoffset);//Continueuntilhalfwaypoint
}
/////////////////////////////////////////////////////////////////////////////
//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()
/////////////////////////////////////////////////////////////////////////////
//CRoundButtonmessagehandlers
voidCRoundButton:
:
PreSubclassWindow()
{
CButton:
:
PreSubclassWindow();
ModifyStyle(0,BS_OWNERDRAW);
CRectrect;
GetClientRect(rect);
//Resizethewindowtomakeitsquare
rect.bottom=rect.right=min(rect.bottom,rect.right);
//Getthevitalstatisticsofthewindow
m_ptCentre=rect.CenterPoint();
m_nRadius=rect.bottom/2-1;
//Setthewindowregionsomouseclicksonlyactivatetheroundsection
//ofthebutton
m_rgn.DeleteObject();
SetWindowRgn(NULL,FALSE);
m_rgn.CreateEllipticRgnIndirect(rect);
SetWindowRgn(m_rgn,TRUE);
//Convertclientcoordstotheparentsclientcoords
ClientToScreen(rect);
CWnd*pParent=GetParent();
if(pParent)pParent->ScreenToClient(rect);
//Resizethewindow
MoveWindow(rect.left,rect.top,rect.Width(),rect.Height(),TRUE);
}
voidCRoundButton:
:
DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)
{
ASSERT(lpDrawItemStruct!
=NULL);
CDC*pDC=CDC:
:
FromHandle(lpDrawItemStruct->hDC);
CRectrect=lpDrawItemStruct->rcItem;
UINTstate=lpDrawItemStruct->itemState;
UINTnStyle=GetStyle();
intnRadius=m_nRadius;
intnSavedDC=pDC->SaveDC();
pDC->SelectStockObject(NULL_BRUSH);
pDC->FillSolidRect(rect,:
:
GetSysColor(COLOR_BTNFACE));
//Drawthefocuscirclearoundthebutton
if((state&ODS_FOCUS)&&m_bDrawDashedFocusCircle)
DrawCircle(pDC,m_ptCentre,nRadius--,RGB(0,0,0));
//Drawtheraised/sunkenedgesofthebutton(unlessflat)
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));
}
}
//drawthetextifthereisany
CStringstrText;
GetWindowText(strText);
if(!
strText.IsEmpty())
{
CRgnrgn;
rgn.CreateEllipticRgn(m_ptCentre.x-nRadius,m_ptCentre.y-nRadius,
m_ptCentre.x+nRadius,m_ptCentre.y+nRadius);
pDC->SelectClipRgn(&rgn);
CSizeExtent=pDC->GetTextExtent(strText);
CPointpt=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();
}
//Drawthefocuscircleontheinsideofthebutton
if((state&ODS_FOCUS)&&m_bDrawDashedFocusCircle)
DrawCircle(pDC,m_ptCentre,nRadius-2,RGB(0,0,0),TRUE);
pDC->RestoreDC(nSavedDC);
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 按钮