3

I'm using a custom gauge, based on the example that came with Delphi (5 Enterprise). For those that don't know, it's like a smooth progress bar, but displays the percentage or value in the centre (vertically and horizontally) of the component.

To make sure the text is readable both when the gauge is filled and when it's empty, the text is displayed using inverted colours.

When font anti-aliasing is used, these inverted colours cause the edge of the font to appear in really crazy colours, ruining the look of the component.

Is there any way to disable font smoothing / anti aliasing for just this one component, or disable it, draw the text, then re-enable it?

My current workaround is to use a font that doesn't get smoothed, like "MS Sans Serif", but I'd like to use the same font as the rest of the UI for consistency.

Drarok
  • 3,612
  • 2
  • 31
  • 48
  • 1
    Not using subpixel anti-aliasing ruins the component. Redesign! – Andreas Rejbrand Sep 15 '10 at 10:16
  • 1
    Merely inverting the colors don't mean the text will be readable. The inverse of gray is still gray. You have four colors, right? The user can choose the control's background color and progress-bar color, but I'll be you only offer one color setting for text when there clearly need to be two. You need to offer settings for text that appears over the colored progress bar as well as for the text that appears over the background. Let the user choose good contrasting colors. (There are ways of automatically choosing contrasting colors, too.) – Rob Kennedy Sep 15 '10 at 12:50
  • There are no "users" of this component except for myself and one other developer in one solitary application. The gauges are filled with black at present, so there's no need to hugely over-complicate the thing with calculating where the overlap is. There's no need for condescending comments, guys. – Drarok Sep 15 '10 at 16:41

1 Answers1

13

Specifying NONANTIALIASED_QUALITY in the LOGFONT structure should turn antialiasing off:

procedure SetFontQuality(Font: TFont; Quality: Byte);
var
  LogFont: TLogFont;
begin
  if GetObject(Font.Handle, SizeOf(TLogFont), @LogFont) = 0 then
    RaiseLastOSError;
  LogFont.lfQuality := Quality;
  Font.Handle := CreateFontIndirect(LogFont);
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
const
  FontQualities: array[Boolean] of Byte = (DEFAULT_QUALITY, NONANTIALIASED_QUALITY);
var
  Canvas: TCanvas;
begin
  Canvas := (Sender as TPaintBox).Canvas;
  SetFontQuality(Canvas.Font, FontQualities[CheckBox1.Checked]);
  Canvas.TextOut(12, 12, 'Hello, world!');
end;
Ondrej Kelle
  • 36,941
  • 2
  • 65
  • 128
  • Cool! The difference is more evident with larger fonts (and a CheckBox1 OnClik event). – Sertac Akyuz Sep 15 '10 at 11:41
  • 4
    +1. This is indeed the way to disable font smoothing, which is what the question asked for. Too bad font smoothing isn't really Drarok's problem. – Rob Kennedy Sep 15 '10 at 12:52