//========================================================================
//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 m_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)
...
{
//Do something..
}
恩,就这么简单,也没什么很特别的地方,仅仅主要是方便大量采用贴图的程序而已.
CButton类完整源码:
/**/
//////////////////////////////////////////////////////////////////////
//
Button.h: interface for the CButton class.
//
//
Version:
//
1.1.1
//
Data:
//
2007.03.31
/**/
//////////////////////////////////////////////////////////////////////
#ifndef BUTTON_H
#define
BUTTON_H
#include
"
CtrlCommon.h
"
//
---------------------------------------------------------------------------
//
Enum data
//
For the button draw
enum
ButtonDrawType
...
{
BTN_DRAW_AUTO,
BTN_DRAW_PUSH,
BTN_DRAW_ENABLE,
BTN_DRAW_DISABLE
}
;
//
------------------------------------------------------------------------------
//
Class
class
CButton
...
{
public:
void SetTransparent(BOOL bTran);
void SetTransparentColor(COLORREF crColor);
void GetPosition(RECT *prcOut);
BOOL CheckTap(const LPPOINT ppt);
void SetPosition(const RECT * pRc);
void SetImgInfoPush(const DISPIMAGEINFO *pImgInfo);
void SetImgInfoDisable(const DISPIMAGEINFO *pImgInfo);
void SetImgInfoEnable(const DISPIMAGEINFO *pImgInfo);
void SetHinstance(HINSTANCE hInst);
BOOL Draw(HDC hdc, ButtonDrawType btnDraw = BTN_DRAW_AUTO);
void SetEnable(BOOL bEnable);
BOOL GetEnable();
CButton();
virtual ~CButton();
protected:
BOOL m_bEnable;
RECT m_rcWndPos;
DISPIMAGEINFO m_ImgPush;
DISPIMAGEINFO m_ImgEnable;
DISPIMAGEINFO m_ImgDisable;
HANDLE m_hBmpPush;
HANDLE m_hBmpEnable;
HANDLE m_hBmpDisable;
HINSTANCE m_hInst;
COLORREF m_crTranColor;
BOOL m_bTran;
}
;
#endif
//
#ifndef BUTTON_H
/**/
//////////////////////////////////////////////////////////////
//
Button.cpp: implementation of the CButton class.
//
/**/
//////////////////////////////////////////////////////////////////////
#include
"
stdafx.h
"
#include
"
Button.h
"
//
-------------------------------------------------------------------
//
Macro define
#define
DEFAULT_TRANSPARENT_COLOR RGB(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:
//
The button is enable or not
//
//
-------------------------------------------------------------------
BOOL CButton::GetEnable()
...
{
return m_bEnable;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the button status
//
//
-------------------------------------------------------------------
void
CButton::SetEnable(BOOL bEnable)
...
{
m_bEnable = bEnable;
}
//
--------------------------------------------------------------------
//
Description:
//
Draw the button
//
//
-------------------------------------------------------------------
BOOL CButton::Draw(HDC hdc, ButtonDrawType btnDraw)
...
{
if(m_hInst == NULL)
...{
return FALSE;
}
HANDLE hBmp = NULL;
PDISPIMAGEINFO pInfo;
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;
}
}
else if(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;
}
else if(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;
}
else if(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;
}
//Create a DC that matches the device
HDC hdcMem = CreateCompatibleDC(hdc);
//Select the bitmap into to the compatible device context
HGDIOBJ hOldSel = SelectObject(hdcMem,hBmp);
//Copy the bitmap image from the memory DC to the screen DC
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);
}
//Restore original bitmap selection and destroy the memory DC
SelectObject(hdcMem,hOldSel);
DeleteDC(hdcMem);
return TRUE;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the handle of instance
//
//
-------------------------------------------------------------------
void
CButton::SetHinstance(HINSTANCE hInst)
...
{
m_hInst = hInst;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the image of enable
//
//
-------------------------------------------------------------------
void
CButton::SetImgInfoEnable(
const
DISPIMAGEINFO
*
pImgInfo)
...
{
if(m_hBmpEnable != NULL)
...{
DeleteObject(m_hBmpEnable);
m_hBmpEnable = NULL;
}
m_ImgEnable = *pImgInfo;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the image of disable
//
//
-------------------------------------------------------------------
void
CButton::SetImgInfoDisable(
const
DISPIMAGEINFO
*
pImgInfo)
...
{
if(m_hBmpDisable != NULL)
...{
DeleteObject(m_hBmpDisable);
m_hBmpDisable = NULL;
}
m_ImgDisable = *pImgInfo;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the image of push
//
//
-------------------------------------------------------------------
void
CButton::SetImgInfoPush(
const
DISPIMAGEINFO
*
pImgInfo)
...
{
if(m_hBmpPush != NULL)
...{
DeleteObject(m_hBmpPush);
m_hBmpPush = NULL;
}
m_ImgPush = *pImgInfo;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the button position
//
//
-------------------------------------------------------------------
void
CButton::SetPosition(
const
RECT
*
pRc)
...
{
m_rcWndPos = *pRc;
}
//
--------------------------------------------------------------------
//
Description:
//
Check tapped position in the area.If the button is disable,
//
it would return FALSE.
//
//
-------------------------------------------------------------------
BOOL CButton::CheckTap(
const
LPPOINT ppt)
...
{
if(m_bEnable == FALSE)
...{
return FALSE;
}
if( ppt->x >= m_rcWndPos.left && ppt->x <= m_rcWndPos.right && ppt->y >= m_rcWndPos.top && ppt->y <= m_rcWndPos.bottom)
...{
return TRUE;
}
else
...{
return FALSE;
}
}
//
--------------------------------------------------------------------
//
Description:
//
Get the position as rect
//
//
---------------------------------------------------------------------
void
CButton::GetPosition(RECT
*
prcOut)
...
{
*prcOut = m_rcWndPos;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the transparent color
//
//
-------------------------------------------------------------------
void
CButton::SetTransparentColor(COLORREF crColor)
...
{
m_crTranColor = crColor;
}
//
--------------------------------------------------------------------
//
Description:
//
Set the transparent mode
//
//
Parameters:
//
bTran:[in]
//
TRUE - Don't draw the transparent color
//
FALSE - Draw all the color
//
//
-------------------------------------------------------------------
void
CButton::SetTransparent(BOOL bTran)
...
{
m_bTran = bTran;
}