|

楼主 |
发表于 2014-7-31 08:22:15
|
显示全部楼层
最终的方案:- // 参考代码:http://support.microsoft.com/kb/318876/zh-cn
- HCURSOR CreateAlphaCursor(UINT* pBmp, int width, int height)
- {
- HBITMAP hBitmap = NULL;
- // 创建hBitmap
- {
- HDC hDC = GetDC(NULL);
- hBitmap = CreateCompatibleBitmap(hDC, width, height);
- ReleaseDC(NULL, hDC);
- // 设置像素
- {
- BITMAPINFO bitmapInfo = { 0 };
- bitmapInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bitmapInfo.bmiHeader.biWidth = width;
- bitmapInfo.bmiHeader.biHeight = height;
- bitmapInfo.bmiHeader.biPlanes = 1;
- bitmapInfo.bmiHeader.biCompression = BI_RGB;
- bitmapInfo.bmiHeader.biBitCount = 32;
- ::SetDIBits(NULL, hBitmap, 0, height, pBmp, &bitmapInfo, DIB_RGB_COLORS);
- }
- }
- // Create an empty mask bitmap.
- HBITMAP hMonoBitmap = CreateBitmap(width, height, 1, 1, NULL);
- 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.
- HCURSOR hAlphaCursor = CreateIconIndirect(&ii);
- DeleteObject(hBitmap);
- DeleteObject(hMonoBitmap);
- return hAlphaCursor;
- }
复制代码 唯一遗憾的是,用这种方法创建出来的光标只是静态的,如果要做动画光标,恐怕得用个独立的线程,不停的SetCursor()。
如果有人知道更好的方法,希望能告知一下。 |
|