可以自己从CButton中派生一个类,具体实现方法类似于CBitmapButton,然后在DrawItem中用pDC->TextOut()来显示标题,标题相同无所谓,只要ID不同就可以。超过256色的位图可以用工具(ACDSee or Photoshop)先转成gif格式,然后再由gif格式的图片转成bmp格式的就可以了。
下面是我写的CButton派生出来的类,仅供参考
void CBitmapBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct != NULL);
// must have at least the first bitmap loaded before calling DrawItem
ASSERT(m_bitmap.m_hObject != NULL); // required
// use the main bitmap for up, the selected bitmap for down
CBitmap* pBitmap = &m_bitmap;
UINT state = lpDrawItemStruct->itemState;
if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)
pBitmap = &m_bitmapSel;
else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)
pBitmap = &m_bitmapFocus; // third image for focused
else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)
pBitmap = &m_bitmapDisabled; // last image for disabled
// draw the whole button
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap* pOld = memDC.SelectObject(pBitmap);
if (pOld == NULL)
return; // destructors will clean up
// LoadBitmaps will load in one, two, three or all four bitmaps
// returns TRUE if all specified images are loaded
BOOL CBitmapBtn::LoadBitmaps(LPCTSTR lpszBitmapResource,
LPCTSTR lpszBitmapResourceSel, LPCTSTR lpszBitmapResourceFocus,
LPCTSTR lpszBitmapResourceDisabled)
{
// delete old bitmaps (if present)
m_bitmap.DeleteObject();
m_bitmapSel.DeleteObject();
m_bitmapFocus.DeleteObject();
m_bitmapDisabled.DeleteObject();
if (!m_bitmap.LoadBitmap(lpszBitmapResource))
{
TRACE0("Failed to load bitmap for normal image.\n");
return FALSE; // need this one image
}
BOOL bAllLoaded = TRUE;
if (lpszBitmapResourceSel != NULL)
{
if (!m_bitmapSel.LoadBitmap(lpszBitmapResourceSel))
{
TRACE0("Failed to load bitmap for selected image.\n");
bAllLoaded = FALSE;
}
}
if (lpszBitmapResourceFocus != NULL)
{
if (!m_bitmapFocus.LoadBitmap(lpszBitmapResourceFocus))
bAllLoaded = FALSE;
}
if (lpszBitmapResourceDisabled != NULL)
{
if (!m_bitmapDisabled.LoadBitmap(lpszBitmapResourceDisabled))
bAllLoaded = FALSE;
}
return bAllLoaded;
}
// SizeToContent will resize the button to the size of the bitmap
void CBitmapBtn::SizeToContent()
{
ASSERT(m_bitmap.m_hObject != NULL);
CSize bitmapSize;
BITMAP bmInfo;
VERIFY(m_bitmap.GetObject(sizeof(bmInfo), &bmInfo) == sizeof(bmInfo));
VERIFY(SetWindowPos(NULL, -1, -1, bmInfo.bmWidth, bmInfo.bmHeight+15,
SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
}
// Autoload will load the bitmap resources based on the text of
// the button
// Using suffices "U", "D", "F" and "X" for up/down/focus/disabled
BOOL CBitmapBtn::AutoLoad(UINT nID, CWnd* pParent)
{
// first attach the CBitmapButton to the dialog control
// if (!SubclassDlgItem(nID, pParent))
// return FALSE;
CString buttonName;
GetWindowText(buttonName);
ASSERT(!buttonName.IsEmpty()); // must provide a title
// Create the bitmap button (must include the BS_OWNERDRAW style).
myButton.Create(NULL, WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,
CRect(10,10,100,100), pParentWnd, 1);
// Load the bitmaps for this button.
myButton.LoadBitmaps(IDB_UP, IDB_DOWN, IDB_FOCUS, IDB_DISABLE);