//========================================================================
//TITLE:
// CProgress类使用例程
//AUTHOR:
// norains
//DATE:
// Tuesday 17-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
CProgress类是仿照微软的CProgress写的一个显示进度类.最大的区别在于,该类的状态显示的图片可以按自己意愿设置,这对于某些需要风格独特的程序来说,显得比较方便.
该类的使用范例如下:
//声明一个对象
CProgressprg;
//设置显示的位置
prg.SetPosition(rcWnd);
//设置有效和无效的图片
prg.SetImgInfoInvalid(&imgInvalid);
prg.SetImgInfoValid(&imgValid)
//设置范围
prg.SetRange(0,6);
//设置等级
prg.SetLevel(4);
//传入应用程序的实例句柄,令控件能读取图片
prg.SetHinstance(hInst);
//绘制控件
prg.Draw(hdc);


/**///////////////////////////////////////////////////////////////////////
//Progress.h:interfacefortheCProgressclass.
//
//Version:
//1.0.2
//Data:
//2007.04.12

/**///////////////////////////////////////////////////////////////////////

#ifndefPROGRESS_H
#definePROGRESS_H


#include"CtrlCommon.h"


//Theimagedisplayedinformation
typedefstruct

...{
LONGimgID;
LONGleft;
LONGtop;
LONGright;
LONGbottom;
}DISPIMAGEINFO,*PDISPIMAGEINFO;

classCProgress

...{
public:
BOOLSetBkMode(intiMode);
voidSetBkColor(COLORREFcrColor);
voidSetHinstance(HINSTANCEhInst);
BOOLDraw(HDChdc);
BOOLSetLevel(intm_iLevel);
BOOLSetRange(intiMin,intiMax);
voidSetImgInfoInvalid(constDISPIMAGEINFO*pInfo);
voidSetImgInfoValid(constDISPIMAGEINFO*pInfo);
voidSetPosition(constRECT*prc);
CProgress();
virtual~CProgress();
protected:
RECTm_rcWndPos;
DISPIMAGEINFOm_ImgValid;
DISPIMAGEINFOm_ImgInvalid;
intm_iMinLevel;
intm_iMaxLevel;
intm_iCurLevel;
HINSTANCEm_hInst;
COLORREFm_crBkColor;
intm_iBkMode;
};

#endif//#ifndefPROGRESS_H


/**///////////////////////////////////////////////////////////////////////
//Progress.cpp:implementationoftheCProgressclass.
//

/**///////////////////////////////////////////////////////////////////////

#include"stdafx.h"
#include"Progress.h"


//--------------------------------------------------------------------
//Macrodefine
#defineDEFAULT_MIN_LEVEL0
#defineDEFAULT_MAX_LEVEL10
#defineDEFAULT_CUR_LEVEL0
#defineDEFAULT_BKCOLORRGB(128,128,128)
#defineDEFAULT_BKMODETRANSPARENT

//Theintervalbetweentwolevelimage
#defineLEVEL_INTERVAL4
//--------------------------------------------------------------------

/**///////////////////////////////////////////////////////////////////////
//Construction/Destruction

/**///////////////////////////////////////////////////////////////////////

CProgress::CProgress()

...{
memset(&m_rcWndPos,0,sizeof(m_rcWndPos));
memset(&m_ImgValid,0,sizeof(m_ImgValid));
memset(&m_ImgInvalid,0,sizeof(m_ImgInvalid));
m_iMaxLevel=DEFAULT_MAX_LEVEL;
m_iMinLevel=DEFAULT_MIN_LEVEL;
m_iCurLevel=DEFAULT_CUR_LEVEL;
m_hInst=NULL;
m_crBkColor=DEFAULT_BKCOLOR;
m_iBkMode=DEFAULT_BKMODE;
}

CProgress::~CProgress()

...{
}


//-------------------------------------------------------------------
//Description:
//Settheposition
//-------------------------------------------------------------------
voidCProgress::SetPosition(constRECT*prc)

...{
m_rcWndPos=*prc;
}


//-------------------------------------------------------------------
//Description:
//Settheimageinformationforvalid
//-------------------------------------------------------------------
voidCProgress::SetImgInfoValid(constDISPIMAGEINFO*pInfo)

