替换delphi中的组件类 [英] Replacing a component class in delphi

查看:31
本文介绍了替换delphi中的组件类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我在某处看到了一个 hack 示例,用于定义现有 VCL 组件(如 TButton 或 TEdit)的自定义版本,并使用相同的类名并执行一些操作,以便 DFM 流媒体将实例化您的版本而不是原始版本.不幸的是,我处于一种需要能够做到这一点的情况,但我找不到相关文章.有谁知道在哪里可以找到有关如何完成此操作的信息?

I know I've seen an example somewhere of a hack to define a custom version of an existing VCL component, like TButton or TEdit, with the same class name and do something to make it so that the DFM streamer will instantiate your version instead of the original. Unfortunately, I'm in a situation where I need to be able to do that and I can't find the write-up. Does anyone know where to find information on how to accomplish this?

推荐答案

在你的表单中,你可以像这样覆盖 ReadState 方法:

In your form you can override the ReadState method like so:

type
  TMyForm = class(TForm)
  protected
    procedure ReadState(Reader: TReader); override;
  end;

procedure TMyForm.ReadState(Reader: TReader);
begin
  Reader.OnFindComponentClass := FindComponentClass;
  inherited;
end;

procedure TMyForm.FindComponentClass(Reader: TReader; const ClassName: string;
  var ComponentClass: TComponentClass);
begin
  if ComponentClass=TButton then begin
    ComponentClass := TMySuperDuperButton;
  end else if ComponentClass=TEdit then begin
    ComponentClass := TMyTotallyAwesomeEdit;
  end;
end;

可能有很多其他方法可以做到这一点,但我就是这样做的!

There are likely numerous other ways to do this, but this is how I do it!

检查 TReader.GetFieldClass(Instance: TObject; const ClassName: string) 建议梅森回忆的黑客.第一行设置 ClassType := Instance.ClassType.所以我怀疑通过将 pas 文件中的声明从 Button1: TButton 更改为 Button1: MyUnit.TButton 将导致您的按钮被创建.或者也许黑客是将 MyUnit 添加到 uses 子句的最后,以便您的 TButton 版本是范围内的那个.然而,这些听起来都不是很实用.

Inspecting TReader.GetFieldClass(Instance: TObject; const ClassName: string) suggests the hack that Mason recalls. The first line sets ClassType := Instance.ClassType. So I suspect that by changing the declaration in the pas file from Button1: TButton to Button1: MyUnit.TButton will result in your button being created. Or perhaps the hack was to add MyUnit to the uses clause right at the end so that your version of TButton is the one that is in scope. However, none of this sounds very practical.

这篇关于替换delphi中的组件类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