How to get real display resolution in Delphi 7 regardless of scale factor?
09:01 16 Dec 2020

I have some problems with getting the screen resolution in Delphi 7 when the scale is more than 125%.

The code I used for testing:

procedure TForm1.Button1Click(Sender: TObject);
var
  MonId: integer;
  Mon: TMonitor;
  AllMonitorsWidth, AllMonitorsHeight: integer;
begin
  Memo1.Clear;

  Memo1.Lines.Append(Format('Number of monitors: %d', [Screen.MonitorCount]));

  for MonId := 0 to Screen.MonitorCount - 1 do
  begin
    Mon := Screen.Monitors[MonId];

    With Memo1.Lines do
    begin
      Append('');
      Append(Format('--------  Monitor #%d  --------', [mon.MonitorNum]));
      Append(Format('Resolution: %dpx x %dpx', [Mon.Width, Mon.Height]));
      Append(Format('X-offset: %dpx', [Mon.Left]));
      Append(Format('Y-offset: %dpx', [Mon.Top]));
    end;
  end;


  AllMonitorsWidth  := GetSystemMetrics(SM_CXVIRTUALSCREEN);
  AllMonitorsHeight := GetSystemMetrics(SM_CYVIRTUALSCREEN);
  With Memo1.Lines do
  begin
    Append('');
    Append('--------  All monitors  --------');
    Append(Format('Resolution: %dpx x %dpx', [AllMonitorsWidth, AllMonitorsHeight]));
    Append(Format('PPI: %d', [Screen.PixelsPerInch]));
  end;
end;

For example, I run this test on a Windows 7 notebook with a 1366px × 768px display and get the following results:

  • 100% (96 ppi): correct result
  • 125% (120 ppi): correct result
  • 150% (144 ppi): incorrect result
  • 200% (192 ppi) incorrect result

As you can see on 150% and 200% scale program detects a wrong pixel density of 96ppi and the resolution is also incorrect.

I think in modern versions of Delphi there may be fixes. But is there any solution for Delphi 7 without upgrading to a newer version?

Moreover there may be more than one display and with different DPIs. So I need to adjust and get resolution for each of them.

windows delphi screen delphi-7 ppi