CButton类的继承+重写--本类来自孙鑫老师C++课堂

本类摘自孙鑫老师C++课堂,如果转载请注明!

直接将头文件和CPP文件加入工程,利用类成员函数对Button控件进行设置(颜色、边框、背景等。。。)

头文件↓

//
// Class: CButtonST
//
// Compiler: Visual C++
// Tested on: Visual C++ 5.0
//
// Version: See GetVersionC() or GetVersionI()
//
// Created: xx/xxxx/1998
// Updated: 19/September/1999
//
// Author: Davide Calabro' davide_calabro@yahoo.com
//
#ifndef _BTNST_H
#define _BTNST_H


#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000


// CBtnST.h : header file
//


// Comment this if you don't want that CButtonST hilights itself
// also when the window is inactive (like happens in Internet Explorer)
//#define ST_LIKEIE


// Comment this if you don't want to use CMemDC class
//#define ST_USE_MEMDC


/
// CButtonST window


class CButtonST : public CButton
{
// Construction
public:
    CButtonST();
~CButtonST();
    enum {ST_ALIGN_HORIZ, ST_ALIGN_VERT, ST_ALIGN_HORIZ_RIGHT};


// Attributes
public:


// Operations
public:


// Overrides
// ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CButtonST)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL


// Implementation
public:
void DrawTransparent(BOOL bRepaint = FALSE);


BOOL GetDefault();


void SetTooltipText(int nId, BOOL bActivate = TRUE);
void SetTooltipText(CString* spText, BOOL bActivate = TRUE);
void ActivateTooltip(BOOL bEnable = TRUE);


BOOL SetBtnCursor(int nCursorId = -1);


void SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = FALSE);
BOOL GetFlatFocus();


void SetDefaultActiveFgColor(BOOL bRepaint = FALSE);
void SetActiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetActiveFgColor();

void SetDefaultActiveBgColor(BOOL bRepaint = FALSE);
void SetActiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetActiveBgColor();

void SetDefaultInactiveFgColor(BOOL bRepaint = FALSE);
void SetInactiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetInactiveFgColor();


void SetDefaultInactiveBgColor(BOOL bRepaint = FALSE);
void SetInactiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetInactiveBgColor();


void SetShowText(BOOL bShow = TRUE);
BOOL GetShowText();


void SetAlign(int nAlign);
int GetAlign();


void SetFlat(BOOL bState = TRUE);
BOOL GetFlat();


void DrawBorder(BOOL bEnable = TRUE);
void SetIcon(int nIconInId, int nIconOutId = NULL);
void SetIcon(HICON hIconIn, HICON hIconOut = NULL);


static const short GetVersionI();
static const char* GetVersionC();


protected:
    //{{AFX_MSG(CButtonST)
afx_msg void OnCaptureChanged(CWnd *pWnd);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnSysColorChange();
//}}AFX_MSG


afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);


DECLARE_MESSAGE_MAP()
private:
void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled);
void InitToolTip();
void PaintBk(CDC* pDC);


int m_nAlign;
BOOL m_bShowText;
BOOL m_bDrawBorder;
BOOL m_bIsFlat;
BOOL m_MouseOnButton;
BOOL m_bDrawFlatFocus;


HCURSOR m_hCursor;
CToolTipCtrl m_ToolTip;


HICON m_hIconIn;
HICON m_hIconOut;
BYTE m_cyIcon;
BYTE m_cxIcon;


CDC m_dcBk;
CBitmap m_bmpBk;
CBitmap* m_pbmpOldBk;
BOOL m_bDrawTransparent;


BOOL m_bIsDefault;


COLORREF  m_crInactiveBg;
    COLORREF  m_crInactiveFg;
    COLORREF  m_crActiveBg;
    COLORREF  m_crActiveFg;
};




#ifdef ST_USE_MEMDC
//
// CMemDC - memory DC
//
// Author: Keith Rule
// Email:  keithr@europa.com
// Copyright 1996-1997, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// History - 10/3/97 Fixed scrolling bug.
//                   Added print support.
//           25 feb 98 - fixed minor assertion bug
//
// This class implements a memory Device Context


class CMemDC : public CDC
{
public:


    // constructor sets up the memory DC
    CMemDC(CDC* pDC) : CDC()
    {
        ASSERT(pDC != NULL);


        m_pDC = pDC;
        m_pOldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
              
        if (m_bMemDC)    // Create a Memory DC
        {
            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        // Make a copy of the relevent parts of the current DC for printing
        {
            m_bPrinting = pDC->m_bPrinting;
            m_hDC       = pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }
    }
    
    // Destructor copies the contents of the mem DC to the original DC
    ~CMemDC()
    {
        if (m_bMemDC) 
        {    
            // Copy the offscreen bitmap onto the screen.
            m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
                          this, m_rect.left, m_rect.top, SRCCOPY);


            //Swap back the original bitmap.
            SelectObject(m_pOldBitmap);
        } else {
            // All we need to do is replace the DC with an illegal value,
            // this keeps us from accidently deleting the handles associated with
            // the CDC that was passed to the constructor.
            m_hDC = m_hAttribDC = NULL;
        }
    }


    // Allow usage as a pointer
    CMemDC* operator->() {return this;}
        
    // Allow usage as a pointer
    operator CMemDC*() {return this;}


private:
    CBitmap  m_bitmap;      // Offscreen bitmap
    CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
    CDC*     m_pDC;         // Saves CDC passed in constructor
    CRect    m_rect;        // Rectangle of drawing area.
    BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
};


#endif

源文件↓

