home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / winbtn.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  167 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_AUX_SEG
  14. #pragma code_seg(AFX_AUX_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CBitmapButton
  26.  
  27. // LoadBitmaps will load in one, two, three or all four bitmaps
  28. // returns TRUE if all specified images are loaded
  29. BOOL CBitmapButton::LoadBitmaps(LPCTSTR lpszBitmapResource,
  30.     LPCTSTR lpszBitmapResourceSel, LPCTSTR lpszBitmapResourceFocus,
  31.     LPCTSTR lpszBitmapResourceDisabled)
  32. {
  33.     // delete old bitmaps (if present)
  34.     m_bitmap.DeleteObject();
  35.     m_bitmapSel.DeleteObject();
  36.     m_bitmapFocus.DeleteObject();
  37.     m_bitmapDisabled.DeleteObject();
  38.  
  39.     if (!m_bitmap.LoadBitmap(lpszBitmapResource))
  40.     {
  41.         TRACE0("Failed to load bitmap for normal image.\n");
  42.         return FALSE;   // need this one image
  43.     }
  44.     BOOL bAllLoaded = TRUE;
  45.     if (lpszBitmapResourceSel != NULL)
  46.     {
  47.         if (!m_bitmapSel.LoadBitmap(lpszBitmapResourceSel))
  48.         {
  49.             TRACE0("Failed to load bitmap for selected image.\n");
  50.             bAllLoaded = FALSE;
  51.         }
  52.     }
  53.     if (lpszBitmapResourceFocus != NULL)
  54.     {
  55.         if (!m_bitmapFocus.LoadBitmap(lpszBitmapResourceFocus))
  56.             bAllLoaded = FALSE;
  57.     }
  58.     if (lpszBitmapResourceDisabled != NULL)
  59.     {
  60.         if (!m_bitmapDisabled.LoadBitmap(lpszBitmapResourceDisabled))
  61.             bAllLoaded = FALSE;
  62.     }
  63.     return bAllLoaded;
  64. }
  65.  
  66. // SizeToContent will resize the button to the size of the bitmap
  67. void CBitmapButton::SizeToContent()
  68. {
  69.     ASSERT(m_bitmap.m_hObject != NULL);
  70.     CSize bitmapSize;
  71.     BITMAP bmInfo;
  72.     VERIFY(m_bitmap.GetObject(sizeof(bmInfo), &bmInfo) == sizeof(bmInfo));
  73.     VERIFY(SetWindowPos(NULL, -1, -1, bmInfo.bmWidth, bmInfo.bmHeight,
  74.         SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
  75. }
  76.  
  77. // Autoload will load the bitmap resources based on the text of
  78. //  the button
  79. // Using suffices "U", "D", "F" and "X" for up/down/focus/disabled
  80. BOOL CBitmapButton::AutoLoad(UINT nID, CWnd* pParent)
  81. {
  82.     // first attach the CBitmapButton to the dialog control
  83.     if (!SubclassDlgItem(nID, pParent))
  84.         return FALSE;
  85.  
  86.     CString buttonName;
  87.     GetWindowText(buttonName);
  88.     ASSERT(!buttonName.IsEmpty());      // must provide a title
  89.  
  90.     LoadBitmaps(buttonName + _T("U"), buttonName + _T("D"),
  91.       buttonName + _T("F"), buttonName + _T("X"));
  92.  
  93.     // we need at least the primary
  94.     if (m_bitmap.m_hObject == NULL)
  95.         return FALSE;
  96.  
  97.     // size to content
  98.     SizeToContent();
  99.     return TRUE;
  100. }
  101.  
  102. // Draw the appropriate bitmap
  103. void CBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  104. {
  105.     ASSERT(lpDIS != NULL);
  106.     // must have at least the first bitmap loaded before calling DrawItem
  107.     ASSERT(m_bitmap.m_hObject != NULL);     // required
  108.  
  109.     // use the main bitmap for up, the selected bitmap for down
  110.     CBitmap* pBitmap = &m_bitmap;
  111.     UINT state = lpDIS->itemState;
  112.     if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)
  113.         pBitmap = &m_bitmapSel;
  114.     else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)
  115.         pBitmap = &m_bitmapFocus;   // third image for focused
  116.     else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)
  117.         pBitmap = &m_bitmapDisabled;   // last image for disabled
  118.  
  119.     // draw the whole button
  120.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  121.     CDC memDC;
  122.     memDC.CreateCompatibleDC(pDC);
  123.     CBitmap* pOld = memDC.SelectObject(pBitmap);
  124.     if (pOld == NULL)
  125.         return;     // destructors will clean up
  126.  
  127.     CRect rect;
  128.     rect.CopyRect(&lpDIS->rcItem);
  129.     pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  130.         &memDC, 0, 0, SRCCOPY);
  131.     memDC.SelectObject(pOld);
  132. }
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CBitmapButton diagnostics
  136. #ifdef _DEBUG
  137. void CBitmapButton::AssertValid() const
  138. {
  139.     CButton::AssertValid();
  140.  
  141.     m_bitmap.AssertValid();
  142.     m_bitmapSel.AssertValid();
  143.     m_bitmapFocus.AssertValid();
  144.     m_bitmapDisabled.AssertValid();
  145. }
  146.  
  147. void CBitmapButton::Dump(CDumpContext& dc) const
  148. {
  149.     CButton::Dump(dc);
  150.  
  151.     dc << "m_bitmap = " << (UINT)m_bitmap.m_hObject;
  152.     dc << "\nm_bitmapSel = " << (UINT)m_bitmapSel.m_hObject;
  153.     dc << "\nm_bitmapFocus = " << (UINT)m_bitmapFocus.m_hObject;
  154.     dc << "\nm_bitmapDisabled = " << (UINT)m_bitmapDisabled.m_hObject;
  155.  
  156.     dc << "\n";
  157. }
  158. #endif
  159.  
  160. #ifdef AFX_INIT_SEG
  161. #pragma code_seg(AFX_INIT_SEG)
  162. #endif
  163.  
  164. IMPLEMENT_DYNAMIC(CBitmapButton, CButton)
  165.  
  166. /////////////////////////////////////////////////////////////////////////////
  167.