Difference between revisions of "High DPI"

From Lazarus wiki
Jump to navigationJump to search
(→‎See also: New section)
(17 intermediate revisions by 5 users not shown)
Line 15: Line 15:
 
* Graphics.ScreenInfo.PixelsPerInchY
 
* Graphics.ScreenInfo.PixelsPerInchY
 
* Forms.Screen.PixelsPerInch
 
* Forms.Screen.PixelsPerInch
* Forms.TForm.DesignTimeDPI (See: [http://wiki.lazarus.freepascal.org/Autosize_/_Layout#DPI_auto-adjustment_and_absolute_layout_auto-adjustment DPI auto-adjustment and absolute layout auto-adjustment])
+
* Forms.TForm.DesignTimePPI (See: [http://wiki.lazarus.freepascal.org/Autosize_/_Layout#DPI_auto-adjustment_and_absolute_layout_auto-adjustment DPI auto-adjustment and absolute layout auto-adjustment])
  
  
Line 130: Line 130:
 
'''Windows DPI Scaling'''
 
'''Windows DPI Scaling'''
  
This is same app running at 144 DPI (150%) without a manifest, so Windows scales it like a bitmap. The result is a blurred image.
+
This is the same app running at 144 DPI (150%) without a manifest, so Windows scales it like a bitmap. The result is a blurred image.
  
 
[[Image:cpicksniff_blured.png]]
 
[[Image:cpicksniff_blured.png]]
Line 142: Line 142:
 
'''High DPI'''
 
'''High DPI'''
  
Finally with both a manifest and a coded scaling handler, the app is in High DPI.
+
Finally with both a manifest and a LCL scaling, the app is in High DPI.
  
 
[[Image:cpicksniff_highdpi.png]]
 
[[Image:cpicksniff_highdpi.png]]
  
==== STEP 1 - Declare High DPI Awareness  ====
+
== High DPI in Lazarus 1.8 and above ==
To do this we need a manifest file that includes the declaration, with Lazarus 0.9.30 we can do this by going to Options > Project Options > then selecting the options "Use Manifest to Enable Themes (Windows)" and "Dpi Aware application (for Vista +)".
 
  
In current lazarus versions there are more settings and the names of the settings has been changed since the first appear in version 0.9.30. Now you have more options. Select the appropriate for your needs.
+
To handle High DPI using new features in 1.8, follow these steps:
  
Next choose a method of scaling: Scaled property, AutoAdjustLayout or ScaleDPI, see step 2.
+
* On Windows: enable DPI awareness in Project Options -> Application. Decide if you want to support per monitor DPI awareness or not.
 +
* Enable LCL scaling for your application DPI awareness in Project Options -> Application -> "Use LCL scaling (Hi-DPI).
 +
* Set TForm.Scaled=True for all your forms (it is the default value). All WYSIWYG should work automatically. Also the designer scales the forms accordingly.
 +
* If you create controls run-time, scale all coordinates, sizes etc that have to be DPI-aware with TControl.Scale96ToForm() or ScaleDesignToForm() (depending on your choice of default PPI) or prepare your container (e.g. panel with controls) as it was with 96 PPI and then call TControl.AutoAdjustLayout(lapAutoAdjustForDPI, 96, ParentFormOfTheContainer.PixelsPerInch, 0, 0);
 +
* If some of your components don't scale their inner sizes, override DoAutoAdjustLayout and scale the sizes (see TToolBar) - it has to be done for all controls. If a LCL control misses DoAutoAdjustLayout please report to mantis and provide a patch if you can.
 +
* Hint: Do ''not'' develop under HighDPI Mode, always develop under normal mode and only test under HighDPI. Otherwise Lazarus IDE will set DesignTimePPI of the different forms to different values.
  
==== STEP 2 - Scale Forms and Controls (Scaled property method) ====
+
=== Lazarus IDE high DPI ===
  
With Lazarus 1.7 we can scale the controls by checking the option "Scaled" of the Form in the object inspector.
+
The Lazarus IDE is DPI-aware itself.
  
==== STEP 2 - Scale Forms and Controls (AutoAdjustLayout method) ====
+
== High DPI in older Lazarus ==
  
To do this we can call AutoAdjustLayout OnCreate event of each form:
+
Is not as nice as 1.8 but it works, call on each form OnCreate event:
  
<syntaxhighlight>procedure TForm1.FormCreate(Sender: TObject);
+
Self.AutoAdjustLayout(lapAutoAdjustForDPI, 96, Screen.PixelsPerInch, Self.Width, ScaleX(Self.Width, 96));
begin
 
  Self.AutoAdjustLayout(lapAutoAdjustForDPI, Self.DesignTimeDPI, Screen.PixelsPerInch, Self.Width, ScaleX(Self.Width, Self.DesignTimeDPI));
 
end;</syntaxhighlight>
 
  
Note the last parameter, you can scale the form according to DPI or maybe keep width and height, you choose if scale it or not.
+
== See also ==
 
 
Or we can use a procedure to scale all forms (maybe not recommended because sometimes want to update the width and height and sometimes not, you decide):
 
 
 
<syntaxhighlight>uses
 
  Forms, Controls, Graphics;
 
 
procedure HighDPI;
 
var
 
  i: integer;
 
begin
 
  for i := 0 to Screen.FormCount - 1 do
 
    Screen.Forms[i].AutoAdjustLayout(lapAutoAdjustForDPI, Screen.Forms[i].DesignTimeDPI, Screen.PixelsPerInch, Screen.Forms[i].Width, ScaleX(Screen.Forms[i].Width, Screen.Forms[i].DesignTimeDPI));
 
end;</syntaxhighlight>
 
 
 
Please note the uses clause since we need these units (for example if you add HighDPI to the lpr file directly instead of creating a new unit).
 
 
 
In your LPR:
 
 
 
<syntaxhighlight>begin
 
  RequireDerivedFormResource:=True;
 
  Application.Initialize;
 
  Application.CreateForm(TForm1, Form1);
 
  HighDPI; // Call it here below all forms creation
 
  Application.Run;
 
end.</syntaxhighlight>
 
 
 
==== STEP 2 - Scale Forms and Controls (ScaleDPI method) ====
 
 
 
This is a custom method, the first that was added to this wiki article.
 
 
 
To do this we can call ScaleDPI procedure in the OnCreate event of each form in your project.
 
 
 
First copy the code below and save it to a text file "uscaledpi.pas":
 
 
 
<syntaxhighlight>unit uscaledpi;
 
 
 
{$mode objfpc}{$H+}
 
 
 
interface
 
 
 
uses
 
  Forms, Graphics, Controls;
 
 
 
procedure HighDPI(FromDPI: integer);
 
procedure ScaleDPI(Control: TControl; FromDPI: integer);
 
 
 
implementation
 
 
 
procedure HighDPI(FromDPI: integer);
 
var
 
  i: integer;
 
begin
 
  if Screen.PixelsPerInch = FromDPI then
 
    exit;
 
 
 
  for i := 0 to Screen.FormCount - 1 do
 
    ScaleDPI(Screen.Forms[i], FromDPI);
 
end;
 
 
 
procedure ScaleDPI(Control: TControl; FromDPI: integer);
 
var
 
  i: integer;
 
  WinControl: TWinControl;
 
begin
 
  if Screen.PixelsPerInch = FromDPI then
 
    exit;
 
 
 
  with Control do
 
  begin
 
    Left := ScaleX(Left, FromDPI);
 
    Top := ScaleY(Top, FromDPI);
 
    Width := ScaleX(Width, FromDPI);
 
    Height := ScaleY(Height, FromDPI);
 
  end;
 
 
 
  if Control is TWinControl then
 
  begin
 
    WinControl := TWinControl(Control);
 
    if WinControl.ControlCount = 0 then
 
      exit;
 
    for i := 0 to WinControl.ControlCount - 1 do
 
      ScaleDPI(WinControl.Controls[i], FromDPI);
 
  end;
 
end;
 
 
 
end.</syntaxhighlight>
 
 
 
Copy the "uscaledpi.pas" file to the main folder of your project:
 
 
 
  MyProject\uscaledpi.pas
 
 
 
In the "uses" section of your project you need to add "uScaleDPI":
 
 
 
<syntaxhighlight>unit form1;
 
 
 
{$mode objfpc}{$H+}
 
 
 
interface
 
 
 
uses
 
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
 
  uScaleDPI; // This includes ScaleDPI procedure </syntaxhighlight>
 
 
 
The OnCreate event of each form calls the procedure in this way:
 
 
 
<syntaxhighlight>procedure TForm1.FormCreate(Sender: TObject);
 
begin
 
  ScaleDPI(Self,96); // 96 is the DPI you designed the Form1 
 
end;</syntaxhighlight>
 
 
 
'''Scale All Forms'''
 
 
 
You can resize all forms at once without having to touch each form's OnCreate event.
 
In order to do this open your project source (typically the ''Project1''.lpr file) and add uScaleDPI in the uses clause.
 
 
 
Then call the procedure ''HighDPI'' below the code that initializes the forms:
 
 
 
<syntaxhighlight>begin
 
  RequireDerivedFormResource := True;
 
  Application.Initialize;
 
  Application.CreateForm(TForm1, Form1);
 
  Application.CreateForm(TForm2, Form2);
 
  Application.CreateForm(TForm3, Form3);
 
  HighDPI(96);  // 96 is the DPI you designed the Form1, Form2 & Form3
 
  Application.Run;
 
end.</syntaxhighlight>
 
 
 
The result looks like this:
 
 
 
<syntaxhighlight>program Project1;
 
 
 
{$mode objfpc}{$H+}
 
 
 
uses
 
  {$IFDEF UNIX}{$IFDEF UseCThreads}
 
  cthreads,
 
  {$ENDIF}{$ENDIF}
 
  Interfaces, Forms,
 
  Unit1, Unit2, Unit3,
 
  uScaleDPI;
 
 
 
{$R *.res}
 
 
 
begin
 
  RequireDerivedFormResource := True;
 
  Application.Initialize;
 
  Application.CreateForm(TForm1, Form1);
 
  Application.CreateForm(TForm2, Form2);
 
  Application.CreateForm(TForm3, Form3);
 
  HighDPI(96);
 
  Application.Run;
 
end.</syntaxhighlight>
 
 
 
'''Advanced'''
 
 
 
Some controls have more properties or different property names like TToolBar buttons (ButtonHeight / ButtonWidth instead Width / Height). Also if you use fixed font sizes the behaviour can change in different OSs.
 
 
 
You can edit the ScaleDPI procedure to include code to scale all controls in the way you want.
 
 
 
This is the ''uscaledpi'' used in [[LazPaint]]. This is very useful for scaling ToolBars and ToolBox.
 
 
 
This is not the final High DPI unit, for example you can use under Windows different LCL widgets, like Qt and this can change the final result.
 
 
 
'''Link:''' [https://github.com/bgrabitmap/lazpaint/blob/master/lazpaint/uscaledpi.pas uscaledpi.pas in LazPaint]
 
 
 
=== Using AutoSize ===
 
 
 
You can enable the 'AutoSize' option for each control you have (including Forms). Then test the effect under the different DPI modes, with different 'skinning' themes (if available in your target OS) and different font sizes. This can be very useful technique, and has been employed to solve several Lazarus IDE HighDPI issues.
 
 
 
For example using the default AutoSize and ChildSizing most controls can be automatically sized and positioned. But the spacing must be scaled:
 
 
 
<syntaxhighlight>  with WinControl.ChildSizing do
 
  begin
 
    HorizontalSpacing := ScaleX(HorizontalSpacing, FromDPI);
 
    LeftRightSpacing := ScaleX(LeftRightSpacing, FromDPI);
 
    TopBottomSpacing := ScaleY(TopBottomSpacing, FromDPI);
 
    VerticalSpacing := ScaleY(VerticalSpacing, FromDPI);
 
  end;
 
</syntaxhighlight>
 
 
 
For more information see:
 
  
 
* [[Autosize / Layout]]
 
* [[Autosize / Layout]]
* [[LCL AutoSizing]]
+
* [[Anchor Sides]]
 
 
The uscaledpi.pas with this additional code:
 
 
 
<syntaxhighlight> unit uscaledpi;
 
 
 
{$mode objfpc}{$H+}
 
 
 
interface
 
 
 
uses
 
  Forms, Graphics, Controls;
 
 
 
procedure HighDPI(FromDPI: integer);
 
procedure ScaleDPI(Control: TControl; FromDPI: integer);
 
 
 
implementation
 
 
 
procedure HighDPI(FromDPI: integer);
 
var
 
  i: integer;
 
begin
 
  if Screen.PixelsPerInch = FromDPI then
 
    exit;
 
 
 
  for i := 0 to Screen.FormCount - 1 do
 
    ScaleDPI(Screen.Forms[i], FromDPI);
 
end;
 
 
 
procedure ScaleDPI(Control: TControl; FromDPI: integer);
 
var
 
  i: integer;
 
  WinControl: TWinControl;
 
begin
 
  if Screen.PixelsPerInch = FromDPI then
 
    exit;
 
 
 
  with Control do
 
  begin
 
    Left := ScaleX(Left, FromDPI);
 
    Top := ScaleY(Top, FromDPI);
 
    Width := ScaleX(Width, FromDPI);
 
    Height := ScaleY(Height, FromDPI);
 
  end;
 
 
 
  if Control is TWinControl then
 
  begin
 
    WinControl := TWinControl(Control);
 
    if WinControl.ControlCount = 0 then
 
      exit;
 
 
 
    with WinControl.ChildSizing do
 
    begin
 
      HorizontalSpacing := ScaleX(HorizontalSpacing, FromDPI);
 
      LeftRightSpacing := ScaleX(LeftRightSpacing, FromDPI);
 
      TopBottomSpacing := ScaleY(TopBottomSpacing, FromDPI);
 
      VerticalSpacing := ScaleY(VerticalSpacing, FromDPI);
 
    end;
 
 
 
    for i := 0 to WinControl.ControlCount - 1 do
 
      ScaleDPI(WinControl.Controls[i], FromDPI);
 
  end;
 
end;
 
 
 
end.
 
    </syntaxhighlight>
 
 
 
 
 
=== Per-Monitor DPI Aware Application (since Windows 8.1) ===
 
 
 
Windows 8.1 introduced the possibility to have different scaling factors for each display connected to a computer (e.g. different DPI values per monitor). In order to support per-monitor DPI-awareness some additional changes are required.
 
 
 
==== STEP 1 - Declare Per-Monitor High DPI Awareness ====
 
 
 
In order to declare per-monitor DPI awareness new values were added to the <code>dpiAware</code> property of the application manifest (see [https://msdn.microsoft.com/en-us/library/windows/desktop/dn469266(v=vs.85).aspx#step_1_set_the_dpi_awareness_level], [https://blogs.msdn.microsoft.com/chuckw/2013/09/10/manifest-madness]). Lazarus does not yet support these values as an UI option (see [http://mantis.freepascal.org/view.php?id=30170 issue 30170]), so you have to declare the value by supplying you own application manifest.
 
 
 
To do so create 2 files with the following content:
 
 
 
;<code>highDPI.rc</code>
 
<syntaxhighlight>1 24 "highDPI.xml"</syntaxhighlight>
 
 
 
;<code>highDPI.xml</code>
 
<syntaxhighlight lang=xml><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
  <asmv3:application xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
 
    <asmv3:windowsSettings
 
        xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
 
      <dpiAware>True/PM</dpiAware>
 
    </asmv3:windowsSettings>
 
  </asmv3:application>
 
</asmv1:assembly></syntaxhighlight>
 
 
 
The <code>True/PM</code> value for the <code>dpiAware</code> property tells Windows that the application is per-monitor DPI aware and DPI scaling should be disabled completely.
 
 
 
Add the following line to your projects <code>.lpr</code> file:
 
 
 
<syntaxhighlight>{$R highDPI.rc}</syntaxhighlight>
 
 
 
It will compile the resource file using <code>windres</code> and include it in your project.
 
 
 
==== STEP 2 - Scale Forms and Controls when the Form is Moved to a Different Monitor ====
 
 
 
Scaling works as described before, however you also have to react to monitor changes. This is done by listening for the <codeWM_DPICHANGED</code> message, which is sent to a window when the effective dots per inch has changed, i.e. it was moved to another monitor (see [https://msdn.microsoft.com/de-de/library/windows/desktop/dn312083.aspx] for details).
 
 
 
To receive window messages declare the global variable <code>var PrevWndProc: WNDPROC;</code> and add the following code to the <code>FormCreate</code> procedure of your form (see also [[Win32/64 Interface#Processing non-user messages in your window]]):
 
 
 
<syntaxhighlight>PrevWndProc := Windows.WNDPROC(SetWindowLongPtr(Self.Handle,GWL_WNDPROC,PtrInt(@WndCallback)));</syntaxhighlight>
 
 
 
The procedure <code>WndCallback</code> is the callback that should be executed when a message is recevied and should look as follows:
 
 
 
<syntaxhighlight>function WndCallback(Ahwnd: HWND; uMsg: UINT; wParam: WParam; lParam: LParam):LRESULT; stdcall;
 
var
 
  windowPos: TRect;
 
begin
 
  if uMsg=WM_DPICHANGED then begin
 
    // https://msdn.microsoft.com/de-de/library/windows/desktop/dn312083.aspx
 
    //  wParam: hiword/loword contain new x/y DPI
 
    //  lparam: pointer to RECT structure with suggested window dimensions
 
    ScaleDPI(Form1, Form1.currDPI, lo(wParam), hi(wParam));
 
    Form1.currDPI := lo(wParam);
 
    windowPos := TRect(Pointer(lParam)^);
 
    Form1.Top := windowPos.Top;
 
    Form1.Left := windowPos.Left;
 
    result := 0;
 
    exit;
 
  end;
 
 
 
  result := CallWindowProc(PrevWndProc, Ahwnd, uMsg, WParam, LParam);
 
end;</syntaxhighlight>
 
 
 
Whenever the <code>WM_DPICHANGED</code> message is received your form will be rescaled and repositioned to match the new DPI value. Note that <code>Form1.currDPI</code> should be declared as a public integer variable of your form ("Form1" in this example) and should be initially set using <code>currDPI := Screen.PixelsPerInch;</code> from within the <code>FormCreate</code> procedure.
 
 
 
;Notes:
 
* Font sizes might be a bit tricky. Either a) specifically set all font sizes an make sure the <code>ScaleDPI</code> procedure adjusts them all or b) (method preferred by the author) set all controls to use the parents font size and adapt the <code>ScaleDPI</code> procedure to only adjust the font size of <code>TForm</code>s.
 
* Title bars won't be rescaled when moving the window to a different monitor. This seems to be a known issue (see [http://stackoverflow.com/questions/36864894/scaling-the-non-client-area-title-bar-menu-bar-for-per-monitor-high-dpi-suppo],[http://stackoverflow.com/questions/31781767/how-do-you-scale-the-title-bar-on-a-dpi-aware-win-application]) with no easy solution.
 
  
 
== External Links ==
 
== External Links ==
Line 491: Line 177:
 
* [http://msdn.microsoft.com/en-us/library/windows/apps/hh465362 Guidelines for scaling] Windows Dev Center - User experience guidelines for scaling Metro style apps
 
* [http://msdn.microsoft.com/en-us/library/windows/apps/hh465362 Guidelines for scaling] Windows Dev Center - User experience guidelines for scaling Metro style apps
 
* [[Windows Icon]] How to create icons that work with High DPI.
 
* [[Windows Icon]] How to create icons that work with High DPI.
 
[[Category:Tutorials]]
 
[[Category:Windows]]
 
[[Category:Windows Widgetsets]]
 

Revision as of 11:49, 13 April 2021

Deutsch (de) English (en) español (es) русский (ru)

Introduction

DPI (Dots Per Inch) is the relation between size in pixels and the actual display size. Here dot is an equivalent for pixel in printing terminology. Applications can either use pixel sizes, or take into account the actual display size. In this second case, sizes are given in points.

Most of today operating systems use default DPI set to 96 and allow to change it to higher value manually. The physical DPI can be determined from display through EDID protocol from physical size data and actual resolution. But the physical DPI is not used automatically by system so if you connect video output to monitor with different size then sceen resolution and visual size of controls are not automatically changed.

Usually DPI is presented as one value but it can be different for horizontal and vertical axes if pixel is not square.

In addition to basic application DPI awareness you can add own DPI options to your application to allow users to set custom per application DPI to overcome wrong system DPI setting.

Lazarus DPI related properties


Pixels and points

For example 300 DPI means that there are 300 pixels (or dots) per inch. There are 72 points per inch, so :

300 pixels ↔ 1 inch

300/72 pixels ↔ 1 point

4.16 pixels ↔ 1 point

Now with 96 DPI :

96 pixels ↔ 1 inch

1.33 pixel ↔ 1 point

Now with 144 DPI :

144 pixels ↔ 1 inch

2 pixels ↔ 1 point

Setting High DPI

Windows

On Windows 95 and later, it is possible to change the DPI ratio to make elements bigger. High DPI means any custom DPI setting with more than 96 DPI (the default setting) *.

High DPI awareness means that an application takes this DPI setting into account.

Windows Vista and Windows 7

In Windows 7 go to "Control Panel > Appearance and Personalization > Display" (or just Control Panel > Display in recent updates).

Select Smaller 100% (default), Medium 125% or Larger 150%. If you select 100% (96 DPI) this is the default Windows DPI setting, (High DPI is not the default).

If you select 125% (120 DPI) the option "Use Windows XP style DPI scaling" is enabled. Applications you run under this setting are scaled as if running under Windows XP.

If you select 150% (144 DPI) the option "Use Windows XP style DPI scaling" is disabled (DPI Virtualization is enabled), and applications you run under this setting must be High DPI Awareness to prevent system scaling which will produce a blurred image.

You can also set your custom DPI setting via the option "Set custom text size (DPI)" and enable/disable the DPI Virtualization.

Windows 8 Metro Applications

For Windows 8 Metro Applications read this http://blogs.msdn.com/b/b8/archive/2012/03/21/scaling-to-different-screens.aspx

Windows 10

Windows 10 "Control Panel > Appearance and Personalization > Display" have more options. You can have different font sizes for each element: Title bar, Menu, Dialog box and so on. Ensure you test twice in order to check if everything works under different sizes.

Now is based on Font Size, not DPI. The DPI option is not recommended, but still there. So, instead of changing the size of all elements in desktop, this will change just the font size (And of course everything else is changed to fit).

Remember that under Windows 10 there are Universal Applications (WinRT) and the classic desktop applications (Win32). We're talking here about desktop applications.

Linux

On Linux DPI setting is more complicated and depends on used software and their version.

You can discover your current monitor DPI by command:

xdpyinfo|grep dots

You can change DPI to new value by command:

xrandr --dpi 144x144

To preserve setting after reboot you need to add the command as script to /etc/X11/Xsession.d/77set_dpi.

More information:

Examples

Fixed Font Sizes (not HighDPI)

Here is a form with an undefined font size (set to zero, which is the default value). It has been designed at 96 DPI (100%), and it looks like this :

Testdpi100.png

Now, at 120 DPI (125%), it becomes :

Testdpi125.png

As you can see, the font gets bigger and so the text is clipped. The window title gets bigger, but the client area of the window remains the same size. Note that these changes in size can occur by using an application with a different Windows theme, or with another operating system.

To avoid this, you must set the font size to a non-zero value. Note that Font.Size is expressed in points and Font.Height is expressed in pixels. In fact, only the value of Font.Height is stored, and Font.Size changes according to current DPI value. So if we set the font size, it will be fixed to a certain size in pixels.

If we try again with a fixed font size of 9 points, then at 96 DPI (100%), we get this :

Testdpi100fixedM12P9.png

Now if the same program is run at 120 DPI (125%), it becomes :

Testdpi125fixedM12P9.png

The result is the almost the same. The title bar is bigger, but the client area and the font size is the same. Note that in fact, the size in points of the font has changed.

The conclusion from this is that it is possible to avoid inconsistency in the display by fixing font sizes. But we do not take into account that the graphical elements may be smaller according to actual DPI of the screen. With DPI awareness, it is possible to make an application behave as if it knew the real size of the pixels.


DPI Aware Application (For Vista +)

CPickSniff is an application to capture screen colors. We will use it as an example to see how High DPI works in Windows.

Default DPI

This is the app running at 96 DPI (100%). It's the default mode, when scaling isn't necessary.

cpicksniff defaultdpi.png

Windows DPI Scaling

This is the same app running at 144 DPI (150%) without a manifest, so Windows scales it like a bitmap. The result is a blurred image.

cpicksniff blured.png

With Manifest

Running at 144 DPI (150%). This time the app includes a manifest but the application contains no code to handle scaling. Items aren't scaled whereas fonts are scaled (Windows does this automatically), so text is clipped.

cpicksniff nohighdpi.png

High DPI

Finally with both a manifest and a LCL scaling, the app is in High DPI.

cpicksniff highdpi.png

High DPI in Lazarus 1.8 and above

To handle High DPI using new features in 1.8, follow these steps:

  • On Windows: enable DPI awareness in Project Options -> Application. Decide if you want to support per monitor DPI awareness or not.
  • Enable LCL scaling for your application DPI awareness in Project Options -> Application -> "Use LCL scaling (Hi-DPI).
  • Set TForm.Scaled=True for all your forms (it is the default value). All WYSIWYG should work automatically. Also the designer scales the forms accordingly.
  • If you create controls run-time, scale all coordinates, sizes etc that have to be DPI-aware with TControl.Scale96ToForm() or ScaleDesignToForm() (depending on your choice of default PPI) or prepare your container (e.g. panel with controls) as it was with 96 PPI and then call TControl.AutoAdjustLayout(lapAutoAdjustForDPI, 96, ParentFormOfTheContainer.PixelsPerInch, 0, 0);
  • If some of your components don't scale their inner sizes, override DoAutoAdjustLayout and scale the sizes (see TToolBar) - it has to be done for all controls. If a LCL control misses DoAutoAdjustLayout please report to mantis and provide a patch if you can.
  • Hint: Do not develop under HighDPI Mode, always develop under normal mode and only test under HighDPI. Otherwise Lazarus IDE will set DesignTimePPI of the different forms to different values.

Lazarus IDE high DPI

The Lazarus IDE is DPI-aware itself.

High DPI in older Lazarus

Is not as nice as 1.8 but it works, call on each form OnCreate event:

Self.AutoAdjustLayout(lapAutoAdjustForDPI, 96, Screen.PixelsPerInch, Self.Width, ScaleX(Self.Width, 96));

See also

External Links