BackgroundUtil.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "stdafx.h"
  2. #include "BackgroundUtil.h"
  3. #ifdef _DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[]=__FILE__;
  6. #define new DEBUG_NEW
  7. #endif
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CBackgroundUtil::CBackgroundUtil()
  12. {
  13. }
  14. CBackgroundUtil::~CBackgroundUtil()
  15. {
  16. m_BmpPattern.Detach();
  17. m_BmpPalette.Detach();
  18. }
  19. BOOL CBackgroundUtil::SetBitmap(UINT uResourceID)
  20. {
  21. BITMAP bm;
  22. BOOL bRet;
  23. // Detach previous resources
  24. m_BmpPattern.Detach();
  25. m_BmpPalette.Detach();
  26. // Default return value
  27. bRet = TRUE;
  28. // Load new bitmap
  29. if (uResourceID != 0)
  30. {
  31. bRet = GetBitmapAndPalette(uResourceID, m_BmpPattern, m_BmpPalette);
  32. // If all ok
  33. if (bRet == TRUE)
  34. {
  35. // Get dimension
  36. m_BmpPattern.GetBitmap(&bm);
  37. // Width of the bitmap
  38. m_nBmpWidth = bm.bmWidth;
  39. // Height of the bitmap
  40. m_nBmpHeight = bm.bmHeight;
  41. }
  42. }
  43. return bRet;
  44. } // End of SetBitmap
  45. BOOL CBackgroundUtil::GetBitmapAndPalette(UINT nIDResource, CBitmap & bitmap, CPalette & pal)
  46. {
  47. LPCTSTR lpszResourceName = (LPCTSTR)nIDResource;
  48. HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(),
  49. lpszResourceName, IMAGE_BITMAP, 0,0, LR_CREATEDIBSECTION);
  50. if (hBmp == NULL) return FALSE;
  51. bitmap.Attach(hBmp);
  52. // Create a logical palette for the bitmap
  53. DIBSECTION ds;
  54. BITMAPINFOHEADER &bmInfo = ds.dsBmih;
  55. bitmap.GetObject(sizeof(ds), &ds);
  56. int nColors = bmInfo.biClrUsed ? bmInfo.biClrUsed : 1 << bmInfo.biBitCount;
  57. // Create a halftone palette if colors > 256.
  58. CClientDC dc(NULL); // Desktop DC
  59. if(nColors > 256)
  60. pal.CreateHalftonePalette(&dc);
  61. else
  62. {
  63. // Create the palette
  64. RGBQUAD *pRGB = new RGBQUAD[nColors];
  65. CDC memDC;
  66. memDC.CreateCompatibleDC(&dc);
  67. memDC.SelectObject( &bitmap );
  68. ::GetDIBColorTable( memDC, 0, nColors, pRGB );
  69. UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
  70. LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
  71. pLP->palVersion = 0x300;
  72. pLP->palNumEntries = nColors;
  73. for (int i=0; i < nColors; i++)
  74. {
  75. pLP->palPalEntry[i].peRed = pRGB[i].rgbRed;
  76. pLP->palPalEntry[i].peGreen = pRGB[i].rgbGreen;
  77. pLP->palPalEntry[i].peBlue = pRGB[i].rgbBlue;
  78. pLP->palPalEntry[i].peFlags = 0;
  79. }
  80. pal.CreatePalette( pLP );
  81. delete[] pLP;
  82. delete[] pRGB;
  83. }
  84. return TRUE;
  85. } // End of GetBitmapAndPalette
  86. BOOL CBackgroundUtil::TileBitmap(CDC* pDC, CRect rc)
  87. {
  88. CDC MemDC;
  89. int x = 0, y = 0;
  90. // If there is a bitmap loaded
  91. if (m_BmpPattern.m_hObject != NULL)
  92. {
  93. MemDC.CreateCompatibleDC(pDC);
  94. CBitmap* pOldBitmap = MemDC.SelectObject(&m_BmpPattern);
  95. // Tile the bitmap
  96. while (y < rc.Height())
  97. {
  98. while(x < rc.Width())
  99. {
  100. pDC->BitBlt(x, y, m_nBmpWidth, m_nBmpHeight, &MemDC, 0, 0, SRCCOPY);
  101. x += m_nBmpWidth;
  102. }
  103. x = 0;
  104. y += m_nBmpHeight;
  105. }
  106. MemDC.SelectObject(pOldBitmap);
  107. return TRUE;
  108. }
  109. return FALSE; // Normal behaviour
  110. } // End of TileBitmap