在delphi中替换一个组件类 [英] Replacing a component class in delphi

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

问题描述

我知道我已经看到一个例子,在一个黑客的地方定义一个现有的VCL组件的定制版本,如TButton或TEdit,具有相同的类名称,并做一些事情,使DFM流光镜将实例化您的版本而不是原来的。不幸的是,我处于这样的情况,我需要能够做到这一点,我找不到写作。有没有人知道如何找到有关如何实现这一点的信息?

解决方案

在你的表单中,你可以覆盖 ReadState 方法如下:

 键入
TMyForm =类(TForm)
protected
procedure ReadState(Reader:TReader);覆盖
结束

程序TMyForm.ReadState(Reader:TReader);
begin
Reader.OnFindComponentClass:= FindComponentClass;
继承;
结束

程序TMyForm.FindComponentClass(Reader:TReader; const ClassName:string;
var ComponentClass:TComponentClass);
begin
如果ComponentClass = TButton然后开始
ComponentClass:= TMySuperDuperButton;
end else如果ComponentClass = TEdit然后开始
ComponentClass:= TMyTotallyAwesomeEdit;
结束
结束

有很多其他方法可以做到这一点,但这是我如何做的! >

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


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?

解决方案

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!

EDIT: 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天全站免登陆