第二步:然后将自己需要的BitMap文件导入。
第三步:在项目工程的CARMDlg::OnInitDialog()中添加如下代码:
SetIcon(m_hIcon,TRUE); // Set big icon
SetIcon(m_hIcon,FALSE); // Set small icon
m_bkg.LoadBitmap(IDB_BMP_BKG);//系统的背景颜色
m_nCollectGoods.SetBitmap(IDB_BITMAP_BUTTON,BUTTON_STATE_UP);
m_nCollectGoods.SetBitmap(IDB_BITMAP_BUTTONB,BUTTON_STATE_DOWN);
m_nCollectGoods.HideText(FALSE);
m_nCollectGoods.FontStyle(L"MS Sans Serif", 14,8,TRUE, FALSE);
m_nCollectGoods.SetTextColor(RGB(255,0,0));
第四步:在CARMDlg::OnPaint()中添加如下代码,实现对窗口的重绘:
//CDialog::OnPaint();
CPaintDC dc(this);
CBrush brush;
brush.CreatePatternBrush(&m_bkg);
CBrush *pOld=dc.SelectObject(&brush);
CRect rc;
GetClientRect(rc);
dc.FillRect(rc,&brush);
dc.SelectObject(pOld);
pOld->DeleteObject();
brush.DeleteObject();
需要注意的地方:
1. 在添加按钮对应的成员变量的时候,类型应该选择CMyButton而不是CButton
#if !defined(AFX_MYBUTTON_H__10830B07_8C08_492A_8AC9_8B3CF805C775__INCLUDED_)
#define AFX_MYBUTTON_H__10830B07_8C08_492A_8AC9_8B3CF805C775__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyButton.h :header file
//
//#include"ZTGDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CMyButtonwindow
typedef enum{
BUTTON_STATE_UP=0,
BUTTON_STATE_DOWN,
BUTTON_STATE_DISABLE
}BUTTON_STATE;
class CMyButton :public CButton
{
// Construction
public:
CMyButton();
// Attributes
public:
// Operations
public:
void PreSubclassWindow();
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
BOOL SetBitmap(int ID,BUTTON_STATEs=BUTTON_STATE_UP);
BOOL SetBitmap(LPCTSTR path,BUTTON_STATE s=BUTTON_STATE_UP);
inline void HideText(BOOLbHide = TRUE) {m_bHideText = bHide; }
void SetTextColor(COLORREF TextColor = RGB(0,0,0));
BOOL FontStyle(CString sFont , int iHeight,int iWidth, BOOL bFont3D, BOOLbConcave);
void PaintText(HDC h_clientDC,CRect m_rc);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyButton)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMyButton();
// Generated message map functions
protected:
//{{AFX_MSG(CMyButton)
afx_msg void OnLButtonDown(UINTnFlags,CPoint point);
afx_msg void OnLButtonUp(UINTnFlags,CPoint point);
afx_msg void OnEnable(BOOLbEnable);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
BOOL m_isdown;
BOOL m_isable;
CBitmap m_bitmapUp;
CBitmap m_bitmapDown;
CBitmap m_bitmapDisable;
CFont m_Font;
BOOL m_bFontCreated;
COLORREF m_TextColor;
BOOL m_bFont3D;
BOOL m_bConcave;
BOOL m_bHideText;
HANDLE m_hFile;
LPCSTR m_lpWave;
};
//{{AFX_INSERT_LOCATION}}
// MicrosoftVisual C++ will insert additional declarations immediately before the previousline.
#endif //!defined(AFX_MYBUTTON_H__10830B07_8C08_492A_8AC9_8B3CF805C775__INCLUDED_)
// MyButton.cpp :implementation file
//
#include "stdafx.h"
//#include"ZTGDlg.h"
//#include"ZTG.h"
#include "MyButton.h"
#ifdef _DEBUG
#define newDEBUG_NEW
#undef THIS_FILE
static charTHIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_isable = TRUE;
m_isdown = FALSE;
m_bFontCreated =FALSE;
m_TextColor =RGB(0,0,0);
m_bFont3D =FALSE;
m_bConcave =FALSE;
m_bHideText =TRUE;
m_hFile =INVALID_HANDLE_VALUE;
m_lpWave =NULL;
}
CMyButton::~CMyButton()
{
if(m_hFile !=INVALID_HANDLE_VALUE)
{
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
}
if(m_lpWave !=NULL)
{
free((void*)m_lpWave);
m_lpWave = NULL;
}
if(m_bFontCreated)
{
m_Font.DeleteObject();
}
}
void CMyButton::PreSubclassWindow()
{
ModifyStyle(0,BS_OWNERDRAW);
}
void CMyButton::DrawItem(LPDRAWITEMSTRUCTlpDrawItemStruct)
{
CRect m_rc;
CDC m_mem;
CDC *pDC=NULL;
BITMAP m_bmp ;
CBitmap *m_oldBitmap =NULL;
CString sCaption;
HDC h_clientDC;
h_clientDC = lpDrawItemStruct->hDC;
pDC =CDC::FromHandle(lpDrawItemStruct->hDC);
GetWindowText(sCaption);
m_rc = lpDrawItemStruct->rcItem;
pDC->SetBkMode(TRANSPARENT);
m_mem.CreateCompatibleDC(pDC);
if(!m_isable)
{
m_oldBitmap = m_mem.SelectObject(&m_bitmapDisable);
m_bitmapDisable.GetBitmap(&m_bmp);
}
else if(m_isdown)
{
m_oldBitmap = m_mem.SelectObject(&m_bitmapDown);
m_bitmapDown.GetBitmap(&m_bmp);
}
else
{
m_oldBitmap = m_mem.SelectObject(&m_bitmapUp);
m_bitmapUp.GetBitmap(&m_bmp);
}
pDC->StretchBlt(0,0,m_rc.right-m_rc.left,m_rc.bottom-m_rc.top,&m_mem,0,0,m_bmp.bmWidth,m_bmp.bmHeight,SRCCOPY);
pDC->SelectObject(m_oldBitmap);
if(m_bHideText ==FALSE)
{
PaintText(h_clientDC,m_rc);
}
m_mem.DeleteDC();
}
BOOL CMyButton::SetBitmap(intID,BUTTON_STATEs/* =BUTTON_STATE_UP */)
{
if(s ==BUTTON_STATE_UP)
return m_bitmapUp.LoadBitmap(ID);
if(s ==BUTTON_STATE_DOWN)
return m_bitmapDown.LoadBitmap(ID);
if(s ==BUTTON_STATE_DISABLE)
return m_bitmapDisable.LoadBitmap(ID);
return FALSE;
}
BOOL CMyButton::SetBitmap(LPCTSTRpath,BUTTON_STATEs/* =BUTTON_STATE_UP */)
{
if(s ==BUTTON_STATE_UP)
return m_bitmapUp.LoadBitmap(path);
if(s ==BUTTON_STATE_DOWN)
return m_bitmapDown.LoadBitmap(path);
if(s ==BUTTON_STATE_DISABLE)
return m_bitmapDisable.LoadBitmap(path);
return FALSE;
}
void CMyButton::PaintText(HDCh_clientDC, CRectm_rc)
{
CFont* def_font;
CString sCaption;
CDC *pDC =CDC::FromHandle(h_clientDC);
GetWindowText(sCaption);
if(m_bFontCreated)
{
def_font = pDC->SelectObject(&m_Font);
}
pDC->SetBkMode(TRANSPARENT);
if(m_bFont3D)
{
if(m_bConcave)
{
pDC->SetTextColor(RGB(255,255,255));
pDC->DrawText(sCaption,m_rc, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
pDC->SetTextColor(m_TextColor);
m_rc.left=m_rc.left- 1;
m_rc.top=m_rc.top- 1;
m_rc.bottom =m_rc.bottom - 1;
m_rc.right=m_rc.right- 1;
pDC->DrawText(sCaption,m_rc, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
else
{
pDC->SetTextColor(RGB(255,255,255));
pDC->DrawText(sCaption,m_rc, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
pDC->SetTextColor(m_TextColor);
m_rc.left=m_rc.left+ 1;
m_rc.top=m_rc.top+ 1;
m_rc.bottom =m_rc.bottom + 1;
m_rc.right=m_rc.right+ 1;
pDC->DrawText(sCaption,m_rc, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
}
else
{
pDC->SetTextColor(m_TextColor);
pDC->DrawText(sCaption,m_rc, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
}
if(m_bFontCreated)
{
pDC->SelectObject(def_font);
}
}
BOOL CMyButton::FontStyle(CStringsFont, int iHeight, int iWidth,BOOL bFont3D, BOOL bConcave)
{
if(m_bFontCreated)
{
m_Font.DeleteObject();
}
if(0 != m_Font.CreateFont(iHeight,iWidth, 0, 0,
FW_BOLD,//FW_NORMAL,
0, 0, 0, 0,0, 0, 0,0, sFont))
{
m_bFontCreated =TRUE;
m_bFont3D = bFont3D;
m_bConcave = bConcave;
return TRUE;
}
else
{
m_bFontCreated =FALSE;
return FALSE;
}
}
void CMyButton::SetTextColor(COLORREFTextColor)
{
m_TextColor = TextColor;
return;
}
BEGIN_MESSAGE_MAP(CMyButton,CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_ENABLE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButtonmessage handlers
void CMyButton::OnLButtonDown(UINTnFlags,CPoint point)
{
m_isdown = TRUE;
CButton::OnLButtonDown(nFlags,point);
}
void CMyButton::OnLButtonUp(UINTnFlags,CPoint point)
{
m_isdown = FALSE;
CButton::OnLButtonUp(nFlags,point);
}
void CMyButton::OnEnable(BOOLbEnable)
{
CButton::OnEnable(bEnable);
m_isable = bEnable;
}