天天看點

mfc界面為按鈕填充顔色

基于cbutton類添加一個cmybutton類

頭檔案:
#pragma once

#include "afxwin.h"
// CMyButton

class CMyButton : public CButton

{

//DECLARE_DYNAMIC(CMyButton)

public:

CMyButton();

virtual ~CMyButton();

//設定Button Down的背景顔色

void SetDownColor(COLORREF color);

//設定Button Up的背景顔色

void SetUpColor(COLORREF color);

BOOL Attach(const UINT nID, CWnd* pParent);

protected:
//必需重載的函數
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);

public:

//三種顔色分别為文字,Button Down的背景顔色,Button Up的背景顔色

COLORREF m_TextColor, m_DownColor,m_UpColor;

};
           
// MyButton.cpp : 實作檔案
//

#include "stdafx.h"
#include "mfc64.h"
#include "MyButton.h"


// CMyButton

CMyButton::CMyButton(void)

{

m_DownColor = m_UpColor = RGB(0,0,0);//初始化設為黑色

}

CMyButton::~CMyButton(void)

{

}


BOOL CMyButton::Attach(const UINT nID,CWnd* pParent)

{

if (!SubclassDlgItem(nID, pParent))

return FALSE;

return TRUE;

}

void CMyButton::SetDownColor(COLORREF color)

{

m_DownColor = color;

}

void CMyButton::SetUpColor(COLORREF color)

{

m_UpColor = color;

}

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)

{

CDC dc;

dc.Attach(lpDrawItemStruct->hDC);//得到繪制的裝置環境CDC

VERIFY(lpDrawItemStruct->CtlType==ODT_BUTTON);

//得當Button上文字,這裡的步驟是:1,先得到在資源裡編輯的按鈕的文字,

//然後将此文字重新繪制到按鈕上,

//同時将此文字的背景色設為透明,這樣,按鈕上僅會顯示文字

const int bufSize = 512;

TCHAR buffer[bufSize];

GetWindowText(buffer, bufSize);

int size=strlen(buffer);//得到長度

DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);//繪制文字

SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);//透明

if (lpDrawItemStruct->itemState&ODS_SELECTED)//當按下按鈕時的處理

{重繪整個控制

CBrush brush(m_DownColor);

dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//利用畫刷brush,填充矩形框

//因為這裡進行了重繪,是以文字也要重繪

DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);

SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);

}

else//當按鈕不操作或者彈起時

{

CBrush brush(m_UpColor);

dc.FillRect(&(lpDrawItemStruct->rcItem),&brush);//

DrawText(lpDrawItemStruct->hDC,buffer,size,&lpDrawItemStruct->rcItem,DT_CENTER|DT_VCENTER|DT_SINGLELINE|DT_TABSTOP);

SetBkMode(lpDrawItemStruct->hDC,TRANSPARENT);

}

if ((lpDrawItemStruct->itemState&ODS_SELECTED)&&(lpDrawItemStruct->itemAction &(ODA_SELECT|ODA_DRAWENTIRE)))

{//選中了本控件,高亮邊框

COLORREF fc=RGB(255-GetRValue(m_UpColor),255-GetGValue(m_UpColor),255-GetBValue(m_UpColor));

CBrush brush(fc);

dc.FrameRect(&(lpDrawItemStruct->rcItem),&brush);//用畫刷brush,填充矩形邊框

}

if (!(lpDrawItemStruct->itemState &ODS_SELECTED) &&(lpDrawItemStruct->itemAction & ODA_SELECT))

{

CBrush brush(m_UpColor); //控制的選中狀态結束,去掉邊框

dc.FrameRect(&lpDrawItemStruct->rcItem,&brush);//}

dc.Detach();

}

}

           

在onintdialog裡添加

//将按鈕修改為BS_OWNERDRAW風格,允許button的采用自繪模式
GetDlgItem(IDC_BUTTON1)->ModifyStyle(0,BS_OWNERDRAW,0);
//綁定控件IDC_BUTTON1與類CMyButton,響應重載函數DrawItem()
m_Btn.Attach(IDC_BUTTON1,this);
//設定Button Down的背景色,SetDownColor()和SetUpnColor()是CMyButton類中的析構函數
m_Btn.SetDownColor(RGB(193,255,193));
//設定Button Up的背景色,沒按下之前
m_Btn.SetUpColor(RGB(229, 245, 255));
           

在頭檔案裡定義 CMyButton m_Btn;就ok了