Leaked source code of windows server 2003
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.

110 lines
2.9 KiB

  1. /*
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name: IconUtil.cpp
  4. Icon related utility functions, starting with an alpha stripped rendering
  5. */
  6. #include "stock.h"
  7. STDAPI_(HBITMAP) CreateDIB(HDC h, WORD depth, int cx, int cy, RGBQUAD** pprgb)
  8. {
  9. BITMAPINFO bi = {0};
  10. bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
  11. bi.bmiHeader.biWidth = cx;
  12. bi.bmiHeader.biHeight = cy;
  13. bi.bmiHeader.biPlanes = 1;
  14. bi.bmiHeader.biBitCount = depth;
  15. bi.bmiHeader.biCompression = BI_RGB;
  16. return CreateDIBSection(h, &bi, DIB_RGB_COLORS, (void**)pprgb, NULL, 0);
  17. }
  18. STDAPI_(BOOL) HasAlpha(RGBQUAD* prgb, int cx, int cy)
  19. {
  20. int iTotal = cx * cy;
  21. for (int i = 0; i < iTotal; i++)
  22. {
  23. if (prgb[i].rgbReserved != 0)
  24. return TRUE;
  25. }
  26. return FALSE;
  27. }
  28. STDAPI_(void) ProcessDIB(RGBQUAD* prgb, int cx, int cy)
  29. {
  30. int iTotal = cx * cy;
  31. for (int i = 0; i < iTotal; i++)
  32. {
  33. // Maybe Adjust the mask to mask out Alpha that's less than 128 and keep colors
  34. // where alpha is mostly opaque, instead of all of it.
  35. prgb[i].rgbReserved = 0;
  36. }
  37. }
  38. STDAPI_(void) AlphaStripRenderIcon(HDC hdc, int x, int y, HICON hicon, HDC hdcCompatible)
  39. {
  40. HICON hiconRender = hicon;
  41. BOOL fAlpha = FALSE;
  42. ICONINFO ii;
  43. if (GetIconInfo(hicon, &ii))
  44. {
  45. BITMAP bm;
  46. GetObject(ii.hbmColor, sizeof(bm), &bm);
  47. HDC hdcNew = CreateCompatibleDC(hdcCompatible); // hdc for a standard dc
  48. HDC hdcSrc = CreateCompatibleDC(hdcCompatible);
  49. if (hdcNew && hdcSrc)
  50. {
  51. RGBQUAD* prgb;
  52. HBITMAP hbmpNew = CreateDIB(hdc, 32, bm.bmWidth, bm.bmHeight, &prgb);
  53. if (hbmpNew)
  54. {
  55. HBITMAP hbmpDelete = hbmpNew;
  56. HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcNew, hbmpNew);
  57. HBITMAP hbmpOld2 = (HBITMAP)SelectObject(hdcSrc, ii.hbmColor);
  58. BitBlt(hdcNew, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);
  59. if (HasAlpha(prgb, bm.bmWidth, bm.bmHeight))
  60. {
  61. fAlpha = TRUE;
  62. ProcessDIB(prgb, bm.bmWidth, bm.bmHeight);
  63. hbmpDelete = ii.hbmColor;
  64. ii.hbmColor = hbmpNew;
  65. }
  66. SelectObject(hdcSrc, hbmpOld2);
  67. SelectObject(hdcNew, hbmpOld);
  68. DeleteObject(hbmpDelete);
  69. }
  70. }
  71. if (hdcNew)
  72. DeleteDC(hdcNew);
  73. if (hdcSrc)
  74. DeleteDC(hdcSrc);
  75. hiconRender = CreateIconIndirect(&ii);
  76. DeleteObject(ii.hbmColor);
  77. DeleteObject(ii.hbmMask);
  78. }
  79. DrawIcon(hdc, x, y, hiconRender);
  80. if (hiconRender != hicon)
  81. {
  82. DestroyIcon(hiconRender);
  83. }
  84. }