How To Create an Alpha Blended Cursor or Icon in Windows XP

Support for Windows XP has ended

Microsoft ended support for Windows XP on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q318876
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes the steps that are necessary to programmatically create a cursor or icon that contains alpha bits. For the purposes of this article, these types of cursors or icons are referred to as "alpha blended cursors" or "alpha blended icons." Alpha blended cursors and alpha blended icons are only supported on Microsoft Windows XP.
To create an alpha blended cursor or icon, create a DIB section that contains alpha values, and call the CreateIconIndirect function with that DIB section. Although the Mask bitmap in the ICONINFO structure is not necessary, you must create an empty monochrome bitmap to send to the CreateIconIndirect function as the Mask bitmap. The alpha bits for each pixel in the DIB section bitmap determines the visibility for each pixel in the cursor or icon that is created.

Follow these steps to create an alpha blended cursor or icon:
  1. Complete a BITMAPV5HEADER structure, as in the code example following these steps, to define a 32 bits per pixel (BPP) alpha blended DIB.
  2. Call the CreateDIBSection function to create a DIB section based on the BITMAPV5HEADER structure that you completed.
  3. Use the bitmap and alpha information that you want for your alpha blended cursor or icon to complete the DIB section.
  4. Complete an ICONINFO structure.
  5. Place an empty monochrome bitmap in the hbmMask field, and then place the alpha blended DIB section in the hbmColor field.
  6. Call the CreateIconIndirect function to create the alpha blended cursor or icon.
The following Microsoft Visual C++ code demonstrates how to create an alpha blended cursor. You can use the same code to create an alpha blended icon by changing the fIcon member of the ICONINFO structure to TRUE:

HCURSOR CreateAlphaCursor(void){    HDC hMemDC;    DWORD dwWidth, dwHeight;    BITMAPV5HEADER bi;    HBITMAP hBitmap, hOldBitmap;    void *lpBits;    DWORD x,y;    HCURSOR hAlphaCursor = NULL;    dwWidth  = 32;  // width of cursor    dwHeight = 32;  // height of cursor    ZeroMemory(&bi,sizeof(BITMAPV5HEADER));    bi.bV5Size           = sizeof(BITMAPV5HEADER);    bi.bV5Width           = dwWidth;    bi.bV5Height          = dwHeight;    bi.bV5Planes = 1;    bi.bV5BitCount = 32;    bi.bV5Compression = BI_BITFIELDS;    // The following mask specification specifies a supported 32 BPP    // alpha format for Windows XP.    bi.bV5RedMask   =  0x00FF0000;    bi.bV5GreenMask =  0x0000FF00;    bi.bV5BlueMask  =  0x000000FF;    bi.bV5AlphaMask =  0xFF000000;     HDC hdc;    hdc = GetDC(NULL);    // Create the DIB section with an alpha channel.    hBitmap = CreateDIBSection(hdc, (BITMAPINFO *)&bi, DIB_RGB_COLORS,         (void **)&lpBits, NULL, (DWORD)0);    hMemDC = CreateCompatibleDC(hdc);    ReleaseDC(NULL,hdc);    // Draw something on the DIB section.    hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);    PatBlt(hMemDC,0,0,dwWidth,dwHeight,WHITENESS);    SetTextColor(hMemDC,RGB(0,0,0));    SetBkMode(hMemDC,TRANSPARENT);    TextOut(hMemDC,0,9,"rgba",4);    SelectObject(hMemDC, hOldBitmap);    DeleteDC(hMemDC);    // Create an empty mask bitmap.    HBITMAP hMonoBitmap = CreateBitmap(dwWidth,dwHeight,1,1,NULL);    // Set the alpha values for each pixel in the cursor so that    // the complete cursor is semi-transparent.    DWORD *lpdwPixel;    lpdwPixel = (DWORD *)lpBits;    for (x=0;x<dwWidth;x++)       for (y=0;y<dwHeight;y++)       {           // Clear the alpha bits           *lpdwPixel &= 0x00FFFFFF;           // Set the alpha bits to 0x9F (semi-transparent)           *lpdwPixel |= 0x9F000000;           lpdwPixel++;       }    ICONINFO ii;    ii.fIcon = FALSE;  // Change fIcon to TRUE to create an alpha icon    ii.xHotspot = 0;    ii.yHotspot = 0;    ii.hbmMask = hMonoBitmap;    ii.hbmColor = hBitmap;    // Create the alpha cursor with the alpha DIB section.    hAlphaCursor = CreateIconIndirect(&ii);    DeleteObject(hBitmap);              DeleteObject(hMonoBitmap);     return hAlphaCursor;}				

Article ID: 318876 - Last Review: 12/07/2015 09:09:00 - Revision: 1.5

Microsoft Platform Software Development Kit-January 2000 Edition, Microsoft Windows XP Professional, the operating system: Microsoft Windows XP 64-Bit Edition

  • kbnosurvey kbarchive kbhowto kbgdi kbcursor kbdswgdi2003swept KB318876