...{
m_ImgValid=*pInfo;
}


//-------------------------------------------------------------------
//Description:
//Settheimageinformationforinvalid
//-------------------------------------------------------------------
voidCProgress::SetImgInfoInvalid(constDISPIMAGEINFO*pInfo)

...{
m_ImgInvalid=*pInfo;
}


//-------------------------------------------------------------------
//Description:
//Settherange
//-------------------------------------------------------------------
BOOLCProgress::SetRange(intiMin,intiMax)

...{
if(iMin<0||iMax<0||iMin>=iMax)

...{
returnFALSE;
}

m_iMinLevel=iMin;
m_iMaxLevel=iMax;

returnTRUE;
}



//-------------------------------------------------------------------
//Description:
//Settheposition
//-------------------------------------------------------------------
BOOLCProgress::SetLevel(intiLevel)

...{
if(iLevel<m_iMinLevel||iLevel>m_iMaxLevel)

...{
returnFALSE;
}
m_iCurLevel=iLevel;
returnTRUE;
}


//-------------------------------------------------------------------
//Description:
//Drawthecontrol
//-------------------------------------------------------------------
BOOLCProgress::Draw(HDChdc)

...{
if(m_hInst==NULL)

...{
returnFALSE;
}

intiImgTotalWidth=(m_ImgValid.right-m_ImgValid.left)*(m_iMaxLevel-m_iMinLevel)+LEVEL_INTERVAL*(m_iMaxLevel-m_iMinLevel-1);
intiImgTotalHeight=m_ImgValid.bottom-m_ImgValid.top;

//CreateaDCthatmatchesthedevice
HBITMAPhBitmap=CreateCompatibleBitmap(hdc,iImgTotalWidth,iImgTotalHeight);
HDChdcMem=CreateCompatibleDC(hdc);
//Selectthebitmapintotothecompatibledevicecontext
HGDIOBJhOldSel=SelectObject(hdcMem,hBitmap);



//Drawthebackground
//Theframecolor
HPENhPen=CreatePen(PS_SOLID,1,m_crBkColor);
HPENhOldPen=NULL;
hOldPen=(HPEN)SelectObject(hdcMem,hPen);
//therectcolor
HBRUSHhBrush=CreateSolidBrush(m_crBkColor);
HGDIOBJhOldBrush=SelectObject(hdcMem,hBrush);
//Draw
Rectangle(hdcMem,0,0,iImgTotalWidth+1,iImgTotalHeight+1);
//Realsetheresource
SelectObject(hdcMem,hOldBrush);
DeleteObject(hBrush);
SelectObject(hdcMem,hOldPen);
DeleteObject(hPen);




//Drawthevalidimage
//CreateaDCthatmatchesthedevicetodrawthebackgroundbitmap
HDChdcBmp=CreateCompatibleDC(hdc);
//Loadthebitmap
HANDLEhBmp=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgValid.imgID),IMAGE_BITMAP,0,0,0);
//Selectthebitmapintotothecompatibledevicecontext
HGDIOBJhOldBmpSel=SelectObject(hdcBmp,hBmp);
inti=0,iPos_x=0;
for(i=m_iMinLevel+1;i<=m_iCurLevel;i++)

...{
//CopythebitmapimagetothememoryDC
BitBlt(hdcMem,iPos_x,0,(m_ImgValid.right-m_ImgValid.left),(m_ImgValid.bottom-m_ImgValid.top),hdcBmp,m_ImgValid.left,m_ImgValid.top,SRCCOPY);
iPos_x+=(m_ImgValid.right-m_ImgValid.left)+LEVEL_INTERVAL;
}
//Deletethebmp
DeleteObject(hBmp);


//Drawtheinvalidimage
hBmp=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgInvalid.imgID),IMAGE_BITMAP,0,0,0);
//Selectthebitmapintotothecompatibledevicecontext
SelectObject(hdcBmp,hBmp);
for(i=m_iCurLevel+1;i<=m_iMaxLevel;i++)

...{
//CopythebitmapimagetothememoryDC
BitBlt(hdcMem,iPos_x,0,(m_ImgValid.right-m_ImgValid.left),(m_ImgValid.bottom-m_ImgValid.top),hdcBmp,m_ImgInvalid.left,m_ImgInvalid.top,SRCCOPY);
iPos_x+=(m_ImgValid.right-m_ImgValid.left)+LEVEL_INTERVAL;
}
//Deletethebmp
DeleteObject(hBmp);



