Make VCL layout and custom drawing respond to high DPI

A high-DPI VCL application needs an appropriate process awareness mode, controls that scale with their form, and custom drawing that uses the current DPI. Enlarging a font alone does not solve clipped captions, fixed bitmap sizes or drawing code that assumes every display has 96 pixels per inch.

This walkthrough targets Delphi 13 Florence VCL on Windows. FireMonkey has different layout and scaling mechanisms. Older VCL versions and third-party controls need their own compatibility review.

Establish the form's DPI behavior

Create a VCL Forms Application. In the project manifest options, select Per Monitor v2 DPI awareness for the configurations you ship. Keep the form's Scaled property enabled. Use the designer's recorded design-time DPI consistently rather than manually changing it to compensate for a layout defect.

Arrange ordinary controls with Align, Anchors, suitable margins and sufficient room for captions. A fixed-width label sized for one English phrase may fail after font substitution or translation even when DPI scaling is correct. Prefer layout relationships that survive changes in both text and available space.

Scale custom drawing metrics

Add a TPaintBox named PaintBox1, align it to the client area and connect its OnPaint event. Add System.Types and Vcl.Graphics to the form unit's uses clause, then use this complete event implementation in a form class named TForm1:

Pascal
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
  Margin: Integer;
  BoxHeight: Integer;
  Bounds: TRect;
begin
  Margin := PaintBox1.ScaleValue(12);
  BoxHeight := PaintBox1.ScaleValue(48);
  Bounds := PaintBox1.ClientRect;
  PaintBox1.Canvas.Brush.Color := clWindow;
  PaintBox1.Canvas.FillRect(Bounds);
  if (Bounds.Right <= 2 * Margin) or (Bounds.Bottom <= 2 * Margin) then
    Exit;
  Bounds.Left := Margin;
  Bounds.Top := Margin;
  Bounds.Right := Bounds.Right - Margin;
  Bounds.Bottom := Bounds.Top + BoxHeight;
  if Bounds.Bottom > PaintBox1.ClientHeight - Margin then
    Bounds.Bottom := PaintBox1.ClientHeight - Margin;
  PaintBox1.Canvas.Brush.Color := clHighlight;
  PaintBox1.Canvas.FillRect(Bounds);
end;

The constants 12 and 48 are authored at the VCL default 96 PPI scale. ScaleValue converts them for the control's current DPI. At 96 PPI the margin is 12 pixels, at 144 PPI it is 18, and at 192 PPI it is 24. The blue rectangle should retain a similar physical size while using more device pixels.

Do not scale ClientWidth or ClientHeight again. Those are already current device dimensions. Repeatedly multiplying previously scaled values also accumulates rounding errors and can enlarge the control every time it moves between monitors. Recompute custom metrics from the original logical constants.

Provide suitable images and fonts

For toolbar and menu images, use the current VCL image collection and virtual image list facilities where the controls support them. Supply artwork with enough resolution or appropriate variants. Stretching one small raster icon across every DPI produces blur even when the control geometry is correct.

Let standard controls use their supported font and theme behavior. For custom text drawing, measure with the actual font used to draw and account for the available rectangle. Scaling a hard-coded character width does not account for different glyph widths or localized content.

Verify real monitor transitions

Test at 100%, 150% and 200% scaling, including moving the running form between monitors with different scales. Check initial startup, maximize and restore, modal dialogs, menus, images, keyboard focus and long captions. Resize the window smaller to distinguish DPI defects from ordinary insufficient-space handling.

Expected behavior is readable text, unclipped controls and consistent physical sizing after each transition. Check third-party controls independently. A correct manifest cannot repair drawing code that ignores DPI.

The drawing arithmetic can be checked without showing a window, but it does not verify VCL layout, theme rendering or monitor-change events. Complete visual acceptance requires the actual application on representative displays and the same manifest and build configuration intended for deployment.

References

High DPI guide

TControl.ScaleValue

TVirtualImageList

Question provenance

This article addresses How do I make my GUI behave well when Windows font scaling is greater than 100% , asked by UnDiUdin on Stack Overflow. The source question is licensed under CC BY-SA 3.0 . The explanation and examples here are newly authored for current Delphi. No answer text or code is reproduced.

Author: Herman Schoenfeld