// BmpButton.cpp : implementation file // #include "stdafx.h" #include "EBook.h" #include "BmpButton.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CBmpButton CBmpButton::CBmpButton() { m_InitDC = FALSE; m_NormalDC.CreateCompatibleDC(NULL); m_DisableDC.CreateCompatibleDC(NULL); m_DownDC.CreateCompatibleDC(NULL); m_ButType = BT_NORMAL; } CBmpButton::~CBmpButton() { m_DisableDC.DeleteDC(); m_DownDC.DeleteDC(); m_NormalDC.DeleteDC(); } BEGIN_MESSAGE_MAP(CBmpButton, CButton) //{{AFX_MSG_MAP(CBmpButton) ON_WM_PAINT() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CBmpButton message handlers void CBmpButton::PreSubclassWindow() { // TODO: Add your specialized code here and/or call the base class // 在此设置按钮样式为自绘按钮 ModifyStyle(0, GetStyle()|BS_OWNERDRAW); CButton::PreSubclassWindow(); } void CBmpButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item CRect rect; GetClientRect(&rect); UINT status = lpDrawItemStruct->itemState; CClientDC dc(this); m_Status = BTS_NORMAL; dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_NormalDC,0,0,SRCCOPY); if (status & ODS_DISABLED) { dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_DisableDC,0,0,SRCCOPY); m_Status = BTS_DISABLE; } else if (status & ODS_SELECTED) { m_Status = BTS_DOWN; dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_DownDC,0,0,SRCCOPY); } } void CBmpButton::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here if (m_InitDC) { CRect rect; GetClientRect(&rect); if (IsWindowEnabled()) { dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_NormalDC,0,0,SRCCOPY); } else { dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_DisableDC,0,0,SRCCOPY); } } // Do not call CButton::OnPaint() for painting messages } BOOL CBmpButton::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default return FALSE; //return CButton::OnEraseBkgnd(pDC); } void CBmpButton::LoadBitmaps(UINT nIDBitmapResource, UINT nIDBitmapResourceSel, UINT nIDBitmapResourceDisabled) { CBitmap bmp; bmp.LoadBitmap(nIDBitmapResource); m_NormalDC.SelectObject(&bmp); bmp.DeleteObject(); if (nIDBitmapResourceSel != 0) { bmp.LoadBitmap(nIDBitmapResourceSel); m_DownDC.SelectObject(&bmp); bmp.DeleteObject(); m_ButType |= BT_DOWN; } if (nIDBitmapResourceDisabled != 0) { bmp.LoadBitmap(nIDBitmapResourceDisabled); m_DisableDC.SelectObject(&bmp); bmp.DeleteObject(); m_ButType |= BT_DISABLE; } m_InitDC = TRUE; } void CBmpButton::SetButType(int nType) { m_ButType |= nType; }