user7421317
user7421317

Reputation:

How to use a DPI aware value in a property of a component?

I'm developing a text editor component with a popup window. The width of popup window should be set while designtime inside a properties editor for my component.

How can I apply the setted width to the screen resolution?

If I enter a value for Width, Delphi stores the value inside the .dfm file. Also there are stored a PixelsPerInch value. If I place a Width = 96 while I'm using a Windows DPI setting of 100%, inside the .dfm will be stored:

PixelsPerInch = 96
Width = 96

If I change my Windows DPI setting right now to 150% and reopen this project in Delphi, the properties editor shows me 148 for the width.

I would like to have this too for my value:

published
  property PopupWidth: Integer read FPopupWidth write FPopupWidth;

... but Delphi does not apply the PixelsPerInch handling for my value.

Why not? And how can I implement this?

enter image description here

Upvotes: 0

Views: 559

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

This scaling mechanism is handled by explicit code in the VCL. The framework provides the virtual ChangeScale method of TControl to allow your controls to participate.

You would override ChangeScale for your control and implement it like this:

procedure TMyControl.ChangeScale(M, D: Integer);
begin
  if sfWidth in ScalingFlags then
    PopupWidth := MulDiv(PopupWidth, M, D);
  inherited;
end;

Upvotes: 2

Related Questions