home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c031 / 10.ddi / MFC / SRC / WINBTN.CP$ / winbtn
Encoding:
Text File  |  1992-03-16  |  3.6 KB  |  126 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 documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11.  
  12. #include "afxwin.h"
  13. #pragma hdrstop
  14.  
  15. #ifdef AFX_CORE_SEG
  16. #pragma code_seg(AFX_CORE_SEG)
  17. #endif
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #define new DEBUG_NEW
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CBitmapButton
  27.  
  28. IMPLEMENT_DYNAMIC(CBitmapButton, CButton)
  29.  
  30. // LoadBitmaps will load in the two bitmaps
  31. BOOL CBitmapButton::LoadBitmaps(LPCSTR lpBitmapResource,
  32.     LPCSTR lpBitmapResourceSel, LPCSTR lpBitmapResourceFocus /* = NULL */)
  33. {
  34.     m_bitmap.DeleteObject();
  35.     if (!m_bitmap.LoadBitmap(lpBitmapResource))
  36.     {
  37.         TRACE("Failed to load bitmap for normal image\n");
  38.         return FALSE;
  39.     }
  40.     if (lpBitmapResourceSel != NULL)
  41.     {
  42.         m_bitmapSel.DeleteObject();
  43.         if (!m_bitmapSel.LoadBitmap(lpBitmapResourceSel))
  44.         {
  45.             TRACE("Failed to load bitmap for selected image\n");
  46.             return FALSE;
  47.         }
  48.     }
  49.     if (lpBitmapResourceFocus != NULL)
  50.     {
  51.         m_bitmapFocus.DeleteObject();
  52.         if (!m_bitmapFocus.LoadBitmap(lpBitmapResourceFocus))
  53.         {
  54.             TRACE("Failed to load bitmap for focused image\n");
  55.             return FALSE;
  56.         }
  57.     }
  58.     return TRUE;
  59. }
  60.  
  61. // SizeToContent will resize the button to the size of the bitmap
  62. void CBitmapButton::SizeToContent()
  63. {
  64.     ASSERT(m_bitmap.m_hObject != NULL);
  65.     CSize bitmapSize;
  66.     BITMAP bmInfo;
  67.     VERIFY(m_bitmap.GetObject(sizeof(bmInfo), &bmInfo) == sizeof(bmInfo));
  68.     VERIFY(SetWindowPos(NULL, -1, -1, bmInfo.bmWidth, bmInfo.bmHeight,
  69.         SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
  70. }
  71.  
  72. // Autoload will load the bitmap resources based on the text of
  73. //  the button
  74. // Using suffices "U", "D" and "F" for up/down/focus (must have all three)
  75. BOOL CBitmapButton::AutoLoad(UINT nID, CWnd* pParent)
  76. {
  77.     // first attach the CBitmapButton to the dialog control
  78.     if (!SubclassDlgItem(nID, pParent))
  79.         return FALSE;
  80.  
  81.     CString buttonName;
  82.     GetWindowText(buttonName);
  83.     ASSERT(!buttonName.IsEmpty());      // must provide a title
  84.  
  85.     if (!LoadBitmaps(buttonName + "U", buttonName + "D", buttonName + "F"))
  86.         return FALSE;
  87.  
  88.     // size to content
  89.     SizeToContent();
  90.     return TRUE;
  91. }
  92.  
  93. // Draw the appropriate bitmap
  94. void CBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  95. {
  96.     // must have at least the first bitmap loaded before calling DrawItem
  97.     ASSERT(m_bitmap.m_hObject != NULL);     // required
  98.  
  99.     // use the main bitmap for up, the selected bitmap for down
  100.     CBitmap* pBitmap = &m_bitmap;
  101.     if ((lpDIS->itemState & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)
  102.         pBitmap = &m_bitmapSel;
  103.     else if ((lpDIS->itemState & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)
  104.         pBitmap = &m_bitmapFocus;   // third image for focused
  105.  
  106.     if (lpDIS->itemAction & (ODA_DRAWENTIRE|ODA_SELECT|ODA_FOCUS))
  107.     {
  108.         // draw the whole button
  109.         CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  110.         CDC memDC;
  111.         memDC.CreateCompatibleDC(pDC);
  112.         CBitmap* pOld = memDC.SelectObject(pBitmap);
  113.         if (pOld == NULL)
  114.             return;     // destructors will clean up
  115.  
  116.         CRect rect;
  117.         rect.CopyRect(&lpDIS->rcItem);
  118.         pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  119.             &memDC, 0, 0, SRCCOPY);
  120.         memDC.SelectObject(pOld);
  121.         // will delete memDC
  122.     }
  123. }
  124.  
  125. /////////////////////////////////////////////////////////////////////////////
  126.