High DPI

From Lazarus wiki
Revision as of 04:16, 23 January 2011 by Lainz (talk | contribs) (→‎Definition)
Jump to navigationJump to search

Definition

Any custom DPI setting with more than 96 DPI (the default setting) *.

Setting High DPI in Windows

In Windows 7 go to Control Panel > Appareance and Personalization > Display. Select Smaller 100% (default), Medium 125% or Larger 150%. If you select 100% (96 DPI) is the default Windows DPI setting, not High DPI. If you select 125% (120 DPI) the option "Use Windows XP style DPI scaling" is enabled, applications you run under this setting are scaled like at Windows XP. If you select 150% (144 DPI) the option "Use Windows XP style DPI scaling" is disabled (DPI Virtualization enabled), applications you run under this setting must be High DPI Awareness else they will be scaled by the system like a blurred image. Also you can set your custom DPI setting in the option "Set custom text size (DPI)" and enable/disable the DPI Virtualization. The best way to get it is changing the settings and running applications under the different settings.

High DPI in Lazarus

With Lazarus IDE follow this steps: - STEP 1) Declare High DPI Awareness - STEP 2) Scale Forms and Controls

STEP 1 - Declare High DPI Awareness

To do this we need a manifest file that includes the declaration, in the Lazarus SVN we can do this going to Options > Project Options > then select the options "Use Manifest to Enable Themes (Windows)" and "Dpi Aware application (for Vista +)".

STEP 2 - Scale Forms and Controls

To do this we can call ScaleDPI procedure OnCreate event of each form in your project.

First copy the below code and save this to a text file "uscaledpi.pas":

 unit uscaledpi;
 
 {$mode objfpc}{$H+}
 
 interface
 
 uses
 Graphics, Controls;
 
 procedure ScaleDPI(Control: TControl; FromDPI: Integer);
 
 implementation
 
 procedure ScaleDPI(Control: TControl; FromDPI: Integer);
 var
 n: Integer;
 WinControl: TWinControl;
 begin
 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 begin
     for n:=0 to WinControl.ControlCount-1 do begin
       if WinControl.Controls[n] is TControl then begin
         ScaleDPI(WinControl.Controls[n],FromDPI);
       end;
     end;
   end;
 end;
 end;
 
 end.

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":

 unit form1;
 
 {$mode objfpc}{$H+}
 
 interface
 
 uses
   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs
   uScaleDPI; // This includes ScaleDPI procedure     

OnCreate event of each form call the procedure in this way:

 procedure TForm1.FormCreate(Sender: TObject);
 begin
   ScaleDPI(Self,96); // 96 is the DPI you designed the Form  
 end;

External Links

High DPI (Windows)