I am trying to make my Delphi 10.3 application dpi aware and I understand that I must handle the WM_DPICHANGED
message for that. I encountered 2 problems:
I don't receive
WM_DPICHANGED
message at all, although I have included the "Per Monitor V2" setting in the manifest file. I don't have multiple monitors so I can only test by changing the OS scaling setting. Or this is not supossed to trigger that message ? If not, then why the Notepad windows it is scaled accordingly (see the video) ? How can I make my app to react in the same way ?How can I do the scaling when I receive the
WM_DPICHANGED
? Normally, I would have called some of the scaling functions, likeScaleBy()
, but Microsoft says that I must useSetWindowPos
with the value provided in LPARAM, if not, I will trigger some infinite loop. But this will only scale my window size, and I want to scale all window content.
Secondary question: Is there a way to trigger that message, other than having 2 monitors ?
Video demonstration: https://www.youtube.com/watch?v=b6DF3MZUD7w
Test app, just to see if I receive the message... but no "x" appears in the caption:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
procedure DpiChanged(var Msg: TMessage); message WM_DPICHANGED;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DpiChanged(var Msg: TMessage);
begin
caption:= caption + ' x';
//ScaleBy(Screen.PixelsPerInch + UserPPI, LastPPI);
end;
end.
TForm.Scaled
toFalse
and scaling manually, because I want to have a custom scaling, like a compensation, above all other scaling parameters.WM_DPICHANGED
is handled... But if I can't trigger that without 2 monitors, to test how it works, I think I will leave my app only with System DPI awareness... But at least I want to make it scale when windows scale factor changes (like Notepad in my question video). What should I hook to get notified when that happens ?WM_DISPLAYCHANGE
is fireed when windows scaling is changed, but now, the problem is that I have nowhere to get the new value of the PPI, because is not updated anywhere. I triedScreen.PixelPerInch
, API callGetDpiForSystem
... They all still have the old value... :(