1. 程式人生 > >CButton類的繼承+重寫--本類來自孫鑫老師C++課堂

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'[email protected]
//
#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:  

[email protected]
// 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'[email protected]
//
#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:  

[email protected]
// 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++課堂

本類摘自孫鑫老師C++課堂,如果轉載請註明!直接將標頭檔案和CPP檔案加入工程,利用類成員函式對Button控制元件進行設定(顏色、邊框、背景等。。。)標頭檔案↓////Class:CButtonST////Compiler:Visual C++//Tested on:Vis

繼承重寫方法,運用多型時呼叫重寫的方法時呼叫子的還是呼叫父的?

package 第五天; public class Person { public void say() { System.out.println("我是Person的say方法"); } } 子類Student如下: package 第五天; publ

JavaSE8基礎 多態 子沒有重寫引用調用父中的方法

win tca 訪問 子類 pub ava alt lease highlight os :windows7 x64 jdk:jdk-8u131-windows-x64 ide:Eclipse Oxygen Release (4.7.0)

19. Python 重寫   的私有變量和私有方法

python 類1.類的重寫例子:class parent(object): name = ‘parent‘ age = 100 def __init__(self): print(‘my name is parent‘) def get_name(self):

逆向第十九講——繼承和成員、運算符重載、模板逆向20171211

指針 emp 繼承方式 virtual n) stp 定義 調試 不同的 一、類繼承逆向 在C++中使用到繼承,主要是為了實現多態,那麽多態就必須會用到虛函數,即會產生虛表指針。 (1)父類和子類中有沒用到虛函數的四種情形 1)父類和子類中都沒有用到

重寫的訪問控制修飾符

基本概念: 重寫也叫作覆蓋,就是當子類的成員與父類的成員的名字相同的時候,從父類繼承下來的成員會重新定義! 此時,通過子類的物件訪問的時候,真正起作用的是子類中定義的成員! 如果必須要執行被重新的父類方法,必須在子類的方法中,顯式的呼叫父類的同名方法! 語法形式為: 在子類的方法

重寫方法,重寫方法與呼叫父方法

class Animal: def eat(self): print("-----吃-----") def drink(self): print("-----喝-----") class Dog(Animal): def bark(self): print("-----汪汪

再探Java子方法重寫方法遵循“兩同兩小一大”規則

今天在牛客碰到這樣一道題: class A { public A foo() { return this; } } class B extends A { public A foo() { return this; } } class C extends

php繼承設計商品手機圖書

設計如下幾個類:商品類,有名稱,有價錢,庫存數,可顯示自身資訊(名稱,價錢,庫存)。手機類,是商品的一種,並且有品牌,有產地,可顯示自身資訊。圖書類,是商品的一種,有作者,有出版社, 可顯示自身資訊。建立一個手機物件,並顯示其自身資訊;建立一個圖書物件,並顯示其自身資訊;擴

Hibernate教程04——繼承(子、父連結表)

Hibernate教程04——類繼承(子類、父類連結表) 一、簡介 每個類都生成一張表,父類儲存公共資料,子類儲存自己獨有的資料,通過id進行關聯。 父類(Animal)和子類(Pig、Bird)均生

Hibernate對映繼承之每個一張表(父子都有各自的表,並共用一個對映檔案)

這種方案是把繼承關係表示為相關的外來鍵關聯。宣告持久化屬性的每個類/子類(包括抽象類甚至介面)都有它自己的表。不同於我們先前例子對映的每個具體類一張表的策略,此處的表僅僅包含了每個非繼承的屬性(由子類本身宣告的每個屬性)以及也是超類表的外來鍵的主鍵的列。 如例CreditCa

JAVA中子繼承了父的私有屬性方法了嗎?

一、繼承知識點簡單梳理 1、繼承會獲得父類的屬性和方法,子類還能有自己的屬性方法。       開閉原則:對修改關閉,對擴充套件開啟。 2、JAVA支援單繼承(一個子類只能有一個直接父類)、支援多重繼承(A extends B,B extends C)。       JAV

Hibernate對映繼承之每個層次結構一張表(父子共用一張表,一個對映檔案)

整個類層次結構可以被對映到單張表。這張表把所有類的所有屬性的列都包括在層次結構中。由特定行表示的具體子類通過一個型別辨別標誌列的值進行識別。這個對映策略在效能和簡單性方面都勝出一籌。它是表示多型的最佳方法---多型和非多型的查詢都執行得很好---並且更易於手工實現。不用複雜的

繼承抽象父的方法和抽象父的子的一些問題

問題:子類繼承抽象父類的方法,子類重寫全部的父類抽象方法,但是不寫方法體。那子類還是抽象的麼? 詳細:例如 父類public abstract void a();子類寫 void a(){}不寫方法體 ,可以咩。。必須要實現才可以麼。。。?隨便寫點方法體? 回答: void

java繼承中父呼叫子函式的問題

 class Parent  {  public void test()  {  System.out.println("the father's test");  }  public Parent()  {  System.out.println("It's father

VS2010編寫UDP網路通訊程式的問題(借用老師《深入VC++》書中程式)

一開始安照老師書上寫的程式敲上去後,按照先伺服器端後客戶端的編譯順序,編譯結果總是“燙燙燙”,網上查找了“燙燙燙”的原因,因為在這本書上面有個不好的程式設計習慣,他沒有把陣列初始化,所以每次輸出的是不確定值“燙燙燙”。然後除錯的過程中 發現是函式recvfrom函式的問題

熱烈慶賀老師的網站重新開放

     自從我的 Java 和 VC 教學視訊推出以來,得到了廣大學員的肯定,不少學員通過這兩套教學視訊從此走上了程式設計師的道路,可以說我推出視訊的目的已經達到。然而,也正是因為視訊受到了廣大學員的歡迎,所以網上網下都出現了某些利用盜版視訊為自己牟取利益的不法之徒,有的甚至還以欺騙錢財為目的,為了打擊這種

關於老師vc++詳解第十四章tcp網路程式設計的亂碼“燙燙”的經驗

 最近在學習孫鑫老師的tcp網路程式設計,在按照孫鑫老師教材的程式碼編寫程式,完成後卻出現一堆的亂碼“燙”,在網上搜索了下,五花八門的亂扯解釋,在這裡我總結下個人學習這一章節的經驗以及注意點:   1.編譯報錯,若確定程式無書寫錯誤,看看是否添加了 “ws2_32.lib”

繼承(父為虛方法以及子重寫

虛擬現實 real 示例代碼 eal each str ons string ide 定義父類為虛方法時需要關鍵字 virtual 子類重寫需要關鍵字 override 示例代碼: //父類 public class People { //虛方

封裝、繼承重寫、抽象

1.封裝 (1)封裝的概念:隱藏了實現細節,還要對外提供可以訪問的方式。便於呼叫者的使用。這是核心之一 (2)使用一個Java中的關鍵字也是一個修飾符 private(私有,許可權修飾符)。只要將Person的屬性和行為私有起來,這樣就無法直接訪問 (3)隱藏後,還需要提供訪問方式。只要對外