根据 MS KB entry,CreateIconIndirect中存在一个怪癖,它识别HBITMAP是通过传递给CreateDIBSection(和BGRA通道布局)的BITMAPV5HEADER创建的. 但是,当其句柄引用时,具有(PixelFormat = pf32bit)和(AlphaFormat = afDefi
但是,当其句柄引用时,具有(PixelFormat = pf32bit)和(AlphaFormat = afDefined)(表现为其他目的的alpha混合)的TBitmap实例不会被识别为用于创建图标或光标的有效alpha混合位图.
目前,我必须使用描述的API调用(see)创建TBitmap的完整副本,以使CreateIconIndirect接受位图处理作为alpha混合.我该如何克服这种笨拙?
这是一个例子:procedure TForm1.Button1Click(Sender: TObject); const crAlpha = TCursor(-25); var Bmp: TBitmap; Px: PRGBQuad; X, Y: Integer; BmpMask: TBitmap; II: TIconInfo; AlphaCursor: HCURSOR; begin Bmp := TBitmap.Create; Bmp.PixelFormat := pf32bit; Bmp.Canvas.Brush.Color := clWhite; Bmp.SetSize(32, 32); Bmp.Canvas.Font.Style := [fsBold]; Bmp.Canvas.Font.Color := clRed; Bmp.Canvas.TextOut(1, 10, 'alpha'); for Y := 0 to (Bmp.Height - 1) do begin Px := Bmp.ScanLine[Y]; for X := 0 to (Bmp.Width - 1) do begin if DWORD(Px^) = DWORD(clWhite) then Px.rgbReserved := $00 else Px.rgbReserved := $FF; Inc(Px); end; end; BmpMask := TBitmap.Create; BmpMask.SetSize(Bmp.Width, Bmp.Height); II.fIcon := False; II.xHotspot := 32; II.yHotspot := 32; II.hbmMask := BmpMask.Handle; II.hbmColor := Bmp.Handle; AlphaCursor := CreateIconIndirect(II); Win32Check(AlphaCursor <> 0); BmpMask.Free; Bmp.AlphaFormat := afDefined; // AlphaBlend below, premultiply channels Canvas.Draw(0, 0, Bmp); // test draw Bmp.Free; Screen.Cursors[crAlpha] := AlphaCursor; Cursor := crAlpha; end;
(顶部’alpha’是测试绘图,另一个是光标)