700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > MFC自制进度条

MFC自制进度条

时间:2022-02-27 06:55:50

相关推荐

MFC自制进度条

改变进度条的颜色/articles/1454.shtml

使用方法

1.将三个文件添加到工程

2.在对话框上添加一个进度条;

新建一个类CGradientProgressCtrl,其基类为CProgressCtrl

3.为进度条添加变量,变量类型CGradientProgressCtrl,否则运行不成功

添加方法:进度条右键-->添加变量

添加变量后,系统会自动在.h文件中添加变量定义CGradientProgressCtrl progress;

在.c文件中的DoDataExchange函数中进行控件绑定并添加关联DDX_Control代码

4.

应用:

m_process1.SetRange(0, 100);

m_process1.ShowPercent(TRUE);

m_process1.SetStartColor(RGB(0, 0, 255));

m_process1.SetEndColor(RGB(255, 255, 0));

m_process1.SetTextColor(RGB(0, 0, 255));

m_process1.SetBkColor(RGB(125, 125, 125));

m_process1.SetPos(50);//设置百分比

5、其中GradientProgressCtrl.h

[cpp] view plain copy #if!defined(AFX_ENHPROGRESSCTRL_H__12909D73_C393_11D1_9FAE_8192554015AD__INCLUDED_) #defineAFX_ENHPROGRESSCTRL_H__12909D73_C393_11D1_9FAE_8192554015AD__INCLUDED_ #if_MSC_VER>=1000 #pragmaonce #endif//_MSC_VER>=1000 //EnhProgressCtrl.h:headerfile //GradientProgressCtrl.h:headerfile #include"MemDC.h" / //CGradientProgressCtrlwindow classCGradientProgressCtrl:publicCProgressCtrl { //Construction public: CGradientProgressCtrl(); //Attributes public: //Attributes voidSetRange(intnLower,intnUpper); voidSetRange32(intnLower,intnUpper); intSetPos(intnPos); intSetStep(intnStep); intStepIt(void); //Operations public: //SetFunctions voidSetTextColor(COLORREFcolor){m_clrText=color;} voidSetBkColor(COLORREFcolor){m_clrBkGround=color;} voidSetStartColor(COLORREFcolor){m_clrStart=color;} voidSetEndColor(COLORREFcolor){m_clrEnd=color;} //Showthepercentcaption voidShowPercent(BOOLbShowPercent=TRUE){m_bShowPercent=bShowPercent;} //GetFunctions COLORREFGetTextColor(void){returnm_clrText;} COLORREFGetBkColor(void){returnm_clrBkGround;} COLORREFGetStartColor(void){returnm_clrStart;} COLORREFGetEndColor(void){returnm_clrEnd;} //Overrides //ClassWizardgeneratedvirtualfunctionoverrides //{{AFX_VIRTUAL(CGradientProgressCtrl) protected: //}}AFX_VIRTUAL //Implementation public: virtual~CGradientProgressCtrl(); //Generatedmessagemapfunctions protected: voidDrawGradient(CPaintDC*pDC,constRECT&rectClient,constint&nMaxWidth,constBOOL&bVertical); intm_nLower,m_nUpper,m_nStep,m_nCurrentPosition; COLORREFm_clrStart,m_clrEnd,m_clrBkGround,m_clrText; BOOLm_bShowPercent; //{{AFX_MSG(CGradientProgressCtrl) afx_msgvoidOnPaint(); afx_msgBOOLOnEraseBkgnd(CDC*pDC); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; / //{{AFX_INSERT_LOCATION}} //MicrosoftDeveloperStudiowillinsertadditionaldeclarationsimmediatelybeforethepreviousline. #endif//!defined(AFX_ENHPROGRESSCTRL_H__12909D73_C393_11D1_9FAE_8192554015AD__INCLUDED_)

5、其中GradientProgressCtrl.c

