'Scaling the application and Handling WM_DPICHANGED message

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:

  1. 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 ?

  2. How can I do the scaling when I receive the WM_DPICHANGED ? Normally, I would have called some of the scaling functions, like ScaleBy(), but Microsoft says that I must use SetWindowPos 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.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source