Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.0 KiB

  1. // pictures.cpp : This is the code for the picture object
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "pictures.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static CHAR BASED_CODE THIS_FILE[] = __FILE__;
  9. #endif
  10. IMPLEMENT_DYNAMIC( CPic, CDC )
  11. #include "memtrace.h"
  12. /****************************************************************************/
  13. CPic::CPic()
  14. : CDC()
  15. {
  16. mhBitmapOld = NULL;
  17. mbReady = FALSE;
  18. /*
  19. ** set up our DC
  20. */
  21. if (! CreateCompatibleDC( NULL ))
  22. {
  23. #ifdef _DEBUG
  24. OutputDebugString( TEXT("GDI error or unable to get a DC!\r\n") );
  25. #endif
  26. }
  27. }
  28. /****************************************************************************/
  29. CPic::~CPic()
  30. {
  31. if (m_hDC)
  32. {
  33. if (mhBitmapOld)
  34. SelectObject( CBitmap::FromHandle( mhBitmapOld ) );
  35. if (mBitmap.m_hObject)
  36. mBitmap.DeleteObject();
  37. if (mMask.m_hObject)
  38. mMask.DeleteObject();
  39. }
  40. }
  41. /****************************************************************************/
  42. void CPic::Picture( CDC* pDC, int iX, int iY, int iPic )
  43. {
  44. if (! mbReady || iPic < 0 || iPic >= miCnt)
  45. return;
  46. int iPicX = iPic * mSize.cx;
  47. SelectObject( &mMask );
  48. // select FG color to be Black and BK color to be White
  49. //
  50. // The Default Mono->Color Conversion sets (Black -> FG Color, White -> BG Color)
  51. // It uses FG/BK color from the destination (color DC).
  52. // we want Black -> black, White -> white
  53. // a black/white bitmap in color format.
  54. COLORREF cRefFGColorOld = pDC->SetTextColor( RGB(0,0,0) );
  55. COLORREF cRefBKColorOld = pDC->SetBkColor(RGB(255,255,255));
  56. pDC->BitBlt( iX, iY, mSize.cx, mSize.cy, this, iPicX, 0, SRCAND );
  57. pDC->SetTextColor(cRefFGColorOld);
  58. pDC->SetBkColor(cRefBKColorOld);
  59. SelectObject( &mBitmap );
  60. pDC->BitBlt( iX, iY, mSize.cx, mSize.cy, this, iPicX, 0, SRCPAINT );
  61. }
  62. /****************************************************************************/
  63. BOOL CPic::PictureSet( LPCTSTR lpszResourceName, int iCnt )
  64. {
  65. BOOL bReturn = FALSE;
  66. /*
  67. ** get the Pictures bitmap
  68. */
  69. if (m_hDC && iCnt)
  70. if (mBitmap.LoadBitmap( lpszResourceName ))
  71. {
  72. miCnt = iCnt;
  73. bReturn = InstallPicture();
  74. }
  75. else
  76. {
  77. #ifdef _DEBUG
  78. OutputDebugString( TEXT("Unable to load the bitmap!\r\n") );
  79. #endif
  80. }
  81. return bReturn;
  82. }
  83. /****************************************************************************/
  84. BOOL CPic::PictureSet( UINT nIDResource, int iCnt )
  85. {
  86. BOOL bReturn = FALSE;
  87. /*
  88. ** get the Pictures bitmap
  89. */
  90. if (m_hDC && iCnt)
  91. if (mBitmap.LoadBitmap( nIDResource ))
  92. {
  93. miCnt = iCnt;
  94. bReturn = InstallPicture();
  95. }
  96. else
  97. {
  98. #ifdef _DEBUG
  99. OutputDebugString( TEXT("Unable to load the bitmap!\r\n") );
  100. #endif
  101. }
  102. return bReturn;
  103. }
  104. /****************************************************************************/
  105. BOOL CPic::InstallPicture()
  106. {
  107. /*
  108. ** get the bitmap info from the picture bitmap, saving the picture size
  109. */
  110. BITMAP bmInfo;
  111. if (mBitmap.GetObject( sizeof( BITMAP ), &bmInfo ) != sizeof( BITMAP ))
  112. {
  113. #ifdef _DEBUG
  114. OutputDebugString( TEXT("GDI error getting bitmap information!\r\n") );
  115. #endif
  116. return FALSE;
  117. }
  118. mSize = CSize( bmInfo.bmWidth / miCnt, bmInfo.bmHeight );
  119. /*
  120. ** put the bitmap in the DC, saving the original.
  121. */
  122. CBitmap* bitmap = SelectObject( &mBitmap );
  123. mhBitmapOld = (HBITMAP)bitmap->m_hObject;
  124. /*
  125. ** create the mask bitmap, same size monochrome
  126. */
  127. if (! mMask.CreateBitmap( bmInfo.bmWidth, bmInfo.bmHeight, 1, 1, NULL ))
  128. {
  129. #ifdef _DEBUG
  130. OutputDebugString( TEXT("GDI error creating the mask bitmap!\r\n") );
  131. #endif
  132. return FALSE;
  133. }
  134. /*
  135. ** put the mask in a temp DC so we can generate the mask bits
  136. */
  137. CDC dc;
  138. dc.CreateCompatibleDC( this );
  139. ASSERT( dc.m_hDC );
  140. CBitmap* ob = dc.SelectObject( &mMask );
  141. /*
  142. ** use the color at the upper left corner for generating the mask
  143. */
  144. SetBkColor( GetPixel( 1, 1 ) );
  145. // this ROP Code will leave bits in the destination bitmap the same color if the
  146. // corresponding source bitmap's bit are black.
  147. // all other bits in the destination (where source bits are not black)
  148. // are turned to black.
  149. #define ROP_DSna 0x00220326L
  150. /*
  151. ** Creates the mask from all pixels in the image of a given color.
  152. ** Copies to the mask, then cuts the image with the mask.
  153. */
  154. // create the mast, All but the background color is Black
  155. // bkcolor is white
  156. dc.BitBlt( 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, this, 0, 0, SRCCOPY );
  157. // select FG color to be Black and BK color to be White
  158. // The Default Mono->Color Conversion sets (Black -> FG Color, White -> BG Color)
  159. // It uses FG/BK color from the destination (color DC).
  160. // we want Black -> black, White -> white
  161. // a black/white bitmap in color format.
  162. COLORREF cRefFGColorOld = dc.SetTextColor( RGB(0,0,0) );
  163. COLORREF cRefBKColorOld = dc.SetBkColor(RGB(255,255,255));
  164. BitBlt( 0, 0, bmInfo.bmWidth, bmInfo.bmHeight, &dc, 0, 0, ROP_DSna );
  165. dc.SetTextColor(cRefFGColorOld);
  166. dc.SetBkColor(cRefBKColorOld);
  167. dc.SelectObject( ob );
  168. mbReady = TRUE;
  169. return TRUE;
  170. }
  171. /****************************************************************************/