@@ -16,16 +16,29 @@ interface
1616 WM_HEIDISQL_DEFERREDFREE = $1100 ;
1717
1818type
19+ TMonitorDpiChangedEvent = procedure(Sender: TObject; OldDPI: Integer; NewDPI: Integer) of object ;
20+
1921 // Form with a sizegrip in the lower right corner, without the need for a statusbar
2022
2123 { TExtForm }
2224
2325 TExtForm = class (TForm)
26+ private
27+ FCurrentPPI: Integer;
28+ FOnMonitorDpiChanged: TMonitorDpiChangedEvent;
29+ procedure FixTreeFontsOnDPIChange (Owner: TComponent; NewDPI, OldDPI: Integer);
2430 protected
2531 procedure FilterNodesByEdit (Edit: TEditButton; Tree: TLazVirtualStringTree);
2632 procedure WMDeferredFree (var Message: TLMessage); message WM_HEIDISQL_DEFERREDFREE;
33+ procedure WMDPIChanged (var Msg: TLMessage); message LM_DPICHANGED;
34+ procedure DoShow ; override;
35+ procedure DoMonitorDpiChanged (OldDPI, NewDPI: Integer); virtual ;
2736 public
2837 constructor Create(AOwner: TComponent); override;
38+ // Applies the tree font/height fixes for the given DPI and fires
39+ // OnMonitorDpiChanged if the DPI changed. Called from WMDPIChanged and
40+ // once at startup from DoShow.
41+ procedure ApplyDPIFix (NewDPI: Integer);
2942 procedure DeferFreeControl (AControl: TControl);
3043 class procedure InheritFont (AFont: TFont);
3144 class procedure SaveListSetup (List: TLazVirtualStringTree);
@@ -37,6 +50,7 @@ TExtForm = class(TForm)
3750 function Space (Multiply: Integer=1 ): Integer;
3851 class procedure PageControlTabHighlight (PageControl: TPageControl);
3952 class procedure ShowPopup (ClickedControl: TControl; PopupMenu: TPopupMenu);
53+ property OnMonitorDpiChanged: TMonitorDpiChangedEvent read FOnMonitorDpiChanged write FOnMonitorDpiChanged;
4054 end ;
4155
4256 TExtComboBox = class (TComboBox)
@@ -63,11 +77,95 @@ constructor TExtForm.Create(AOwner: TComponent);
6377var
6478 OldImageList: TCustomImageList;
6579begin
80+ FCurrentPPI := 0 ;
6681 inherited ;
6782
6883 InheritFont(Font);
6984end ;
7085
86+ procedure TExtForm.WMDPIChanged (var Msg: TLMessage);
87+ begin
88+ // Let the LCL scale fonts and bounds first (AutoAdjustLayout inside
89+ // TCustomForm.WMDPIChanged); the new DPI comes directly from the message.
90+ inherited ;
91+ ApplyDPIFix(hi(Cardinal(Msg.wParam)));
92+ end ;
93+
94+ procedure TExtForm.DoShow ;
95+ begin
96+ inherited ;
97+ // Trigger the DPI fix once at startup (and when re-shown on a monitor with a
98+ // different DPI): on a high-DPI primary monitor no WM_DPICHANGED is sent at
99+ // startup, so without this the tree fonts/heights would not be scaled.
100+ ApplyDPIFix(Monitor.PixelsPerInch);
101+ end ;
102+
103+ procedure TExtForm.ApplyDPIFix (NewDPI: Integer);
104+ var
105+ OldDPI: Integer;
106+ begin
107+ OldDPI := FCurrentPPI;
108+ if OldDPI <= 0 then
109+ OldDPI := 96 ; // design-time DPI
110+ if (NewDPI > 0 ) and (NewDPI <> FCurrentPPI) then begin
111+ // Moving window to a different screen or user changed the DPI setting.
112+ FCurrentPPI := NewDPI;
113+ FixTreeFontsOnDPIChange(Self, NewDPI, OldDPI);
114+ DoMonitorDpiChanged(OldDPI, NewDPI);
115+ end ;
116+ end ;
117+
118+ procedure TExtForm.FixTreeFontsOnDPIChange (Owner: TComponent; NewDPI, OldDPI: Integer);
119+ var
120+ i: Integer;
121+ VT: TLazVirtualStringTree;
122+ Node: PVirtualNode;
123+ begin
124+ // Recurse through owned components: query tabs etc. are owned by the form,
125+ // but their trees are owned by panels/tabsheets owned by them.
126+ for i := 0 to Owner.ComponentCount - 1 do begin
127+ if Owner.Components[i] is TLazVirtualStringTree then begin
128+ VT := TLazVirtualStringTree(Owner.Components[i]);
129+ // VirtualTrees paint into an offscreen bitmap whose canvas font PPI stays
130+ // at Screen.PixelsPerInch (primary monitor). TFont.Assign converts via Size
131+ // when PPIs differ, which would downconvert the LCL-scaled font back to
132+ // 96 dpi, so pin the PPI to the buffer canvas PPI and set the pixel height
133+ // for the monitor DPI directly. ParentFont must be set to False explicitly:
134+ // assigning Font clears it internally (TControl.FontChanged), and then the
135+ // parent font would no longer be copied on the next DPI change.
136+ VT.ParentFont := False;
137+ VT.Font.PixelsPerInch := Screen.PixelsPerInch;
138+ VT.Font.Height := -MulDiv(Font.Size, NewDPI, 72 );
139+ // The header has its own font (ParentFont default False) which the LCL
140+ // does not scale either; apply the same fix.
141+ VT.Header.ParentFont := False;
142+ VT.Header.Font.PixelsPerInch := Screen.PixelsPerInch;
143+ VT.Header.Font.Height := VT.Font.Height;
144+ // VT does not scale node/header heights on a DPI change, so scale them by
145+ // the DPI ratio (this preserves multiline multipliers set by FixVT).
146+ VT.DefaultNodeHeight := MulDiv(VT.DefaultNodeHeight, NewDPI, OldDPI);
147+ VT.Header.Height := MulDiv(VT.Header.Height, NewDPI, OldDPI);
148+ VT.BeginUpdate;
149+ try
150+ Node := VT.GetFirstInitialized;
151+ while Assigned(Node) do begin
152+ VT.NodeHeight[Node] := MulDiv(VT.NodeHeight[Node], NewDPI, OldDPI);
153+ Node := VT.GetNextInitialized(Node);
154+ end ;
155+ finally
156+ VT.EndUpdate;
157+ end ;
158+ end ;
159+ FixTreeFontsOnDPIChange(Owner.Components[i], NewDPI, OldDPI);
160+ end ;
161+ end ;
162+
163+ procedure TExtForm.DoMonitorDpiChanged (OldDPI, NewDPI: Integer);
164+ begin
165+ if Assigned(FOnMonitorDpiChanged) then
166+ FOnMonitorDpiChanged(Self, OldDPI, NewDPI);
167+ end ;
168+
71169
72170class procedure TExtForm.InheritFont (AFont: TFont);
73171var
0 commit comments