if(m_iBkMode==TRANSPARENT)

...{
TransparentImage(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,0,0,iImgTotalWidth,iImgTotalHeight,m_crBkColor);
}
else

...{
StretchBlt(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,0,0,iImgTotalWidth,iImgTotalHeight,SRCCOPY);
}
//RestoreoriginalbitmapselectionanddestroythememoryDC
SelectObject(hdcBmp,hOldBmpSel);
SelectObject(hdcMem,hOldSel);
DeleteObject(hBitmap);
DeleteDC(hdcBmp);
DeleteDC(hdcMem);



returnTRUE;
}


//-------------------------------------------------------------------
//Description:
//Setthehinstance
//-------------------------------------------------------------------
voidCProgress::SetHinstance(HINSTANCEhInst)

...{
m_hInst=hInst;
}


//-------------------------------------------------------------------
//Description:
//Setthebackgroundcolor
//-------------------------------------------------------------------
voidCProgress::SetBkColor(COLORREFcrColor)

...{
m_crBkColor=crColor;
}


//--------------------------------------------------------------------
//Description:
//Setthebackgroundmode.IfyouchoosetheTRANSPARENT,thetransparent
//coloristhebackgroundcolor
//
//Parameters:
//iMode:[in]Thevalueisjustlikeasfollow:
//OPAQUE--Backgroundisfilledwiththecurrentbackgroundcolorbeforethetext,
//hatchedbrush,orpenisdrawn.
//TRANSPARENT--Backgroundremainsuntouched.

//--------------------------------------------------------------------
BOOLCProgress::SetBkMode(intiMode)

...{
if(iMode==OPAQUE||iMode==TRANSPARENT)

...{
m_iBkMode=iMode;
returnTRUE;
}
else

...{
returnFALSE;
}
}
//TITLE:
// CProgress类使用例程
//AUTHOR:
// norains
//DATE:
// Tuesday 17-April-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
CProgress类是仿照微软的CProgress写的一个显示进度类.最大的区别在于,该类的状态显示的图片可以按自己意愿设置,这对于某些需要风格独特的程序来说,显得比较方便.
该类的使用范例如下:
//声明一个对象
CProgressprg;
//设置显示的位置
prg.SetPosition(rcWnd);
//设置有效和无效的图片
prg.SetImgInfoInvalid(&imgInvalid);
prg.SetImgInfoValid(&imgValid)
//设置范围
prg.SetRange(0,6);
//设置等级
prg.SetLevel(4);
//传入应用程序的实例句柄,令控件能读取图片
prg.SetHinstance(hInst);
//绘制控件
prg.Draw(hdc);


/**///////////////////////////////////////////////////////////////////////
//Progress.h:interfacefortheCProgressclass.
//
//Version:
//1.0.2
//Data:
//2007.04.12
/**///////////////////////////////////////////////////////////////////////
#ifndefPROGRESS_H
#definePROGRESS_H

#include"CtrlCommon.h"

//Theimagedisplayedinformation
typedefstruct
...{
LONGimgID;
LONGleft;
LONGtop;
LONGright;
LONGbottom;
}DISPIMAGEINFO,*PDISPIMAGEINFO;
classCProgress
...{
public:
BOOLSetBkMode(intiMode);
voidSetBkColor(COLORREFcrColor);
voidSetHinstance(HINSTANCEhInst);
BOOLDraw(HDChdc);
BOOLSetLevel(intm_iLevel);
BOOLSetRange(intiMin,intiMax);
voidSetImgInfoInvalid(constDISPIMAGEINFO*pInfo);
voidSetImgInfoValid(constDISPIMAGEINFO*pInfo);
voidSetPosition(constRECT*prc);
CProgress();
virtual~CProgress();
protected:
RECTm_rcWndPos;
DISPIMAGEINFOm_ImgValid;
DISPIMAGEINFOm_ImgInvalid;
intm_iMinLevel;
intm_iMaxLevel;
intm_iCurLevel;
HINSTANCEm_hInst;
COLORREFm_crBkColor;
intm_iBkMode;
};
#endif//#ifndefPROGRESS_H


