加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何基于enum typeinfo调用重载函数?

发布时间:2020-12-15 09:30:25 所属栏目:大数据 来源:网络整理
导读:我想为几个T Images画一些主题部分.在下面的代码中,GetElementDetails需要一定的枚举值.我有枚举类型的PTypeInfo,但我不知道如何将类型转换为枚举类型. procedure TForm1.Button1Click(Sender: TObject); procedure drawType(c: tcanvas; ti: ptypeinfo); va
我想为几个T Images画一些主题部分.在下面的代码中,GetElementDetails需要一定的枚举值.我有枚举类型的PTypeInfo,但我不知道如何将类型转换为枚举类型.

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,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转换为作为第二个变量传递的类型(TThemedToolBar,TThemedButton等).我怎么解决这个问题?

解决方法

你不能轻易做到这一点. GetElementDetails方法在其第一个参数上严重超载.重载分辨率是静态的,在编译时执行.您希望在运行时根据枚举的运行时类型绑定到方法.这对普通的方法调用是不可能的.

唯一的方法是使用RTTI.枚举样式服务对象的方法.找到名为GetElementDetails的所有名称.选择其参数与枚举的运行时类型匹配的那个.然后使用RTTI调用它.

我无法编写任何代码来向您展示这一点,但是现在您知道需要做什么,这些技术有很多很好的例子.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读