gpt4 book ai didi

delphi - 如何根据枚举类型信息调用重载函数?

转载 作者:行者123 更新时间:2023-12-03 15:10:11 26 4
gpt4 key购买 nike

我想将一些主题部分绘制到多个 TImage 上。在下面的代码中,GetElementDetails 需要某个枚举值。我有枚举类型的 PTypeInfo,但我不知道如何将 i 类型转换为枚举类型。

procedure TForm1.Button1Click(Sender: TObject);
procedure drawType(c: tcanvas; ti: ptypeinfo);
var
r: trect;
i: integer;
details: TThemedElementDetails;
begin
r.Left := 0; r.Top := 0; r.Right := 19; r.Bottom := 19;
for i := GetTypeData(ti).MinValue to GetTypeData(ti).MaxValue do begin
// How to cast i to the enum type of ti?
details := StyleServices.GetElementDetails( ???(i) );

StyleServices.DrawElement(c.Handle, details, R);
if (i mod 10 = 0) and (i > 0) then begin
r.Left := 0; r.Right := 19; r.Top := r.Bottom + 3; r.Bottom := r.Bottom + 22;
end
else r.Inflate(-22,0,22,0);
end;
end;
begin
drawType(image1.canvas, typeinfo(TThemedToolBar));
drawType(image2.canvas, typeinfo(TThemedButton));
drawType(image3.canvas, typeinfo(TThemedCategoryPanelGroup));
drawType(image4.canvas, typeinfo(TThemedComboBox));
end;

我需要将 i 转换为作为第二个变量传递的类型(TThemedToolBarTThemedButton 等)。我该如何解决这个问题?

最佳答案

试试这个:

type
TAbstractStyleServicesFunction = reference to function(const styleServices: TAbstractStyleServices; const I: Integer)
: TThemedElementDetails;


procedure TForm1.drawType(c: TCanvas; ti: PTypeInfo; const styleServicesFunction: TAbstractStyleServicesFunction);
var
r: trect;
I: Integer;
begin
r.Left := 0;
r.Top := 0;
r.Right := 19;
r.Bottom := 19;
for I := GetTypeData(ti).MinValue to GetTypeData(ti).MaxValue do
begin
styleServices.DrawElement(c.Handle, styleServicesFunction(styleServices, I), r);

if (I mod 10 = 0) and (I > 0) then
begin
r.Left := 0;
r.Right := 19;
r.Top := r.Bottom + 3;
r.Bottom := r.Bottom + 22;
end
else
r.Inflate(-22, 0, 22, 0);
end;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
drawType(image1.canvas, typeinfo(TThemedToolBar),
function(const styleServices: TAbstractStyleServices; const I: Integer): TThemedElementDetails
begin
Result := styleServices.GetElementDetails(TThemedToolBar(I));
end);
end;

关于delphi - 如何根据枚举类型信息调用重载函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25157610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com