在Delphi中创建带抗锯齿文本的悬浮顶层窗口问题求助
问题:创建带抗锯齿文本的透明悬浮窗口时出现不良伪影
想要创建一个仅显示抗锯齿文本(无其他窗口元素)的顶层悬浮窗口,尝试用GDI+绘制文本以保留文本渲染位图的alpha通道,但效果不佳。已成功创建透明位图,但文本绘制函数似乎将背景识别为黑色而非透明,导致出现不良的抗锯齿伪影。附上完整代码,请问遗漏了什么?
uses GDIPAPI ,GDIPOBJ; var GDIPToken: ULONG; GDIStartupInput: TGDIPlusStartupInput; procedure TOSDInfoForm.FormCreate(Sender: TObject); var bmp: TBitmap; blend: TBLENDFUNCTION; pointSrc, pointForm: TPoint; bmpsize: TSize; g: TGPGraphics; fontFamily: TGPFontFamily; font: TGPFont; brush: TGPSolidBrush; status: TStatus; begin BorderStyle := bsNone; FormStyle := fsStayOnTop; SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED); GDIStartupInput.SuppressBackgroundThread := False; GDIStartupInput.SuppressExternalCodecs := False; GDIStartupInput.GdiplusVersion := 1; GdiplusStartup(GDIPToken, @GDIStartupInput, nil); bmp := TBitmap.Create; try bmp.PixelFormat := pf32bit; bmp.Width := Width; bmp.Height := Height; g := TGPGraphics.Create(bmp.Canvas.Handle); try //g.SetTextRenderingHint(TextRenderingHintClearTypeGridFit); g.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit); g.Clear(MakeColor(0, 0, 0, 0)); // Clear with full transparent fontFamily := TGPFontFamily.Create('Arial'); font := TGPFont.Create(fontFamily, 32, FontStyleRegular, UnitPixel); brush := TGPSolidBrush.Create(MakeColor(255, 255, 0, 0)); // Red text g.DrawString('Hello, World!', -1, font, MakePoint(10.0, 10.0), brush); brush.Free; font.Free; fontFamily.Free; finally g.Free; end; pointSrc := Point(0, 0); pointForm := Point(Left, Top); bmpsize.cx := bmp.Width; bmpsize.cy := bmp.Height; blend.BlendOp := AC_SRC_OVER; blend.BlendFlags := 0; blend.SourceConstantAlpha := 255; // Opaque blend.AlphaFormat := AC_SRC_ALPHA; UpdateLayeredWindow(Handle, 0, @pointForm, @bmpsize, bmp.Canvas.Handle, @pointSrc, 0, @blend, ULW_ALPHA); finally bmp.Free; end; end; procedure TOSDInfoForm.FormDestroy(Sender: TObject); begin GdiplusShutdown(GDIPToken); end;
编辑说明:
感谢Tom的提示,上述代码已可正常使用,可自行取用。请注意该代码使用GDI+,至少需要Windows 7系统才能无需手动安装GDI+即可运行。
内容的提问来源于stack exchange,提问作者bLight
相关产品推荐
相关产品推荐

