home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09963.iso / strategy / hangman.zip / PictureButton.cpp < prev    next >
C/C++ Source or Header  |  1996-01-13  |  3KB  |  98 lines

  1. // PictureButton.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Hangman32.h"
  6. #include "PictureButton.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CPictureButton
  16.  
  17. CPictureButton::CPictureButton()
  18. {
  19. }
  20.  
  21. CPictureButton::~CPictureButton()
  22. {
  23. }
  24.  
  25. // Autoload will load the bitmap resources based on the text of
  26. //  the button
  27. // Using suffices "U", "D", "F" and "X" for up/down/focus/disabled
  28. BOOL CPictureButton::AutoLoad(UINT nID, CWnd* pParent)
  29. {
  30.     // first attach the CBitmapButton to the dialog control
  31.     if (!SubclassDlgItem(nID, pParent))
  32.         return FALSE;
  33.  
  34.     CString buttonName;
  35.     GetWindowText(buttonName);
  36.     ASSERT(!buttonName.IsEmpty());      // must provide a title
  37.  
  38.     LoadBitmaps(buttonName + _T("U"), buttonName + _T("D"),
  39.       buttonName + _T("F"), buttonName + _T("X"));
  40.  
  41.     // we need at least the primary
  42.     if (m_bitmap.m_hObject == NULL)
  43.         return FALSE;
  44.  
  45.     // size to content
  46.     // SizeToContent();
  47.     return TRUE;
  48. }
  49.  
  50. // Draw the appropriate bitmap
  51. void CPictureButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  52. {
  53.     ASSERT(lpDIS != NULL);
  54.     // must have at least the first bitmap loaded before calling DrawItem
  55.     ASSERT(m_bitmap.m_hObject != NULL);     // required
  56.  
  57.     // use the main bitmap for up, the selected bitmap for down
  58.     CBitmap* pBitmap = &m_bitmap;
  59.     UINT state = lpDIS->itemState;
  60.     if ((state & ODS_SELECTED) && m_bitmapSel.m_hObject != NULL)
  61.         pBitmap = &m_bitmapSel;
  62. #ifndef _MAC
  63.     else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL)
  64. #else
  65.     else if ((state & ODS_FOCUS) && m_bitmapFocus.m_hObject != NULL &&
  66.             (GetParent()->GetStyle() & DS_WINDOWSUI))
  67. #endif
  68.         pBitmap = &m_bitmapFocus;   // third image for focused
  69.     else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)
  70.         pBitmap = &m_bitmapDisabled;   // last image for disabled
  71.  
  72.     // draw the whole button
  73.     CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  74.     CDC memDC;
  75.     memDC.CreateCompatibleDC(pDC);
  76.     CBitmap* pOld = memDC.SelectObject(pBitmap);
  77.     if (pOld == NULL)
  78.         return;     // destructors will clean up
  79.  
  80.     CRect rect;
  81.     rect.CopyRect(&lpDIS->rcItem);
  82.     BITMAP bits;
  83.     pBitmap->GetObject(sizeof(BITMAP), &bits);
  84.     pDC->StretchBlt(rect.left,rect.top,rect.Width(),rect.Height(),&memDC,0,0,
  85.                     bits.bmWidth,bits.bmHeight,SRCCOPY);
  86.     memDC.SelectObject(pOld);
  87. }
  88.  
  89.  
  90. BEGIN_MESSAGE_MAP(CPictureButton, CButton)
  91.     //{{AFX_MSG_MAP(CPictureButton)
  92.         // NOTE - the ClassWizard will add and remove mapping macros here.
  93.     //}}AFX_MSG_MAP
  94. END_MESSAGE_MAP()
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CPictureButton message handlers
  98.