Drawing text on glass background becomes blurred as Alpha is lowered Drawing text on glass background becomes blurred as Alpha is lowered google-chrome google-chrome

Drawing text on glass background becomes blurred as Alpha is lowered


As Ian Boyd suggested in his nice post about How to draw ClearType text on Aero glass ? you should apply antialising when you're rendering the text on a sheet of glass. So to fix your problem, try to modify your code this way:

if rectF.Width > 10 thenbegin  if (GetParentForm.GlassFrame.Enabled) and (GetParentForm.GlassFrame.SheetOfGlass) then    graphics.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);  graphics.DrawString(Item.DisplayCaption, -1, font, rectF, stringFormat, solidBrush);end;

To simulate your problem it's enough to just render the text on the sheet of Aero glass, like the following code does in a 3 different ways:

uses  GDIPAPI, GDIPOBJ;procedure TForm1.FormCreate(Sender: TObject);begin  Font.Color := clWhite;  GlassFrame.SheetOfGlass := True;  GlassFrame.Enabled := True;end;procedure TForm1.FormPaint(Sender: TObject);var  S: WideString;  GPFont: TGPFont;  GPGraphics: TGPGraphics;  GPSolidBrush: TGPSolidBrush;  GPGraphicsPath: TGPGraphicsPath;begin  S := 'This is a sample text rendered on the sheet of Aero glass!';  GPFont := TGPFont.Create(Canvas.Handle, Font.Handle);  GPSolidBrush := TGPSolidBrush.Create(MakeColor(GetRValue(Font.Color),    GetGValue(Font.Color), GetBValue(Font.Color)));  GPGraphicsPath := TGPGraphicsPath.Create;  GPGraphicsPath.AddString(S, Length(S), TGPFontFamily.Create(Font.Name),    GPFont.GetStyle, GPFont.GetSize, MakePoint(20.0, 60.0), nil);  try    GPGraphics := TGPGraphics.Create(Canvas.Handle);    try      GPGraphics.SetSmoothingMode(SmoothingModeAntiAlias);      GPGraphics.FillPath(GPSolidBrush, GPGraphicsPath);      GPGraphics.DrawString(S, Length(S), GPFont, MakePoint(20.0, 20.0),        nil, GPSolidBrush);      GPGraphics.SetTextRenderingHint(        TextRenderingHintSingleBitPerPixelGridFit);      GPGraphics.DrawString(S, Length(S), GPFont, MakePoint(20.0, 40.0),        nil, GPSolidBrush);    finally      GPGraphics.Free;    end;  finally    GPFont.Free;    GPSolidBrush.Free;    GPGraphicsPath.Free;  end;end;

And it results to the following image where:

  1. the first text was rendered by the DrawString function without text antialiasing enabled
  2. the second one was rendered by the DrawString function with text antialiasing enabled, configured to the TextRenderingHintSingleBitPerPixelGridFit mode
  3. the third one was rendered by the path filling, inspired by this article with the smoothing mode set to the SmoothingModeAntiAlias style

enter image description here


As an alternative, you can try drawing with the theme api (Vista and later). Playing with various shadow/border/glow settings, it may be possible to come up with readable text. Some tryout on sheet of glass:


  enter image description here

code (XE2):

procedure TForm1.FormPaint(Sender: TObject);var  R: TRect;  ThemeData: HTHEME;  Opts: TDTTOpts;begin  R := Rect(10, 10, 150, 30);  vcl.themes.DrawGlassText(Canvas.Handle, 'DrawGlassText Sample', R, 0, 3,      clBlack, TStyleManager.SystemStyle.GetElementDetails(ttsLabel));  OffsetRect(R, 160, 0);  ThemeData := OpenThemeData(Handle, 'textstyle');  Opts.dwSize := SizeOf(Opts);  Opts.crText := ColorToRGB(clBlack);  Opts.crShadow := $D0D0B0;  Opts.iTextShadowType := TST_SINGLE;  Opts.ptShadowOffset := Point(1, 1);  Opts.fApplyOverlay := True;  Opts.iGlowSize := 3;  Opts.dwFlags := DTT_TEXTCOLOR or DTT_SHADOWTYPE or DTT_SHADOWCOLOR      or DTT_SHADOWOFFSET or DTT_GLOWSIZE;  DrawThemeTextEx(ThemeData, Canvas.Handle, TEXT_LABEL, TS_NORMAL,      'DrawThemeTextEx Sample', -1, 0, @R, Opts);  OffsetRect(R, 180, 0);  Opts.crText := ColorToRGB(clBlack);  Opts.iGlowSize := 4;  Opts.fApplyOverlay := True;  Opts.dwFlags := DTT_TEXTCOLOR or DTT_GLOWSIZE;  DrawThemeTextEx(ThemeData, Canvas.Handle, TEXT_BODYTITLE, 0,      'Another Sample', -1, 0, @R, Opts);  CloseThemeData(ThemeData);end;