问题描述
我正在使用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()
。如果csFreeNotification
在ComponentState
中,则不被调用ScaleForPPI
,并且当树的视图设置为Parent
时,树视图不会缩放。
这是VCL中的错误,还是有其他一些首选的方式来创建运行时控件并设置其Parent
?
顺便说一句,如果我在timageList.Create(Self)
构造函数中使用TMyTree
,也会发生同样的问题。