Difference between revisions of "High DPI"
(code highlight) |
|||
Line 7: | Line 7: | ||
== High DPI in Lazarus == | == High DPI in Lazarus == | ||
− | With | + | With Lazarus we have to follow those steps in order to make an High DPI Awareness application for Windows 7. I'm not using ScaleBy & ScaleControls because those have a lot of bugs for this purpose. |
[http://lazarus.freepascal.org/index.php/topic,10466.msg59076.html#msg59076 Here] you can download an example. | [http://lazarus.freepascal.org/index.php/topic,10466.msg59076.html#msg59076 Here] you can download an example. | ||
Line 15: | Line 15: | ||
Else you can copy the below code and save to a text file "manifest.xml": | Else you can copy the below code and save to a text file "manifest.xml": | ||
− | + | <xml><?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"><description>Windows Shell</description> | <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"><description>Windows Shell</description> | ||
<dependency> | <dependency> | ||
Line 41: | Line 41: | ||
</windowsSettings> | </windowsSettings> | ||
</application> | </application> | ||
− | + | </assembly></xml> | |
And save the below code to a text file "manifest.rc": | And save the below code to a text file "manifest.rc": | ||
Line 54: | Line 54: | ||
And include in your project: | And include in your project: | ||
− | + | <delphi>implementation | |
− | + | {$R *.lfm} | |
− | + | {$R manifest.rc} // This includes Windows Manifest to enable Themes and High DPI</delphi> | |
=== STEP 2 - Scale Forms and Controls === | === STEP 2 - Scale Forms and Controls === | ||
Line 64: | Line 64: | ||
First copy the below code and save to a text file "uscaledpi.pas": | First copy the below code and save to a text file "uscaledpi.pas": | ||
− | + | <delphi>unit uscaledpi; | |
− | + | {$mode objfpc}{$H+} | |
− | + | interface | |
− | + | uses | |
Graphics, Controls; | Graphics, Controls; | ||
− | + | procedure ScaleDPI(Control: TControl; FromDPI: Integer); | |
− | + | implementation | |
− | + | procedure ScaleDPI(Control: TControl; FromDPI: Integer); | |
− | + | var | |
n: Integer; | n: Integer; | ||
WinControl: TWinControl; | WinControl: TWinControl; | ||
− | + | begin | |
with Control do begin | with Control do begin | ||
Left:=ScaleX(Left,FromDPI); | Left:=ScaleX(Left,FromDPI); | ||
Line 98: | Line 98: | ||
end; | end; | ||
end; | end; | ||
− | + | end; | |
− | + | end.</delphi> | |
Copy the "uscaledpi.pas" file to the main folder of your project: | Copy the "uscaledpi.pas" file to the main folder of your project: | ||
Line 108: | Line 108: | ||
In the "uses" section of your project you need to add "uScaleDPI": | In the "uses" section of your project you need to add "uScaleDPI": | ||
− | + | <delphi>unit form1; | |
− | + | {$mode objfpc}{$H+} | |
− | + | interface | |
− | + | uses | |
− | + | Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs | |
− | + | uScaleDPI; // This includes ScaleDPI procedure </delphi> | |
OnCreate event of each form call the procedure in this way: | OnCreate event of each form call the procedure in this way: | ||
− | + | <delphi>procedure TForm1.FormCreate(Sender: TObject); | |
− | + | begin | |
− | + | ScaleDPI(Self,96); // 96 is the DPI you designed the Form1 | |
− | + | end;</delphi> | |
== External Links == | == External Links == | ||
+ | |||
*[http://msdn.microsoft.com/en-us/library/dd464646(v=VS.85).aspx High DPI (Windows)] MSDN article about High DPI | *[http://msdn.microsoft.com/en-us/library/dd464646(v=VS.85).aspx High DPI (Windows)] MSDN article about High DPI | ||
*[http://lazarus.freepascal.org/index.php/topic,10924.html dpiScaling for Windows by Lainz] 2010-10-28 | *[http://lazarus.freepascal.org/index.php/topic,10924.html dpiScaling for Windows by Lainz] 2010-10-28 | ||
*[http://lazarus.freepascal.org/index.php/topic,10592.html Windows 7 High DPI Application Example] 2010-09-21 | *[http://lazarus.freepascal.org/index.php/topic,10592.html Windows 7 High DPI Application Example] 2010-09-21 | ||
*[http://lazarus.freepascal.org/index.php/topic,10466.html Windows 7 High DPI Application] 2010-09-09 | *[http://lazarus.freepascal.org/index.php/topic,10466.html Windows 7 High DPI Application] 2010-09-09 |
Revision as of 11:23, 23 January 2011
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 we have to follow those steps in order to make an High DPI Awareness application for Windows 7. I'm not using ScaleBy & ScaleControls because those have a lot of bugs for this purpose. Here you can download an example.
STEP 1 - Declare High DPI Awareness
To do this we need a manifest file that includes the declaration, with the newest Lazarus subversion 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 +)".
Else you can copy the below code and save to a text file "manifest.xml":
<xml><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"><description>Windows Shell</description> <dependency> <dependentAssembly> <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*" /> </dependentAssembly> </dependency> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="asInvoker" uiAccess="false"/> </requestedPrivileges> </security> </trustInfo> <application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> </windowsSettings> </application>
</assembly></xml>
And save the below code to a text file "manifest.rc":
1 24 "manifest.xml"
Then copy those files to the main folder of your project:
MyProject\manifest.xml MyProject\manifest.rc
And include in your project:
<delphi>implementation
{$R *.lfm} {$R manifest.rc} // This includes Windows Manifest to enable Themes and High DPI</delphi>
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 to a text file "uscaledpi.pas":
<delphi>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.</delphi>
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":
<delphi>unit form1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs uScaleDPI; // This includes ScaleDPI procedure </delphi>
OnCreate event of each form call the procedure in this way:
<delphi>procedure TForm1.FormCreate(Sender: TObject); begin
ScaleDPI(Self,96); // 96 is the DPI you designed the Form1
end;</delphi>
External Links
- High DPI (Windows) MSDN article about High DPI
- dpiScaling for Windows by Lainz 2010-10-28
- Windows 7 High DPI Application Example 2010-09-21
- Windows 7 High DPI Application 2010-09-09