Supporting Custom DPI (Large Fonts) in Delphi Applications

With advance of modern monitors, their screen pixel resolutions are growing larger and larger. Back in 2003, typical development requirement was to support 800×600 resolution, nowadays it is 1024×768.

Actually, according to my webserver logs, screen resolution of 1280×1024 is set on 50% of all monitors. This percentage could bigger, but users complain fonts are getting too small to be read, and decrease their screen resolutions to 1024×768. Meanwhile their monitors support larger resolutions…

Most advanced users increase DPI setting in Display properties, and start to experience problems, because most applications do not support non-default DPI settings.

For example, in my case, my typical screen configuration, which I recommend, looks as follows:

  1. Screen resolution is set to maximum (1280×1024, 1600×1200 or bigger…)
  2. DPI setting is set to 120 pixels per inch (Large Fonts)
  3. Font smoothing option is set to ClearType for TFT monitors, and Standard for CRT monitors.

90% of Delphi applications I have seen, do not support Large Fonts, or support it very ugly. This is due to the fact, that VCL implementation of custom DPI is very ugly. There is a Scaled property in TForm, which was about to handle form scaling in case of non-default DPI setting, but it is not working correctly.

So here is my minimal code to support custom DPI/Large Fonts in Delphi applications correctly:

procedure TgxCustomForm.FormCreate(Sender: TObject); begin   Assert(not Scaled, 'You should set Scaled property of Form to False!');     case Screen.PixelsPerInch of     96:     begin       // default resolution at which application is developed       ///ScaleBy(96, 96);     end;     120:     begin       ScaleBy(120, 96);     end;     else     begin       ScaleBy(Screen.PixelsPerInch, 96);     end;   end; end;

Comments for this post

New Code

New variant of above code:

procedure TgxCustomForm.FormCreate(Sender: TObject); begin     Assert(not Scaled, 'You should set Scaled property of Form to False!');     if Screen.PixelsPerInch <> PixelsPerInch then   begin         ScaleBy(Screen.PixelsPerInch, PixelsPerInch);     end; end;

Andriy, your last version

Andriy, your last version will obviously fail: Screen.PixelsPerInch and PixelsPerInch will have the same value! Like in the original post you will have to store the PPI used while developing the app and then compare that to the actual PPI when creating the window: procedure TgxCustomForm.FormCreate(Sender: TObject); begin Assert(not Scaled, 'You should set Scaled property of Form to False!'); if Screen.PixelsPerInch <> 96 then begin ScaleBy(Screen.PixelsPerInch, 96); end; end; where 96 should obviously be replaced by the development resolution

Display Statistics

OneStat.com (www.onestat.com) today reported that the screen resolution 800 x 600 pixels has signficantly decreased since July 2005. More and more internet users choose for screen resolution 1024 x 768 or higher.

http://www.onestat.com/html/aboutus_pressbox43-screen-resolutions.html

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>

More information about formatting options

»