Lexikos: Used for menu icons on Windows Vista and later. Some similarities to IconToBitmap, maybe should be merged if IconToBitmap does not specifically need to create a device-dependent bitmap?
| 2720 | |
| 2721 | // Lexikos: Used for menu icons on Windows Vista and later. Some similarities to IconToBitmap, maybe should be merged if IconToBitmap does not specifically need to create a device-dependent bitmap? |
| 2722 | HBITMAP IconToBitmap32(HICON ahIcon, bool aDestroyIcon) |
| 2723 | { |
| 2724 | // Get the icon's internal colour and mask bitmaps. |
| 2725 | // hbmColor is needed to measure the icon. |
| 2726 | // hbmMask is needed to generate an alpha channel if the icon does not have one. |
| 2727 | ICONINFO icon_info; |
| 2728 | if (!GetIconInfo(ahIcon, &icon_info)) |
| 2729 | return NULL; |
| 2730 | |
| 2731 | HBITMAP hbitmap = NULL; // Set default in case of failure. |
| 2732 | |
| 2733 | // Get size of icon's internal bitmap. |
| 2734 | BITMAP icon_bitmap; |
| 2735 | if (GetObject(icon_info.hbmColor, sizeof(BITMAP), &icon_bitmap)) |
| 2736 | { |
| 2737 | int width = icon_bitmap.bmWidth; |
| 2738 | int height = icon_bitmap.bmHeight; |
| 2739 | |
| 2740 | HDC hdc = CreateCompatibleDC(NULL); |
| 2741 | if (hdc) |
| 2742 | { |
| 2743 | BITMAPINFO bitmap_info = {0}; |
| 2744 | // Set parameters for 32-bit bitmap. May also be used to retrieve bitmap data from the icon's mask bitmap. |
| 2745 | BITMAPINFOHEADER &bitmap_header = bitmap_info.bmiHeader; |
| 2746 | bitmap_header.biSize = sizeof(BITMAPINFOHEADER); |
| 2747 | bitmap_header.biWidth = width; |
| 2748 | bitmap_header.biHeight = height; |
| 2749 | bitmap_header.biBitCount = 32; |
| 2750 | bitmap_header.biPlanes = 1; |
| 2751 | |
| 2752 | // Create device-independent bitmap. |
| 2753 | UINT *bits; |
| 2754 | if (hbitmap = CreateDIBSection(hdc, &bitmap_info, 0, (void**)&bits, NULL, 0)) |
| 2755 | { |
| 2756 | HGDIOBJ old_object = SelectObject(hdc, hbitmap); |
| 2757 | if (old_object) // Above succeeded. |
| 2758 | { |
| 2759 | // Draw icon onto bitmap. Use DrawIconEx because DrawIcon always draws a large (usually 32x32) icon! |
| 2760 | DrawIconEx(hdc, 0, 0, ahIcon, 0, 0, 0, NULL, DI_NORMAL); |
| 2761 | |
| 2762 | // May be necessary for bits to be in sync, according to documentation for CreateDIBSection: |
| 2763 | GdiFlush(); |
| 2764 | |
| 2765 | // Calculate end of bitmap data. |
| 2766 | UINT *bits_end = bits + width*height; |
| 2767 | |
| 2768 | UINT *this_pixel; // Used in a few loops below. |
| 2769 | |
| 2770 | // Check for alpha data. |
| 2771 | bool has_nonzero_alpha = false; |
| 2772 | for (this_pixel = bits; this_pixel < bits_end; ++this_pixel) |
| 2773 | { |
| 2774 | if (*this_pixel >> 24) |
| 2775 | { |
| 2776 | has_nonzero_alpha = true; |
| 2777 | break; |
| 2778 | } |
| 2779 | } |
no outgoing calls
no test coverage detected