home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / WINBTN.CP_ / WINBTN.CP
Encoding:
Text File  |  1993-02-08  |  4.7 KB  |  159 lines

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