CButton使用详解

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                //========================================================================
//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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值