
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.
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: 02/12/2007 20:40:00 - Revision: 1.5