我想为几个TImages画一些主题部分.在我的下面的代码中,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;
Run Code Online (Sandbox Code Playgroud)
我需要强制转换i为第二变量(通过型式TThemedToolBar,TThemedButton等等).我怎么解决这个问题?
你不能轻易做到这一点。该GetElementDetails方法的第一个参数严重过载。重载解析是静态的,在编译时执行。您希望根据枚举的运行时类型在运行时绑定到该方法。这对于正常的方法调用是不可能的。
实现此目的的唯一方法是使用 RTTI。枚举样式服务对象的方法。找到所有有这个名字的人GetElementDetails。选择参数与枚举的运行时类型匹配的参数。然后使用 RTTI 调用它。
我无法编写任何代码来向您展示这一点,但是现在您知道需要做什么,这些技术有很多很好的示例。