DPI缩放运行时创建的具有PopupMenu的控件

问题描述

我正在使用Delphi 10.4.1 Sydney,并创建了一个简单的控件,并在运行时设置了Parent

constructor TMyTree.Create(AOwner: TComponent);
begin
  inherited Create(Owner);
  PopupMenu := TPopupMenu.Create(Self);
end;

procedure TMyTree.ChangeScale(M,D: Integer; isDpiChange: boolean);
begin
  inherited ChangeScale(M,D,isDpiChange);
  OutputDebugString(PChar(Format('M: %d,D: %d',[M,D])));
  //The program is started at 150% DPI and DPI changed to 175% DPI
  //Debug Output: M: 168,D: 96 - ERROR! It should be M: 168,D: 144
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  fTree := TMyTree.Create(Self);
  fTree.Parent := Self;
  OutputDebugString(PChar(Format('CurrentPPI: %d',[fTree.CurrentPPI])));
  //Debug Output at 150% DPI scale: CurrentPPI: 144 - OK!
end;

问题在于,VCL用错误的参数调用ChangeScale()(请参见代码中的注释)。

我认为这是由于在PopupMenu := TPopupMenu.Create(Self);构造函数调用TMyTree时,VCL将树的ComponentState设置为[csFreeNotification]

我已经检查了VCL代码,更具体地说,检查了TControl.SetParent()。如果csFreeNotificationComponentState中,则调用ScaleForPPI,并且当树的视图设置为Parent时,树视图不会缩放。

这是VCL中的错误,还是有其他一些首选的方式来创建运行时控件并设置其Parent

顺便说一句,如果我在timageList.Create(Self)构造函数中使用TMyTree,也会发生同样的问题。

解决方法

这是VCL中的一个已知问题。 Quality Portal中有几张与此有关的票证:

RSP-15381: A scaled form gets resized to design time ClientWidth/ClientHeight when embeded in a parent form

RSP-18162: Frame with Assigned PopupMenu is wrong Displayed on high DPI

RSP-19012: Parented controls with free notifications aren't scaled

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...