在 MFC 中,CBitmapButton 类确实有一个名为 LoadBitmaps 的公共方法。LoadBitmaps 方法用于加载按钮的位图。以下是该方法的基本形式:
BOOL CBitmapButton::LoadBitmaps(LPCTSTR lpszBitmapResource,
                                 LPCTSTR lpszBitmapResourceSel /*= NULL*/,
                                 LPCTSTR lpszBitmapResourceFocus /*= NULL*/,
                                 LPCTSTR lpszBitmapResourceDisabled /*= NULL*/)
{
   // 加载正常状态的位图
   if (!m_bitmap.Attach(::LoadImage(AfxGetResourceHandle(),
                                    lpszBitmapResource,
                                    IMAGE_BITMAP,
                                    0,
                                    0,
                                    LR_DEFAULTSIZE | LR_LOADMAP3DCOLORS)))
   {
      return FALSE;
   }

   SetBitmap(m_bitmap);

   // 如果提供了选定状态的位图资源,则加载并设置
   if (lpszBitmapResourceSel != NULL)
   {
      if (!m_bitmapSel.Attach(::LoadImage(AfxGetResourceHandle(),
                                          lpszBitmapResourceSel,
                                          IMAGE_BITMAP,
                                          0,
                                          0,
                                          LR_DEFAULTSIZE | LR_LOADMAP3DCOLORS)))
      {
         return FALSE;
      }
      SetBitmap(m_bitmapSel, BS_PRESSED);
   }

   // 如果提供了焦点状态的位图资源,则加载并设置
   if (lpszBitmapResourceFocus != NULL)
   {
      if (!m_bitmapFocus.Attach(::LoadImage(AfxGetResourceHandle(),
                                             lpszBitmapResourceFocus,
                                             IMAGE_BITMAP,
                                             0,
                                             0,
                                             LR_DEFAULTSIZE | LR_LOADMAP3DCOLORS)))
      {
         return FALSE;
      }
      SetBitmap(m_bitmapFocus, BS_HOT);
   }

   // 如果提供了禁用状态的位图资源,则加载并设置
   if (lpszBitmapResourceDisabled != NULL)
   {
      if (!m_bitmapDisabled.Attach(::LoadImage(AfxGetResourceHandle(),
                                                lpszBitmapResourceDisabled,
                                                IMAGE_BITMAP,
                                                0,
                                                0,
                                                LR_DEFAULTSIZE | LR_LOADMAP3DCOLORS)))
      {
         return FALSE;
      }
      SetBitmap(m_bitmapDisabled, BS_DISABLED);
   }

   return TRUE;
}

这个方法允许你为按钮的不同状态(正常、选定、焦点、禁用)加载不同的位图资源。你可以在你的派生类中调用这个方法,例如:
CMyBitmapButton::CMyBitmapButton()
{
   LoadBitmaps(IDB_NORMAL, IDB_SELECTED, IDB_FOCUSED, IDB_DISABLED);
   // 其他初始化工作...
}

这样,当创建 CMyBitmapButton 对象时,它将加载并显示相应状态的位图资源。请确保在调用 LoadBitmaps 方法之前,按钮控件已经创建并与父窗口关联。


转载请注明出处:http://www.zyzy.cn/article/detail/15749/MFC/CBitmapButton