//========================================================================
//TITLE:
// CButton使用详解
//AUTHOR:
// norains
//DATE:
// Saturday 16-May-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
恩,不用怀疑了,这个CButton类也不是微软的通用控件,而是和我之前写的CText及CProgress一样,都是为了方便大面积需要贴图的程序,而模仿微软的特性来写的一个类.
闲话少说,但确实也没什么可说的,就让我们直接看看例程代码.
恩,就这么简单,也没什么很特别的地方,仅仅主要是方便大量采用贴图的程序而已.
CButton类完整源码:
/**/ //
// Button.cpp:implementationoftheCButtonclass.
//
/**/ //
#include " stdafx.h "
#include " Button.h "
// -------------------------------------------------------------------
// Macrodefine
#define DEFAULT_TRANSPARENT_COLORRGB(125,125,125)
// --------------------------------------------------------------------
/**/ //
// Construction/Destruction
/**/ //
CButton::CButton()
... {
m_bEnable=TRUE;
memset(&m_rcWndPos,0,sizeof(m_rcWndPos));
memset(&m_ImgPush,0,sizeof(m_ImgPush));
memset(&m_ImgEnable,0,sizeof(m_ImgEnable));
memset(&m_ImgDisable,0,sizeof(m_ImgDisable));
m_hBmpPush=NULL;
m_hBmpEnable=NULL;
m_hBmpDisable=NULL;
m_hInst=NULL;
m_crTranColor=DEFAULT_TRANSPARENT_COLOR;
m_bTran=FALSE;
}
CButton:: ~ CButton()
... {
if(m_hBmpPush!=NULL)
...{
DeleteObject(m_hBmpPush);
m_hBmpPush=NULL;
}
if(m_hBmpEnable!=NULL)
...{
DeleteObject(m_hBmpEnable);
m_hBmpEnable=NULL;
}
if(m_hBmpDisable!=NULL)
...{
DeleteObject(m_hBmpDisable);
m_hBmpDisable=NULL;
}
}
// --------------------------------------------------------------------
// Description:
// Thebuttonisenableornot
//
// -------------------------------------------------------------------
BOOLCButton::GetEnable()
... {
returnm_bEnable;
}
// --------------------------------------------------------------------
// Description:
// Setthebuttonstatus
//
// -------------------------------------------------------------------
void CButton::SetEnable(BOOLbEnable)
... {
m_bEnable=bEnable;
}
// --------------------------------------------------------------------
// Description:
// Drawthebutton
//
// -------------------------------------------------------------------
BOOLCButton::Draw(HDChdc,ButtonDrawTypebtnDraw)
... {
if(m_hInst==NULL)
...{
returnFALSE;
}
HANDLEhBmp=NULL;
PDISPIMAGEINFOpInfo;
if(btnDraw==BTN_DRAW_AUTO)
...{
if(m_bEnable==TRUE)
...{
if(m_hBmpEnable==NULL)
...{
m_hBmpEnable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgEnable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpEnable;
pInfo=&m_ImgEnable;
}
else
...{
if(m_hBmpDisable==NULL)
...{
m_hBmpDisable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgDisable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpDisable;
pInfo=&m_ImgDisable;
}
}
elseif(btnDraw==BTN_DRAW_ENABLE)
...{
if(m_hBmpEnable==NULL)
...{
m_hBmpEnable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgEnable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpEnable;
pInfo=&m_ImgEnable;
}
elseif(btnDraw==BTN_DRAW_DISABLE)
...{
if(m_hBmpDisable==NULL)
...{
m_hBmpDisable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgDisable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpDisable;
pInfo=&m_ImgDisable;
}
elseif(btnDraw==BTN_DRAW_PUSH)
...{
if(m_hBmpPush==NULL)
...{
m_hBmpPush=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgPush.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpPush;
pInfo=&m_ImgPush;
}
//CreateaDCthatmatchesthedevice
HDChdcMem=CreateCompatibleDC(hdc);
//Selectthebitmapintotothecompatibledevicecontext
HGDIOBJhOldSel=SelectObject(hdcMem,hBmp);
//CopythebitmapimagefromthememoryDCtothescreenDC
if(m_bTran==FALSE)
...{
StretchBlt(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,pInfo->left,pInfo->top,(pInfo->right-pInfo->left),(pInfo->bottom-pInfo->top),SRCCOPY);
}
else
...{
TransparentBlt(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,pInfo->left,pInfo->top,(pInfo->right-pInfo->left),(pInfo->bottom-pInfo->top),m_crTranColor);
}
//RestoreoriginalbitmapselectionanddestroythememoryDC
SelectObject(hdcMem,hOldSel);
DeleteDC(hdcMem);
returnTRUE;
}
// --------------------------------------------------------------------
// Description:
// Setthehandleofinstance
//
// -------------------------------------------------------------------
void CButton::SetHinstance(HINSTANCEhInst)
... {
m_hInst=hInst;
}
// --------------------------------------------------------------------
// Description:
// Settheimageofenable
//
// -------------------------------------------------------------------
void CButton::SetImgInfoEnable( const DISPIMAGEINFO * pImgInfo)
... {
if(m_hBmpEnable!=NULL)
...{
DeleteObject(m_hBmpEnable);
m_hBmpEnable=NULL;
}
m_ImgEnable=*pImgInfo;
}
// --------------------------------------------------------------------
// Description:
// Settheimageofdisable
//
// -------------------------------------------------------------------
void CButton::SetImgInfoDisable( const DISPIMAGEINFO * pImgInfo)
... {
if(m_hBmpDisable!=NULL)
...{
DeleteObject(m_hBmpDisable);
m_hBmpDisable=NULL;
}
m_ImgDisable=*pImgInfo;
}
// --------------------------------------------------------------------
// Description:
// Settheimageofpush
//
// -------------------------------------------------------------------
void CButton::SetImgInfoPush( const DISPIMAGEINFO * pImgInfo)
... {
if(m_hBmpPush!=NULL)
...{
DeleteObject(m_hBmpPush);
m_hBmpPush=NULL;
}
m_ImgPush=*pImgInfo;
}
// --------------------------------------------------------------------
// Description:
// Setthebuttonposition
//
// -------------------------------------------------------------------
void CButton::SetPosition( const RECT * pRc)
... {
m_rcWndPos=*pRc;
}
// --------------------------------------------------------------------
// Description:
// Checktappedpositioninthearea.Ifthebuttonisdisable,
// itwouldreturnFALSE.
//
// -------------------------------------------------------------------
BOOLCButton::CheckTap( const LPPOINTppt)
... {
if(m_bEnable==FALSE)
...{
returnFALSE;
}
if(ppt->x>=m_rcWndPos.left&&ppt->x<=m_rcWndPos.right&&ppt->y>=m_rcWndPos.top&&ppt->y<=m_rcWndPos.bottom)
...{
returnTRUE;
}
else
...{
returnFALSE;
}
}
// --------------------------------------------------------------------
// Description:
// Getthepositionasrect
//
// ---------------------------------------------------------------------
void CButton::GetPosition(RECT * prcOut)
... {
*prcOut=m_rcWndPos;
}
// --------------------------------------------------------------------
// Description:
// Setthetransparentcolor
//
// -------------------------------------------------------------------
void CButton::SetTransparentColor(COLORREFcrColor)
... {
m_crTranColor=crColor;
}
// --------------------------------------------------------------------
// Description:
// Setthetransparentmode
//
// Parameters:
// bTran:[in]
// TRUE-Don'tdrawthetransparentcolor
// FALSE-Drawallthecolor
//
// -------------------------------------------------------------------
void CButton::SetTransparent(BOOLbTran)
... {
m_bTran=bTran;
}
//TITLE:
// CButton使用详解
//AUTHOR:
// norains
//DATE:
// Saturday 16-May-2007
//Environment:
// EVC4.0 + Standard SDK 4.2
// EVC4.0 + Standard SDK 5.0
//========================================================================
恩,不用怀疑了,这个CButton类也不是微软的通用控件,而是和我之前写的CText及CProgress一样,都是为了方便大面积需要贴图的程序,而模仿微软的特性来写的一个类.
闲话少说,但确实也没什么可说的,就让我们直接看看例程代码.
CButtonm_Btn;
// 设置该按钮可用
m_Btn.SetEnable(TRUE);
// 设置实例句柄
m_Btn.SetHinstance(hInst);
// 设置按钮的位置
m_Btn.SetPosition( & RC_BTN);
// 设置普通状态,按下状态,无效状态的图片信息
m_Btn.SetImgInfoDisable( & IMG_DISABLE);
m_Btn.SetImgInfoEnable( & IMG_ENABLE);
m_Btn.SetImgInfoPush( & IMG_PUSH);
// 设置透明绘制
m_Btn.SetTransparent(TRUE);
// 设置透明色
m_Btn.SetTransparentColor(TRANSPARENT_COLOR);
// 绘制背景
m_Btn.Draw(hdc);
// 判断是否点击该按钮
if (m_Btn.CheckTap( & pt) == TRUE)
... {
//Dosomething..
}
// 设置该按钮可用
m_Btn.SetEnable(TRUE);
// 设置实例句柄
m_Btn.SetHinstance(hInst);
// 设置按钮的位置
m_Btn.SetPosition( & RC_BTN);
// 设置普通状态,按下状态,无效状态的图片信息
m_Btn.SetImgInfoDisable( & IMG_DISABLE);
m_Btn.SetImgInfoEnable( & IMG_ENABLE);
m_Btn.SetImgInfoPush( & IMG_PUSH);
// 设置透明绘制
m_Btn.SetTransparent(TRUE);
// 设置透明色
m_Btn.SetTransparentColor(TRANSPARENT_COLOR);
// 绘制背景
m_Btn.Draw(hdc);
// 判断是否点击该按钮
if (m_Btn.CheckTap( & pt) == TRUE)
... {
//Dosomething..
}
恩,就这么简单,也没什么很特别的地方,仅仅主要是方便大量采用贴图的程序而已.
CButton类完整源码:
/**/
//
// Button.h:interfacefortheCButtonclass.
//
// Version:
// 1.1.1
// Data:
// 2007.03.31
/**/ //
#ifndefBUTTON_H
#define BUTTON_H
#include " CtrlCommon.h "
// ---------------------------------------------------------------------------
// Enumdata
// Forthebuttondraw
enum ButtonDrawType
... {
BTN_DRAW_AUTO,
BTN_DRAW_PUSH,
BTN_DRAW_ENABLE,
BTN_DRAW_DISABLE
} ;
// ------------------------------------------------------------------------------
// Class
class CButton
... {
public:
voidSetTransparent(BOOLbTran);
voidSetTransparentColor(COLORREFcrColor);
voidGetPosition(RECT*prcOut);
BOOLCheckTap(constLPPOINTppt);
voidSetPosition(constRECT*pRc);
voidSetImgInfoPush(constDISPIMAGEINFO*pImgInfo);
voidSetImgInfoDisable(constDISPIMAGEINFO*pImgInfo);
voidSetImgInfoEnable(constDISPIMAGEINFO*pImgInfo);
voidSetHinstance(HINSTANCEhInst);
BOOLDraw(HDChdc,ButtonDrawTypebtnDraw=BTN_DRAW_AUTO);
voidSetEnable(BOOLbEnable);
BOOLGetEnable();
CButton();
virtual~CButton();
protected:
BOOLm_bEnable;
RECTm_rcWndPos;
DISPIMAGEINFOm_ImgPush;
DISPIMAGEINFOm_ImgEnable;
DISPIMAGEINFOm_ImgDisable;
HANDLEm_hBmpPush;
HANDLEm_hBmpEnable;
HANDLEm_hBmpDisable;
HINSTANCEm_hInst;
COLORREFm_crTranColor;
BOOLm_bTran;
} ;
#endif // #ifndefBUTTON_H
// Button.h:interfacefortheCButtonclass.
//
// Version:
// 1.1.1
// Data:
// 2007.03.31
/**/ //
#ifndefBUTTON_H
#define BUTTON_H
#include " CtrlCommon.h "
// ---------------------------------------------------------------------------
// Enumdata
// Forthebuttondraw
enum ButtonDrawType
... {
BTN_DRAW_AUTO,
BTN_DRAW_PUSH,
BTN_DRAW_ENABLE,
BTN_DRAW_DISABLE
} ;
// ------------------------------------------------------------------------------
// Class
class CButton
... {
public:
voidSetTransparent(BOOLbTran);
voidSetTransparentColor(COLORREFcrColor);
voidGetPosition(RECT*prcOut);
BOOLCheckTap(constLPPOINTppt);
voidSetPosition(constRECT*pRc);
voidSetImgInfoPush(constDISPIMAGEINFO*pImgInfo);
voidSetImgInfoDisable(constDISPIMAGEINFO*pImgInfo);
voidSetImgInfoEnable(constDISPIMAGEINFO*pImgInfo);
voidSetHinstance(HINSTANCEhInst);
BOOLDraw(HDChdc,ButtonDrawTypebtnDraw=BTN_DRAW_AUTO);
voidSetEnable(BOOLbEnable);
BOOLGetEnable();
CButton();
virtual~CButton();
protected:
BOOLm_bEnable;
RECTm_rcWndPos;
DISPIMAGEINFOm_ImgPush;
DISPIMAGEINFOm_ImgEnable;
DISPIMAGEINFOm_ImgDisable;
HANDLEm_hBmpPush;
HANDLEm_hBmpEnable;
HANDLEm_hBmpDisable;
HINSTANCEm_hInst;
COLORREFm_crTranColor;
BOOLm_bTran;
} ;
#endif // #ifndefBUTTON_H
/**/ //
// Button.cpp:implementationoftheCButtonclass.
//
/**/ //
#include " stdafx.h "
#include " Button.h "
// -------------------------------------------------------------------
// Macrodefine
#define DEFAULT_TRANSPARENT_COLORRGB(125,125,125)
// --------------------------------------------------------------------
/**/ //
// Construction/Destruction
/**/ //
CButton::CButton()
... {
m_bEnable=TRUE;
memset(&m_rcWndPos,0,sizeof(m_rcWndPos));
memset(&m_ImgPush,0,sizeof(m_ImgPush));
memset(&m_ImgEnable,0,sizeof(m_ImgEnable));
memset(&m_ImgDisable,0,sizeof(m_ImgDisable));
m_hBmpPush=NULL;
m_hBmpEnable=NULL;
m_hBmpDisable=NULL;
m_hInst=NULL;
m_crTranColor=DEFAULT_TRANSPARENT_COLOR;
m_bTran=FALSE;
}
CButton:: ~ CButton()
... {
if(m_hBmpPush!=NULL)
...{
DeleteObject(m_hBmpPush);
m_hBmpPush=NULL;
}
if(m_hBmpEnable!=NULL)
...{
DeleteObject(m_hBmpEnable);
m_hBmpEnable=NULL;
}
if(m_hBmpDisable!=NULL)
...{
DeleteObject(m_hBmpDisable);
m_hBmpDisable=NULL;
}
}
// --------------------------------------------------------------------
// Description:
// Thebuttonisenableornot
//
// -------------------------------------------------------------------
BOOLCButton::GetEnable()
... {
returnm_bEnable;
}
// --------------------------------------------------------------------
// Description:
// Setthebuttonstatus
//
// -------------------------------------------------------------------
void CButton::SetEnable(BOOLbEnable)
... {
m_bEnable=bEnable;
}
// --------------------------------------------------------------------
// Description:
// Drawthebutton
//
// -------------------------------------------------------------------
BOOLCButton::Draw(HDChdc,ButtonDrawTypebtnDraw)
... {
if(m_hInst==NULL)
...{
returnFALSE;
}
HANDLEhBmp=NULL;
PDISPIMAGEINFOpInfo;
if(btnDraw==BTN_DRAW_AUTO)
...{
if(m_bEnable==TRUE)
...{
if(m_hBmpEnable==NULL)
...{
m_hBmpEnable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgEnable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpEnable;
pInfo=&m_ImgEnable;
}
else
...{
if(m_hBmpDisable==NULL)
...{
m_hBmpDisable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgDisable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpDisable;
pInfo=&m_ImgDisable;
}
}
elseif(btnDraw==BTN_DRAW_ENABLE)
...{
if(m_hBmpEnable==NULL)
...{
m_hBmpEnable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgEnable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpEnable;
pInfo=&m_ImgEnable;
}
elseif(btnDraw==BTN_DRAW_DISABLE)
...{
if(m_hBmpDisable==NULL)
...{
m_hBmpDisable=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgDisable.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpDisable;
pInfo=&m_ImgDisable;
}
elseif(btnDraw==BTN_DRAW_PUSH)
...{
if(m_hBmpPush==NULL)
...{
m_hBmpPush=LoadImage(m_hInst,MAKEINTRESOURCE(m_ImgPush.imgID),IMAGE_BITMAP,0,0,0);
}
hBmp=m_hBmpPush;
pInfo=&m_ImgPush;
}
//CreateaDCthatmatchesthedevice
HDChdcMem=CreateCompatibleDC(hdc);
//Selectthebitmapintotothecompatibledevicecontext
HGDIOBJhOldSel=SelectObject(hdcMem,hBmp);
//CopythebitmapimagefromthememoryDCtothescreenDC
if(m_bTran==FALSE)
...{
StretchBlt(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,pInfo->left,pInfo->top,(pInfo->right-pInfo->left),(pInfo->bottom-pInfo->top),SRCCOPY);
}
else
...{
TransparentBlt(hdc,m_rcWndPos.left,m_rcWndPos.top,(m_rcWndPos.right-m_rcWndPos.left),(m_rcWndPos.bottom-m_rcWndPos.top),
hdcMem,pInfo->left,pInfo->top,(pInfo->right-pInfo->left),(pInfo->bottom-pInfo->top),m_crTranColor);
}
//RestoreoriginalbitmapselectionanddestroythememoryDC
SelectObject(hdcMem,hOldSel);
DeleteDC(hdcMem);
returnTRUE;
}
// --------------------------------------------------------------------
// Description:
// Setthehandleofinstance
//
// -------------------------------------------------------------------
void CButton::SetHinstance(HINSTANCEhInst)
... {
m_hInst=hInst;
}
// --------------------------------------------------------------------
// Description:
// Settheimageofenable
//
// -------------------------------------------------------------------
void CButton::SetImgInfoEnable( const DISPIMAGEINFO * pImgInfo)
... {
if(m_hBmpEnable!=NULL)
...{
DeleteObject(m_hBmpEnable);
m_hBmpEnable=NULL;
}
m_ImgEnable=*pImgInfo;
}
// --------------------------------------------------------------------
// Description:
// Settheimageofdisable
//
// -------------------------------------------------------------------
void CButton::SetImgInfoDisable( const DISPIMAGEINFO * pImgInfo)
... {
if(m_hBmpDisable!=NULL)
...{
DeleteObject(m_hBmpDisable);
m_hBmpDisable=NULL;
}
m_ImgDisable=*pImgInfo;
}
// --------------------------------------------------------------------
// Description:
// Settheimageofpush
//
// -------------------------------------------------------------------
void CButton::SetImgInfoPush( const DISPIMAGEINFO * pImgInfo)
... {
if(m_hBmpPush!=NULL)
...{
DeleteObject(m_hBmpPush);
m_hBmpPush=NULL;
}
m_ImgPush=*pImgInfo;
}
// --------------------------------------------------------------------
// Description:
// Setthebuttonposition
//
// -------------------------------------------------------------------
void CButton::SetPosition( const RECT * pRc)
... {
m_rcWndPos=*pRc;
}
// --------------------------------------------------------------------
// Description:
// Checktappedpositioninthearea.Ifthebuttonisdisable,
// itwouldreturnFALSE.
//
// -------------------------------------------------------------------
BOOLCButton::CheckTap( const LPPOINTppt)
... {
if(m_bEnable==FALSE)
...{
returnFALSE;
}
if(ppt->x>=m_rcWndPos.left&&ppt->x<=m_rcWndPos.right&&ppt->y>=m_rcWndPos.top&&ppt->y<=m_rcWndPos.bottom)
...{
returnTRUE;
}
else
...{
returnFALSE;
}
}
// --------------------------------------------------------------------
// Description:
// Getthepositionasrect
//
// ---------------------------------------------------------------------
void CButton::GetPosition(RECT * prcOut)
... {
*prcOut=m_rcWndPos;
}
// --------------------------------------------------------------------
// Description:
// Setthetransparentcolor
//
// -------------------------------------------------------------------
void CButton::SetTransparentColor(COLORREFcrColor)
... {
m_crTranColor=crColor;
}
// --------------------------------------------------------------------
// Description:
// Setthetransparentmode
//
// Parameters:
// bTran:[in]
// TRUE-Don'tdrawthetransparentcolor
// FALSE-Drawallthecolor
//
// -------------------------------------------------------------------
void CButton::SetTransparent(BOOLbTran)
... {
m_bTran=bTran;
}