How to use CBitmapButton from MFC

MFC is a class library provided by Microsoft, from 1990s, which is pretty convenient to create simple win32 styled GUI apps with the help of visual studio. This article is going to introduce how to create a bitmap button using CBitmapButton

In order to make CBitmapButton work:

  • BS_OWNERDRAW must be set in style, take the resource file for example:
CONTROL		"CANCEL", IDCANCEL, "Button", BS_OWNERDRAW | WS_TABSTOP,110,39,40,30
  • Load Bitmap resources in dialog constructor:
//  4 bitmaps is needed: up, down, focus and disable
m_oBitmapButton.LoadBitmaps(IDB_BITMAP_UP, IDB_BITMAP_DOWN, IDB_BITMAP_FOCUS, IDB_BITMAP_DISABLE);
  • change button size in dialog initialize function:
m_oBitmapButton.SizeToContent();

The bitmap button now should be shown in the dialog.