/**///////////////////////////////////////////////////////////////////////
//Progress.cpp:implementationoftheCProgressclass.
//
/**///////////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include"Progress.h"

//--------------------------------------------------------------------
//Macrodefine
#defineDEFAULT_MIN_LEVEL0
#defineDEFAULT_MAX_LEVEL10
#defineDEFAULT_CUR_LEVEL0
#defineDEFAULT_BKCOLORRGB(128,128,128)
#defineDEFAULT_BKMODETRANSPARENT
//Theintervalbetweentwolevelimage
#defineLEVEL_INTERVAL4
//--------------------------------------------------------------------
/**///////////////////////////////////////////////////////////////////////
//Construction/Destruction
/**///////////////////////////////////////////////////////////////////////
CProgress::CProgress()
...{
memset(&m_rcWndPos,0,sizeof(m_rcWndPos));
memset(&m_ImgValid,0,sizeof(m_ImgValid));
memset(&m_ImgInvalid,0,sizeof(m_ImgInvalid));
m_iMaxLevel=DEFAULT_MAX_LEVEL;
m_iMinLevel=DEFAULT_MIN_LEVEL;
m_iCurLevel=DEFAULT_CUR_LEVEL;
m_hInst=NULL;
m_crBkColor=DEFAULT_BKCOLOR;
m_iBkMode=DEFAULT_BKMODE;
}
CProgress::~CProgress()
...{
}

//-------------------------------------------------------------------
//Description:
//Settheposition
//-------------------------------------------------------------------
voidCProgress::SetPosition(constRECT*prc)
...{
m_rcWndPos=*prc;
}

//-------------------------------------------------------------------
//Description:
//Settheimageinformationforvalid
//-------------------------------------------------------------------
voidCProgress::SetImgInfoValid(constDISPIMAGEINFO*pInfo)
...{
m_ImgValid=*pInfo;
}

//-------------------------------------------------------------------
//Description:
//Settheimageinformationforinvalid
//-------------------------------------------------------------------
voidCProgress::SetImgInfoInvalid(constDISPIMAGEINFO*pInfo)
...{
m_ImgInvalid=*pInfo;
}

//-------------------------------------------------------------------
//Description:
//Settherange
//-------------------------------------------------------------------
BOOLCProgress::SetRange(intiMin,intiMax)
...{
if(iMin<0||iMax<0||iMin>=iMax)
...{
returnFALSE;
}
m_iMinLevel=iMin;
m_iMaxLevel=iMax;
returnTRUE;
}


//-------------------------------------------------------------------
//Description:
//Settheposition
//-------------------------------------------------------------------
BOOLCProgress::SetLevel(intiLevel)
...{
if(iLevel<m_iMinLevel||iLevel>m_iMaxLevel)
...{
returnFALSE;
}
m_iCurLevel=iLevel;
returnTRUE;
}

