I built a simple Delphi program to test two solutions provided by Martin Schneider on StackOverflow here:
Enabling TWebBrowser High DPI support
… but when the program is dragged to a monitor scaled to 175%, the page content remains unscaled.
Here’s my code for Martin’s “Solution 2”:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.IOUtils,
Vcl.StdCtrls, Vcl.ExtCtrls, ShellAPI, Vcl.ComCtrls, Vcl.ToolWin, System.StrUtils,
ShlObj, System.UITypes, Vcl.OleCtrls, SHDocVw,
Winapi.Mshtmhst;
type
TDpiAwareWebBrowser = class(TWebBrowser, IDocHostUIHandler)
strict private
// IDocHostUIHandler "override"
function GetHostInfo(var pInfo: TDocHostUIInfo): HRESULT; stdcall;
end;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
DOCHOSTUIFLAG_DPI_AWARE = $40000000;
function TDpiAwareWebBrowser.GetHostInfo(var pInfo: TDocHostUIInfo): HRESULT;
begin
// original code from TWebBrowser.GetHostInfo
pInfo.cbSize := SizeOf(pInfo);
pInfo.dwFlags := 0;
pInfo.dwFlags := pInfo.dwFlags or DOCHOSTUIFLAG_NO3DBORDER;
pInfo.dwFlags := pInfo.dwFlags or DOCHOSTUIFLAG_THEME;
pInfo.dwFlags := pInfo.dwFlags or DOCHOSTUIFLAG_DPI_AWARE; // NEW added flag
Result := S_OK;
//ResizeScrollBars; // will be called by subsequent routines anyway.
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
var wb:= TDpiAwareWebBrowser.Create(Form1);
TWinControl(wb).Parent:= Form1;
wb.align:= alClient;
wb.Navigate('https://google.com');
end;
end.
In this screenshot, you can see the title bar of the test program is properly scaled for a monitor set to 175%, but the TWebBrowser based component isn’t:
Here’s a screenshot showing the monitor’s scaling:
I also implemented Martin’s “Solution 1” as shown in this screenshot of the Windows registry:
I’m using Delphi 11:
I’m developing on Windows 10 inside a VMware Workstation 16 Pro virtual machine.
What am I missing?