[cpp] view plain copy //GradientProgressCtrl.cpp:implementationfile #include"stdafx.h" #include"GradientProgressCtrl.h" #ifdef_DEBUG #definenewDEBUG_NEW #undefTHIS_FILE staticcharTHIS_FILE[]=__FILE__; #endif / //CGradientProgressCtrl CGradientProgressCtrl::CGradientProgressCtrl() { //DefaultsassignedbyCProgressCtrl() m_nLower=0; m_nUpper=100; m_nCurrentPosition=0; m_nStep=10; //Initialcolors m_clrStart=COLORREF(RGB(255,0,0)); m_clrEnd=COLORREF(RGB(0,0,255)); m_clrBkGround=COLORREF(RGB(0,0,0));//::GetSysColor(COLOR_3DFACE); m_clrText=COLORREF(RGB(255,255,255)); //Initialshowpercent m_bShowPercent=FALSE; } CGradientProgressCtrl::~CGradientProgressCtrl() { } BEGIN_MESSAGE_MAP(CGradientProgressCtrl,CProgressCtrl) //{{AFX_MSG_MAP(CGradientProgressCtrl) ON_WM_PAINT() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() / //CGradientProgressCtrlmessagehandlers / /* OnPaint Themaindrawingroutine.Consistsoftwoparts (1)CalltheDrawGradientroutinetodrawthevisiblepartoftheprogressgradient (2)Ifneeded,showthepercentagetext */ / voidCGradientProgressCtrl::OnPaint() { CPaintDCdc(this);//devicecontextforpainting //TODO:Addyourmessagehandlercodehere CRectrectClient; GetClientRect(&rectClient); //Ifthecurrentpositionisinvalidthenweshouldfadeintothebackground if(m_nCurrentPosition<=m_nLower||m_nCurrentPosition>m_nUpper) { CRectrect; GetClientRect(rect); CBrushbrush; brush.CreateSolidBrush(COLORREF(RGB(0,0,0)));//::GetSysColor(COLOR_3DFACE)); dc.FillRect(&rect,&brush); VERIFY(brush.DeleteObject()); return; } //Theactionstotakedependonwhetherornotweareaverticalcontrol DWORDdwStyles=GetStyle(); BOOLbVertical=(BOOL)(dwStyles&PBS_VERTICAL); //Figureoutwhatpartshouldbevisiblesowecanstopthegradientwhenneeded floatmaxWidth; if(bVertical) maxWidth=((float)m_nCurrentPosition/(float)m_nUpper*(float)rectClient.bottom); else maxWidth=((float)m_nCurrentPosition/(float)m_nUpper*(float)rectClient.right); //Drawthegradient DrawGradient(&dc,rectClient,(int)maxWidth,bVertical); //Showpercentindicatorifneeded if(m_bShowPercent) { CStringstrPercent; floatfp=100.0f; fp*=(float)(m_nCurrentPosition-m_nLower); fp/=(float)(m_nUpper-m_nLower); strPercent.Format(_T("%3.0f%%"),fp); dc.SetTextColor(m_clrText); dc.SetBkMode(TRANSPARENT); dc.DrawText(strPercent,&rectClient,DT_VCENTER|DT_CENTER|DT_SINGLELINE); } //DonotcallCProgressCtrl::OnPaint()forpaintingmessages } / /* SetRange Overriddenbaseclassmembertorememberwheretheindicatorthinks itisandtheboundaryrangeofthecontrol. Params nLowerlowerbound nUpperuppoerbound */ / voidCGradientProgressCtrl::SetRange(intnLower,intnUpper) { m_nLower=nLower; m_nUpper=nUpper; m_nCurrentPosition=nLower; CProgressCtrl::SetRange(nLower,nUpper); } / /* SetRange32 Overriddenbaseclassmembertorememberwheretheindicatorthinks itisandtheboundaryrangeofthecontrol. Params nLowerlowerbound nUpperuppoerbound */ / voidCGradientProgressCtrl::SetRange32(intnLower,intnUpper) { m_nLower=nLower; m_nUpper=nUpper; m_nCurrentPosition=nLower; CProgressCtrl::SetRange(nLower,nUpper); } / /* SetPos Overriddenbaseclassmembertoretainwherethecurrentprogressindicator islocated. Params nPosCurrentpositioninrange */ / intCGradientProgressCtrl::SetPos(intnPos) { m_nCurrentPosition=nPos; return(CProgressCtrl::SetPos(nPos)); } / /* SetStep Overriddenbaseclassmembertoretainthestepintervalusedwhen fillingtheprogresscontrol Params nStepstepintervalforfillingprogresscontrol */ / intCGradientProgressCtrl::SetStep(intnStep) { m_nStep=nStep; return(CProgressCtrl::SetStep(nStep)); } / /* StepIt Overriddenbaseclassmembertoincrementthecontrolaccordingtothe currentpositionandthestepinterval Params nStepstepintervalforfillingprogresscontrol */ / intCGradientProgressCtrl::StepIt(void) { m_nCurrentPosition+=m_nStep; return(CProgressCtrl::StepIt()); } / /* DrawGradient CalledfromOnPaint,itdoesmostoftheworkoffillingintheclient rectanglewiththeappropriatecolors.Thenormalroutinewouldfill theentireclientrectangle,butwetruncatethedrawingtoreflect thecurrentpositionintheprogresscontrol Params pDCpointertoCPaintDCforrendering rectClientclientrectanglewhereweshoulddraw nMaxWidthwhereweshouldstopdrawingthegradient */ / voidCGradientProgressCtrl::DrawGradient(CPaintDC*pDC,constRECT&rectClient,constint&nMaxWidth,constBOOL&bVertical) { RECTrectFill;//Rectangleforfillingband floatfStep;//Howwideiseachband? CBrushbrush;//Brushtofillinthebar CMYMemDCmemDC(pDC); //Firstfindoutthelargestcolordistancebetweenthestartandendcolors.Thisdistance //willdeterminehowmanystepsweusetocarveuptheclientregionandthesizeofeach //gradientrect. intr,g,b;//Firstdistance,thenstartingvalue floatrStep,gStep,bStep;//Stepsizeforeachcolor BOOLbSameColor=FALSE;//Handlecaseifstartcolor==endcolor //Getthecolordifferences r=(GetRValue(m_clrEnd)-GetRValue(m_clrStart)); g=(GetGValue(m_clrEnd)-GetGValue(m_clrStart)); b=(GetBValue(m_clrEnd)-GetBValue(m_clrStart)); //Checktoseeifcolorsaresame if((r==0)&&(g==0)&&(b==0)) { bSameColor=TRUE; //Addedthethreelinesbelowtofixthedrawing //problemwhichusedtooccurwhenboththestart //andendcolorsaresame. r=GetRValue(m_clrStart); g=GetGValue(m_clrStart); b=GetBValue(m_clrStart); } intnSteps; //Selectmax.possiblevaluefornStepsifthecolorsareequal if(bSameColor&&m_clrStart==0) nSteps=255; else//Makethenumberofstepsequaltothegreatestdistance nSteps=max(abs(r),max(abs(g),abs(b))); //Determinehowlargeeachbandshouldbeinordertocoverthe //clientwithnStepsbands(oneforeverycolorintensitylevel) if(bVertical) fStep=(float)rectClient.bottom/(float)nSteps; else fStep=(float)rectClient.right/(float)nSteps; //Calculatethestepsizeforeachcolor rStep=r/(float)nSteps; gStep=g/(float)nSteps; bStep=b/(float)nSteps;//这三个变量都设置为0就不会出现渐变颜色 //Resetthecolorstothestartingposition r=GetRValue(m_clrStart); g=GetGValue(m_clrStart); b=GetBValue(m_clrStart); //Startfillingbands for(intiOnBand=0;iOnBand<nSteps;iOnBand++) { //Filltheverticalcontrol if(bVertical) { ::SetRect(&rectFill, 0,//UpperleftX (int)(iOnBand*fStep),//UpperleftY rectClient.right+1,//LowerrightX (int)((iOnBand+1)*fStep));//LowerrightY //CDC::FillSolidRectisfaster,butitdoesnothandle8-bitcolordepth VERIFY(brush.CreateSolidBrush(RGB(r+rStep*iOnBand,g+gStep*iOnBand,b+bStep*iOnBand))); memDC.FillRect(&rectFill,&brush); VERIFY(brush.DeleteObject()); //Ifwearepastthemaximumforthecurrentpositionweneedtogetoutoftheloop. //Beforeweleave,werepainttheremainderoftheclientareawiththebackgroundcolor. if(rectFill.bottom>nMaxWidth) { ::SetRect(&rectFill,0,rectFill.bottom,rectClient.right,rectClient.bottom); VERIFY(brush.CreateSolidBrush(m_clrBkGround)); memDC.FillRect(&rectFill,&brush); VERIFY(brush.DeleteObject()); return; } } else//Fillthehorizontalcontrol { ::SetRect(&rectFill, (int)(iOnBand*fStep),//UpperleftX 0,//UpperleftY (int)((iOnBand+1)*fStep),//LowerrightX rectClient.bottom+1);//LowerrightY //CDC::FillSolidRectisfaster,butitdoesnothandle8-bitcolordepth VERIFY(brush.CreateSolidBrush(RGB(r+rStep*iOnBand,g+gStep*iOnBand,b+bStep*iOnBand))); memDC.FillRect(&rectFill,&brush); VERIFY(brush.DeleteObject()); //Ifwearepastthemaximumforthecurrentpositionweneedtogetoutoftheloop. //Beforeweleave,werepainttheremainderoftheclientareawiththebackgroundcolor. if(rectFill.right>nMaxWidth) { ::SetRect(&rectFill,rectFill.right,0,rectClient.right,rectClient.bottom); VERIFY(brush.CreateSolidBrush(m_clrBkGround)); memDC.FillRect(&rectFill,&brush); VERIFY(brush.DeleteObject()); return; } } } } / /* OnEraseBkgnd OverriddenCWndfunctionsothatalldrawingisdoneintheOnPaintcall. WereturnTRUEsothatCWnddoesn'ttrytoeraseourbackground. Params pDCpointertoCDCforrendering */ / BOOLCGradientProgressCtrl::OnEraseBkgnd(CDC*pDC) { //TODO:Addyourmessagehandlercodehereand/orcalldefault returnTRUE; }

