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.

114 lines
3.1 KiB

  1. //-------------------------------------------------------------------------
  2. // TmUtils.cpp - theme manager shared utilities
  3. //-------------------------------------------------------------------------
  4. #include "stdafx.h"
  5. #include "TmUtils.h"
  6. //-------------------------------------------------------------------------
  7. CBitmapPixels::CBitmapPixels()
  8. {
  9. _hdrBitmap = NULL;
  10. _iWidth = 0;
  11. _iHeight = 0;
  12. }
  13. //-------------------------------------------------------------------------
  14. CBitmapPixels::~CBitmapPixels()
  15. {
  16. if (_hdrBitmap)
  17. delete [] (BYTE *)_hdrBitmap;
  18. }
  19. //-------------------------------------------------------------------------
  20. HRESULT CBitmapPixels::OpenBitmap(HDC hdc, HBITMAP bitmap, BOOL fForceRGB32,
  21. DWORD OUT **pPixels, OPTIONAL OUT int *piWidth, OPTIONAL OUT int *piHeight,
  22. OPTIONAL OUT int *piBytesPerPixel, OPTIONAL OUT int *piBytesPerRow)
  23. {
  24. if (! pPixels)
  25. return E_INVALIDARG;
  26. BITMAP bminfo;
  27. GetObject(bitmap, sizeof(bminfo), &bminfo);
  28. _iWidth = bminfo.bmWidth;
  29. _iHeight = bminfo.bmHeight;
  30. int iBytesPerPixel = 3;
  31. #if 0
  32. if ((fForceRGB32) || (bminfo.bmBitsPixel == 32))
  33. iBytesPerPixel = 4;
  34. else
  35. iBytesPerPixel = 3;
  36. #endif
  37. int iRawBytes = _iWidth * iBytesPerPixel;
  38. int iBytesPerRow = 4*((iRawBytes+3)/4);
  39. int size = sizeof(BITMAPINFOHEADER) + _iHeight*iBytesPerRow;
  40. BYTE *dibBuff = new BYTE[size+100]; // avoid random GetDIBits() failures with 100 bytes padding (?)
  41. if (! dibBuff)
  42. return E_OUTOFMEMORY;
  43. _hdrBitmap = (BITMAPINFOHEADER *)dibBuff;
  44. memset(_hdrBitmap, 0, sizeof(BITMAPINFOHEADER));
  45. _hdrBitmap->biSize = sizeof(BITMAPINFOHEADER);
  46. _hdrBitmap->biWidth = _iWidth;
  47. _hdrBitmap->biHeight = _iHeight;
  48. _hdrBitmap->biPlanes = 1;
  49. _hdrBitmap->biBitCount = 8*iBytesPerPixel;
  50. _hdrBitmap->biCompression = BI_RGB;
  51. bool fNeedRelease = false;
  52. if (! hdc)
  53. {
  54. hdc = GetWindowDC(NULL);
  55. fNeedRelease = true;
  56. }
  57. int linecnt = GetDIBits(hdc, bitmap, 0, _iHeight, DIBDATA(_hdrBitmap), (BITMAPINFO *)_hdrBitmap,
  58. DIB_RGB_COLORS);
  59. if (fNeedRelease)
  60. ReleaseDC(NULL, hdc);
  61. *pPixels = (DWORD *)DIBDATA(_hdrBitmap);
  62. if (piWidth)
  63. *piWidth = _iWidth;
  64. if (piHeight)
  65. *piHeight = _iHeight;
  66. if (piBytesPerPixel)
  67. *piBytesPerPixel = iBytesPerPixel;
  68. if (piBytesPerRow)
  69. *piBytesPerRow = iBytesPerRow;
  70. return S_OK;
  71. }
  72. //-------------------------------------------------------------------------
  73. void CBitmapPixels::CloseBitmap(HDC hdc, HBITMAP hBitmap)
  74. {
  75. if (_hdrBitmap)
  76. {
  77. if (hBitmap) // rewrite bitmap
  78. {
  79. bool fNeedRelease = false;
  80. if (! hdc)
  81. {
  82. hdc = GetWindowDC(NULL);
  83. fNeedRelease = true;
  84. }
  85. SetDIBits(hdc, hBitmap, 0, _iHeight, DIBDATA(_hdrBitmap), (BITMAPINFO *)_hdrBitmap,
  86. DIB_RGB_COLORS);
  87. if ((fNeedRelease) && (hdc))
  88. ReleaseDC(NULL, hdc);
  89. }
  90. delete [] (BYTE *)_hdrBitmap;
  91. _hdrBitmap = NULL;
  92. }
  93. }
  94. //-------------------------------------------------------------------------