winbtn.cpp 4.8 KB

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