//
// Class: CButtonST
//
// Compiler: Visual C++
// Tested on: Visual C++ 5.0
//
// Version: See GetVersionC() or GetVersionI()
//
// Created: xx/xxxx/1998
// Updated: 19/September/1999
//
// Author: Davide Calabro' davide_calabro@yahoo.com
//
#ifndef _BTNST_H
#define _BTNST_H


#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000


// CBtnST.h : header file
//


// Comment this if you don't want that CButtonST hilights itself
// also when the window is inactive (like happens in Internet Explorer)
//#define ST_LIKEIE


// Comment this if you don't want to use CMemDC class
//#define ST_USE_MEMDC


/
// CButtonST window


class CButtonST : public CButton
{
// Construction
public:
    CButtonST();
~CButtonST();
    enum {ST_ALIGN_HORIZ, ST_ALIGN_VERT, ST_ALIGN_HORIZ_RIGHT};


// Attributes
public:


// Operations
public:


// Overrides
// ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CButtonST)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL


// Implementation
public:
void DrawTransparent(BOOL bRepaint = FALSE);


BOOL GetDefault();


void SetTooltipText(int nId, BOOL bActivate = TRUE);
void SetTooltipText(CString* spText, BOOL bActivate = TRUE);
void ActivateTooltip(BOOL bEnable = TRUE);


BOOL SetBtnCursor(int nCursorId = -1);


void SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = FALSE);
BOOL GetFlatFocus();


void SetDefaultActiveFgColor(BOOL bRepaint = FALSE);
void SetActiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetActiveFgColor();

void SetDefaultActiveBgColor(BOOL bRepaint = FALSE);
void SetActiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetActiveBgColor();

void SetDefaultInactiveFgColor(BOOL bRepaint = FALSE);
void SetInactiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetInactiveFgColor();


void SetDefaultInactiveBgColor(BOOL bRepaint = FALSE);
void SetInactiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetInactiveBgColor();


void SetShowText(BOOL bShow = TRUE);
BOOL GetShowText();


void SetAlign(int nAlign);
int GetAlign();


void SetFlat(BOOL bState = TRUE);
BOOL GetFlat();


void DrawBorder(BOOL bEnable = TRUE);
void SetIcon(int nIconInId, int nIconOutId = NULL);
void SetIcon(HICON hIconIn, HICON hIconOut = NULL);


static const short GetVersionI();
static const char* GetVersionC();


protected:
    //{{AFX_MSG(CButtonST)
afx_msg void OnCaptureChanged(CWnd *pWnd);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnSysColorChange();
//}}AFX_MSG


afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);


DECLARE_MESSAGE_MAP()
private:
void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled);
void InitToolTip();
void PaintBk(CDC* pDC);


int m_nAlign;
BOOL m_bShowText;
BOOL m_bDrawBorder;
BOOL m_bIsFlat;
BOOL m_MouseOnButton;
BOOL m_bDrawFlatFocus;


HCURSOR m_hCursor;
CToolTipCtrl m_ToolTip;


HICON m_hIconIn;
HICON m_hIconOut;
BYTE m_cyIcon;
BYTE m_cxIcon;


CDC m_dcBk;
CBitmap m_bmpBk;
CBitmap* m_pbmpOldBk;
BOOL m_bDrawTransparent;


BOOL m_bIsDefault;


COLORREF  m_crInactiveBg;
    COLORREF  m_crInactiveFg;
    COLORREF  m_crActiveBg;
    COLORREF  m_crActiveFg;
};




#ifdef ST_USE_MEMDC
//
// CMemDC - memory DC
//
// Author: Keith Rule
// Email:  keithr@europa.com
// Copyright 1996-1997, Keith Rule
//
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
//
// History - 10/3/97 Fixed scrolling bug.
//                   Added print support.
//           25 feb 98 - fixed minor assertion bug
//
// This class implements a memory Device Context


class CMemDC : public CDC
{
public:


    // constructor sets up the memory DC
    CMemDC(CDC* pDC) : CDC()
    {
        ASSERT(pDC != NULL);


        m_pDC = pDC;
        m_pOldBitmap = NULL;
        m_bMemDC = !pDC->IsPrinting();
              
        if (m_bMemDC)    // Create a Memory DC
        {
            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        // Make a copy of the relevent parts of the current DC for printing
        {
            m_bPrinting = pDC->m_bPrinting;
            m_hDC       = pDC->m_hDC;
            m_hAttribDC = pDC->m_hAttribDC;
        }
    }
    
    // Destructor copies the contents of the mem DC to the original DC
    ~CMemDC()
    {
        if (m_bMemDC) 
        {    
            // Copy the offscreen bitmap onto the screen.
            m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
                          this, m_rect.left, m_rect.top, SRCCOPY);


            //Swap back the original bitmap.
            SelectObject(m_pOldBitmap);
        } else {
            // All we need to do is replace the DC with an illegal value,
            // this keeps us from accidently deleting the handles associated with
            // the CDC that was passed to the constructor.
            m_hDC = m_hAttribDC = NULL;
        }
    }


    // Allow usage as a pointer
    CMemDC* operator->() {return this;}
        
    // Allow usage as a pointer
    operator CMemDC*() {return this;}


private:
    CBitmap  m_bitmap;      // Offscreen bitmap
    CBitmap* m_pOldBitmap;  // bitmap originally found in CMemDC
    CDC*     m_pDC;         // Saves CDC passed in constructor
    CRect    m_rect;        // Rectangle of drawing area.
    BOOL     m_bMemDC;      // TRUE if CDC really is a Memory DC.
};


#endif


/


//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.


#endif


/


//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.


#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值