6、其中MemDC.h

[cpp] view plain copy #if!defined(AFX_MEMDC_H__CA1D3541_7235_11D1_ABBA_00A0243D1382__INCLUDED_) #defineAFX_MEMDC_H__CA1D3541_7235_11D1_ABBA_00A0243D1382__INCLUDED_ #if_MSC_VER>=1000 #pragmaonce #endif//_MSC_VER>=1000 //MemDC.h:headerfile // // //CMemDC-memoryDC // //Author:KeithRule //Email:keithr@ //Copyright1996-1997,KeithRule // //Youmayfreelyuseormodifythiscodeprovidedthis //Copyrightisincludedinallderivedversions. // //History-10/3/97Fixedscrollingbug. //Addedprintsupport. //25feb98-fixedminorassertionbug // //ThisclassimplementsamemoryDeviceContext classCMYMemDC:publicCDC { public: //constructorsetsupthememoryDC CMYMemDC(CDC*pDC):CDC() { ASSERT(pDC!=NULL); m_pDC=pDC; m_pOldBitmap=NULL; m_bMemDC=!pDC->IsPrinting(); if(m_bMemDC)//CreateaMemoryDC { pDC->GetClipBox(&m_rect); CreateCompatibleDC(pDC); m_bitmap.CreateCompatibleBitmap(pDC,m_rect.Width(),m_rect.Height()); m_pOldBitmap=SelectObject(&m_bitmap); SetWindowOrg(m_rect.left,m_rect.top); } else//MakeacopyofthereleventpartsofthecurrentDCforprinting { m_bPrinting=pDC->m_bPrinting; m_hDC=pDC->m_hDC; m_hAttribDC=pDC->m_hAttribDC; } } //DestructorcopiesthecontentsofthememDCtotheoriginalDC ~CMYMemDC() { if(m_bMemDC){ //Copytheoffscreenbitmapontothescreen. m_pDC->BitBlt(m_rect.left,m_rect.top,m_rect.Width(),m_rect.Height(), this,m_rect.left,m_rect.top,SRCCOPY); //Swapbacktheoriginalbitmap. SelectObject(m_pOldBitmap); }else{ //AllweneedtodoisreplacetheDCwithanillegalvalue, //thiskeepsusfromaccidentlydeletingthehandlesassociatedwith //theCDCthatwaspassedtotheconstructor. m_hDC=m_hAttribDC=NULL; } } //Allowusageasapointer CMYMemDC*operator->(){returnthis;} //Allowusageasapointer operatorCMYMemDC*(){returnthis;} private: CBitmapm_bitmap;//Offscreenbitmap CBitmap*m_pOldBitmap;//bitmaporiginallyfoundinCMYMemDC CDC*m_pDC;//SavesCDCpassedinconstructor CRectm_rect;//Rectangleofdrawingarea. BOOLm_bMemDC;//TRUEifCDCreallyisaMemoryDC. }; / //{{AFX_INSERT_LOCATION}} //MicrosoftDeveloperStudiowillinsertadditionaldeclarationsimmediatelybeforethepreviousline. #endif//!defined(AFX_MEMDC_H__CA1D3541_7235_11D1_ABBA_00A0243D1382__INCLUDED_)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。
相关阅读
MFC 进度条

MFC 进度条

2023-04-15

mfc进度条

mfc进度条

2021-12-14

MfC之进度条

MfC之进度条

2018-11-23

MFC进度条编程

MFC进度条编程

2020-06-22