1.在項目->添加類->CMyButton。這樣會自動生成兩個文件,一個.CPP文件和一個.h文件。例如CMyButton.cpp和CMyButton.h
2.在你的主工程的頭文件C**Dlg.h中添加對CMyButton.h的包含,即 #include “CMyButton.h”
3.找到OnInitDialog()函數,在該函數裏面添加如下代碼:
CMyButton m_Btn;//定義一個CMybutton的變量,可以在其他地方進行定義,只需要包含 “CMyButton.h” 即可
//將按鈕修改爲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(255,0,0));
//設置Button Up的背景色
m_Btn.SetUpColor(RGB(0,0,255));
注:若控件IDC_BUTTON1在以前進行過消息綁定,那麼就會出現bug,只需要在映射函數DoDataExchange()中將消息綁定的語句註釋掉就行了。如下:
//DDX_Control(pDX, IDC_BUTTON1, m_cbBtn);//註釋掉就可以了。
4.在新建的CMyButton.cpp文件和CMyButton.h文件中添加下面代碼即可:
頭文件:CMyButton.h如下:
#pragma once
#include "afxwin.h"
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(LPDRAWITEMSTRUCTlpDrawItemStruct);
public:
//三種顏色分別爲文字,Button Down的背景顏色,Button Up的背景顏色
COLORREF m_TextColor, m_DownColor,m_UpColor;
};
源文件:CMyButton.cpp
#include "StdAfx.h"
#include "CMyButton.h"
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(COLORREFcolor)
{ //CMyButton類的函數
m_DownColor = color;
}
void CMyButton::SetUpColor(COLORREF color)
{
m_UpColor = color;
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)
{
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();
}
關於改變AfxMessageBox對話框標題
其實這個標題,也就是我們的默認的工程名,我們應該怎麼在不改變工程名的基礎上改變標題呢?其實這個標題在資源String Table裏就能找到,查找AFX_IDS_APP_TITLE,在這裏你就能輕而易舉的改變標題了.