//-------------------------------------------------------------------
//Description:
//Drawthecontrol
//-------------------------------------------------------------------
BOOLCProgress::Draw(HDChdc)
...{
if(m_hInst==NULL)
...{
returnFALSE;
}
intiImgTotalWidth=(m_ImgValid.right-m_ImgValid.left)*(m_iMaxLevel-m_iMinLevel)+LEVEL_INTERVAL*(m_iMaxLevel-m_iMinLevel-1);
intiImgTotalHeight=m_ImgValid.bottom-m_ImgValid.top;
//CreateaDCthatmatchesthedevice
HBITMAPhBitmap=CreateCompatibleBitmap(hdc,iImgTotalWidth,iImgTotalHeight);
HDChdcMem=CreateCompatibleDC(hdc);
//Selectthebitmapintotothecompatibledevicecontext
HGDIOBJhOldSel=SelectObject(hdcMem,hBitmap);


//Drawthebackground
//Theframecolor
HPENhPen=CreatePen(PS_SOLID,1,m_crBkColor);
HPENhOldPen=NULL;
hOldPen=(HPEN)SelectObject(hdcMem,hPen);
//therectcolor
HBRUSHhBrush=CreateSolidBrush(m_crBkColor);
HGDIOBJhOldBrush=SelectObject(hdcMem,hBrush);
//Draw
Rectangle(hdcMem,0,0,iImgTotalWidth+1,iImgTotalHeight+1);
//Realsetheresource
SelectObject(hdcMem,hOldBrush);
DeleteObject(hBrush);
SelectObject(hdcMem,hOldPen);
DeleteObject(hPen);



//Drawthevalidimage
//CreateaDCthatmatchesthedevicetodrawthebackgroundbitmap
HDChdcBmp=CreateCompatibleDC(hdc);
//Loadthebitmap
HANDLEhBmp=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgValid.imgID),IMAGE_BITMAP,0,0,0);
//Selectthebitmapintotothecompatibledevicecontext
HGDIOBJhOldBmpSel=SelectObject(hdcBmp,hBmp);
inti=0,iPos_x=0;
for(i=m_iMinLevel+1;i<=m_iCurLevel;i++)
...{
//CopythebitmapimagetothememoryDC
BitBlt(hdcMem,iPos_x,0,(m_ImgValid.right-m_ImgValid.left),(m_ImgValid.bottom-m_ImgValid.top),hdcBmp,m_ImgValid.left,m_ImgValid.top,SRCCOPY);
iPos_x+=(m_ImgValid.right-m_ImgValid.left)+LEVEL_INTERVAL;
}
//Deletethebmp
DeleteObject(hBmp);

//Drawtheinvalidimage
hBmp=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgInvalid.imgID),IMAGE_BITMAP,0,0,0);
//Selectthebitmapintotothecompatibledevicecontext
SelectObject(hdcBmp,hBmp);
for(i=m_iCurLevel+1;i<=m_iMaxLevel;i++)
...{
//CopythebitmapimagetothememoryDC
BitBlt(hdcMem,iPos_x,0,(m_ImgValid.right-m_ImgValid.left),(m_ImgValid.bottom-m_ImgValid.top),hdcBmp,m_ImgInvalid.left,m_ImgInvalid.top,SRCCOPY);
iPos_x+=(m_ImgValid.right-m_ImgValid.left)+LEVEL_INTERVAL;
}
//Deletethebmp
DeleteObject(hBmp);


if(m_iBkMode==TRANSPARENT)
...{
TransparentImage(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,0,0,iImgTotalWidth,iImgTotalHeight,m_crBkColor);
}
else
...{
StretchBlt(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,0,0,iImgTotalWidth,iImgTotalHeight,SRCCOPY);
}
//RestoreoriginalbitmapselectionanddestroythememoryDC
SelectObject(hdcBmp,hOldBmpSel);
SelectObject(hdcMem,hOldSel);
DeleteObject(hBitmap);
DeleteDC(hdcBmp);
DeleteDC(hdcMem);


returnTRUE;
}

//-------------------------------------------------------------------
//Description:
//Setthehinstance
//-------------------------------------------------------------------
voidCProgress::SetHinstance(HINSTANCEhInst)
...{
m_hInst=hInst;
}

//-------------------------------------------------------------------
//Description:
//Setthebackgroundcolor
//-------------------------------------------------------------------
voidCProgress::SetBkColor(COLORREFcrColor)
...{
m_crBkColor=crColor;
}

//--------------------------------------------------------------------
//Description:
//Setthebackgroundmode.IfyouchoosetheTRANSPARENT,thetransparent
//coloristhebackgroundcolor
//
//Parameters:
//iMode:[in]Thevalueisjustlikeasfollow:
//OPAQUE--Backgroundisfilledwiththecurrentbackgroundcolorbeforethetext,
//hatchedbrush,orpenisdrawn.
//TRANSPARENT--Backgroundremainsuntouched.
//--------------------------------------------------------------------
BOOLCProgress::SetBkMode(intiMode)
...{
if(iMode==OPAQUE||iMode==TRANSPARENT)
...{
m_iBkMode=iMode;
returnTRUE;
}
else
...{
returnFALSE;
}
}
本文介绍了一个名为CProgress的自定义控件类,该类用于显示进度,并允许用户自定义有效和无效状态下的图片资源。文章提供了详细的使用示例,包括如何创建对象、设置位置、指定图片资源等。

被折叠的 条评论
为什么被折叠?



