Print This Post

5-Minute-Snack: Determine all your display dimensions with resolution in Delphi

Back in the day, you did not need to know much about the display that your application has been started on except its dimensions. Forms needed to be resized so they would fit the display nicely. Hardly anybody had multiple displays and resolutions of 4000 pixels were unthinkable. Today, the requirement for applications have changed and multi-display environments with 4K displays have become very common. Thus, Delphi offers very comfortable means to inquire about the connected displays using the Screen object:

procedure TFrmMain.ShowDisplays;
var
  LMonitor : TMonitor;
  LPrimary: String;

begin
  Log.Lines.Clear;

  for var c := 0 to Screen.MonitorCount - 1 do
  begin
    LMonitor := Screen.Monitors[c];
    LPrimary := '';

    if LMonitor.Primary then
    begin
      LPrimary := '(Primary)';
    end;

    Log.Lines.Add( Format('Display %d: %dx%d %3d ppi %s', [
      LMonitor.MonitorNum,
      LMonitor.Width,
      LMonitor.Height,
      LMonitor.PixelsPerInch,
      LPrimary ] ) );
  end;

end;

You can inquire the number of displays connected using MonitorCount and then iterate all the displays. The TMonitor class gives you all the information you need about all displays. The source code dumps all the info into a memo.

Be aware that you also get information about the resolution of your display. If your Windows system is scaling every image at 100%, it will most likely be 96 ppi.

However, most displays nowadays work at zoom factors of 200% or even 300%. This is what makes development of VCL applications that use images a bit tricky. Delphi Rio introduces excellent ImageList controls that allow you to specify the same image for different resolutions. What you need to remember is that if your display is set to 200% all the images used must be twice as large to look the same as when running on a 100% display!

As said, the VCL has been updated for this, even if you move windows between different displays with different resolutions. Key is that you use the new ImageList components.

The source code above allows you to provide images at run-time in the correct resolution. Let’s say you want to present an image that you retrieved from a web service at 96 ppi and you want to display it at 192 ppi you need to stretch it by the factor 2. In order to know that the display that is being used is at 192 ppi, you need something like the code above.

This is the output for my current workstation:

Tags: , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Partnerships



Top