Jump to content
pyscripter

TControl.SetParent

Recommended Posts

TControl.SetParent contains the following code:

 

      if not (csLoading in ComponentState) and not (csDesigning in ComponentState)
        and not (csDestroying in ComponentState) and not (csFreeNotification in ComponentState) then
      begin
        if not (csDesigning in Parent.ComponentState) then
          ScaleForPPI(GetParentCurrentDpi);
      end;

Apart from the fact that I would write the if condition as

      if [csLoading, csDesigning, csDestroying, csFreeNotification] * ComponentState = [] then

does anyone have any idea why controls with csFreeNotification are not scaled?   This makes writing per Monitor DPI aware applications much harder.

Edited by pyscripter

Share this post


Link to post

@Uwe Raabe Thanks!  Good to know I am not the only one bitten by this (should have checked QA),  but bad to know it has not been fixed for quite some time now.  I placed my vote on the first issue.

 

You can work around the bug by manually calling ScaleForPPI(future parent PPI)  before setting the parent but it gets difficult to get right with things like docking.   Embarcadero will face this issue when they try to make the IDE per monitor DPI-aware as promised.    

Edited by pyscripter

Share this post


Link to post

This is another tough one to work around:

 

procedure TCustomForm.ScaleForPPIRect(NewPPI: Integer; NewRect: PRect);
var
  LCurrentPixelsPerInch: Integer;
  I: Integer;
  PriorHeight: Integer;
begin
  if not (csDesigning in ComponentState) and
    ((not HandleAllocated and (Parent <> nil)) or (NewPPI < 30)) then
    Exit;

Parented forms with  unallocated handle are not scaled (e.g. page-like controls).  It becomes even harder since TCustomForm.SetParent destroys the handle if the form had no parent.

This is irrespective of FreeNotifications.

Edited by pyscripter

Share this post


Link to post
4 hours ago, Uwe Raabe said:

🤦‍♂️ Oh FFS - how many versions do we have to wait for properly functioning high dpi support. The fact that these were reported 3 or 4 years ago and have not been addressed is astounding. Surely if you are working on high dpi support you would look at these issues in your own bug tracking system.  

Share this post


Link to post

The workaround for both issues is to follow a pattern such as this:

Form.SetParent(Panel);
Form.HandleNeeded;
Form.ScaleForPPI(Panel.CurrentPPI);

 

  • Thanks 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×