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

Article translations Article translations
Article ID: 318876 - View products that this article applies to.
This article was previously published under Q318876
Expand all | Collapse all

SUMMARY

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.

MORE INFORMATION

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;
}
				

Properties

Article ID: 318876 - Last Review: February 12, 2007 - Revision: 1.5
APPLIES TO
  • Microsoft Platform Software Development Kit-January 2000 Edition, when used with:
    • the operating system: Microsoft Windows XP
  • Microsoft Windows XP Professional
  • the operating system: Microsoft Windows XP 64-Bit Edition
Keywords: 
kbhowto kbgdi kbcursor kbdswgdi2003swept KB318876

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com