本类摘自孙鑫老师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

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

  1. 按要求编写一个Java应用程序:(1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。(2)编写一个类,继承自矩形类,同时该类描述长方体,具有长、宽、高属性

    (1)定义一个类,描述一个矩形,包含有长.宽两种属性,和计算面积方法. public class Jvcs {private int Long; //长private int wide; //宽pub ...

  2. 设计复数类 Complex继承于数字类 Number,私有成员变量包括 double类型的实部rea(父类 Number中已定义)和虚部imag,公有成员方法包括两个构造方法(一个不带参数的和一个带两

    ​ 设计复数类 Complex继承于数字类 Number,私有成员变量包括 double类型的实部rea(父类 Number中已定义)和虚部imag,公有成员方法包括两个构造方法(一个不带参数的和一个 ...

  3. 在android studio中如何创建一个类来继承另外一个类_在Android使用Transition API检测用户活动...

    在当今世界,移动设备是我们日常生活中必不可少的一部分,我们在走路.跑步.开车以及其他许多活动时都会使用移动设备. 了解用户拿着手机的时候在做什么,可以让你的应用程序根据用户的动作进行直观的调整.对于某 ...

  4. php购物网站类的继承和多态,类的继承与多态

    实例 /** * 对象三要素之: 继承与多态 * 1. 继承是指类之间的继承,是代码复用的重要手段,之前我们是通过"函数"实现的代码复用 * 2. 继承使用关键字: extends ...

  5. 【python】-- 类的继承(新式类/经典类)、多态

    继承 之前我们说到了类的公有属性和类的私有属性,其实就是类的封装,现在准备随笔的 是继承,是面向对象的第二大特性. 面向对象编程 (OOP) 语言的一个主要功能就是"继承".继承是 ...

  6. python类的继承关系,python——类和类之间的关系(继承多态)

    1.类和类之间的关系有三种:is-a.has-a和use-a关系. is-a关系也叫继承或泛化,比如学生和人的关系.手机和电子产品的关系都属于继承关系. has-a关系通常称之为关联,比如部门和员工的 ...

  7. Java类的继承学生研究生类图_UML part3 类图、对象图

    UML part3 类图.对象图 四种关系 1. 依赖关系 ①定义:依赖描述了两个模型元素之间的关系,如果被依赖的模型元素发生变化就会影响到另一个模型元素 ②举例:动物有生命和水进行繁殖,动物和水,动 ...

  8. python类的继承优缺点_python 类的三大特性--继承

    继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题, 继承是一种创建新类的方式,在python中新建的类可以继承一个或多个父类,父 ...

  9. C++ 类的继承,基类,派生类

    继承: 当创建一个类时,不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可. 这个已有的类称为基类,新建的类称为派生类. 例: //基类class Animal {// ...

最新文章

  1. Go 学习笔记(81)— Go 性能分析工具 pprof
  2. 启动MongoDB shell客户端会什么会一闪而过
  3. 好用的 php类,一个好用的php文件上传处理类
  4. SRV记录用来标识某台服务器使用了某个服务,常见于微软系统的目录管理——深入的话需要去折腾Azure Active Directory...
  5. ABAP--如何快速从BSEG读取数据
  6. postgresql学习笔记(三)连接管理
  7. [渝粤教育] 广东-国家-开放大学 21秋期末考试基础会计10258k2
  8. 网页 添加QQ/MSN/旺旺 在线聊天代码
  9. java 加密 encrypt_JAVA Encrypter 加密算法
  10. 一个90后草根站长的内心独白
  11. 高通qca-wifi移植
  12. React 实现 PDF 文件在线预览 - 手把手教你写 React PDF 预览功能
  13. C# 透明窗体制作方法
  14. JavaScript中onblur事件
  15. 一个邮箱联结全球?也许不会是遥不可及的梦想
  16. convert_variables_to_constants()
  17. 程序员:迟到1分钟罚300,加班却视而不见,月底工资让人寒心
  18. 应用场景一:西门子PLC通过桥接器连接MQTT服务器
  19. 西安交通大学护理学计算机考试,西安交通大学护理专业怎么样
  20. python turtle绘制圣诞树,就一棵破松树

热门文章

  1. git出现的若干问题以及解决方案
  2. 布丁浅谈之Linux常用基本命令
  3. MySQL插入数据与系统时间相差12小时
  4. 没有经验该如何转行做产品经理?
  5. 解决pip无法更新问题的简单方法:You are using pip version 20.1.2, however version 20.2.2 is available.......问题 的完
  6. 自动化部署工具瓦力(walle)的安装
  7. 【漏洞复现】MS17-010漏洞(永恒之蓝)复现
  8. 版本控制系统工作模式_繁星漫天_新浪博客
  9. Mondrian 4: Get ready!
  10. 百分点科技位居中国数据治理解决方案市场第二