Delphi子类的可视化组件并使用它 [英] Delphi subclass visual component and use it

查看:122
本文介绍了Delphi子类的可视化组件并使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用另一个类MyTToolBar将TToolBar子类化,以便我可以覆盖一个方法。我是Delphi的新手,但是经过两个小时的尝试,我无法使用MyTToolBar来代替TToolBar。我不能成为第一个想要覆盖视觉组件类的方法的人。

I would like to subclass TToolBar with another class called MyTToolBar so that I can override a method. I'm new to Delp but after two hours of trying various methods, I can't get MyTToolBar to be used instead of TToolBar. I can't be the first person who's wanted to override a method on a visual component class.

我从Xcode背景中更多地介绍了一个可视化组件的子类化。您创建一个父类(例如,MySuperClass)的子类(例如,MySubclass),然后只需简单地分配Xcode的Interface Builder视图中的子类。该子类被自动识别和使用。

I come more from an Xcode background where subclassing a visual component is easy. You create a subclass (e.g., MySubclass) of a parent class (e.g., MySuperClass) and then just simply assign the subclass in the Interface Builder view of Xcode. The subclass is automatically recognized and used.

为什么我不能在Delphi RAD Studio XE3中这样做?

Why can't I seem to do this in Delphi RAD Studio XE3?

将TFoolBar添加到TForm后,似乎无法更改类。我尝试通过对象检查器以及通过.PAS源代码文件。如果我更改.PAS文件中的类,我收到一条错误消息,说工具栏应该是类型为Vcl.ComCtrls.TToolBar,但是被声明为MyTToolbar。更正声明?这只是看起来很傻...

After adding a TToolBar to a TForm, it doesn't seem possible to change the class. I tried through the Object Inspector as well as through the .PAS source code file. If I change the class in the .PAS file, I get an error message saying the toolbar "should be of type Vcl.ComCtrls.TToolBar but is declared as MyTToolbar. Correct the declaration?" This just seems silly...

哦,我也使用新的组件向导选择:文件 - >新建 - >其他 - > Delphi项目 - > Delphi文件 - >组件。我选择MyTToolBar的祖先作为TToolBar,并告诉它在样本调色板页面中注册。但是,它不会出现在样本页面中。

Oh, and I've also used the new component wizard from selecting: File -> New -> Other -> Delphi Projects -> Delphi Files -> Component. I select the ancestor for MyTToolBar as TToolBar and tell it to register in the 'Samples' palette page. However, it doesn't appear in the 'Samples' page.

推荐答案

与XCode方法最接近的方法是使用Delphi中的插件类。基本上,您不要更改IDE为标准的 TToolBar 用法创建的代码。而是声明一个从标准的 TToolBar 组件派生的新类,但也被命名为 TToolBar ,并使您可以看到编译器标准 TToolBar 已被声明。无论哪个 TToolBar 类最后一次被编译器看到将是每当 TForm DFM被流式化时实例化的实际类类型

The closest equivilent to your XCode approach is to use an "interposer" class in Delphi. Basically, you do not change the code that the IDE creates for the standard TToolBar usage. You instead declare a new class that derives from the standard TToolBar component but is also named TToolBar and you make it visible to the compiler after the standard TToolBar has been declared. Whichever TToolBar class is last seen by the compiler will be the actual class type that gets instantiated whenever the TForm DFM is streamed.

您可以在标准之后,编译器看到您的自定义 TToolBar TToolBar 通过两种不同的方式之一:

You can make your custom TToolBar class be seen by the compiler after the standard TToolBar class by one of two different ways:


  1. 声明自定义 TToolBar 类与您的 TForm相同的单元类类:

unit MyForm;

interface

uses
  ..., Vcl.ComCtrls, ...;

type
  TToolBar = class(Vcl.ComCtrls.TToolBar)
    // override what you need...
  end;

  TMyForm = class(TForm)
    ToolBar1: TToolBar; // <-- do not change this!
    ...
  end;

implementation

// implement custom TToolBar as needed...

// normal TForm implementation code as needed ...

end.


  • 您可以声明自定义 TToolBar 在自己的单元中的类,然后添加到 TForm 单元的之后使用已添加ComCtrls 单元:

  • you can declare the custom TToolBar class in its own unit that is then added to the TForm unit's uses clause after the ComCtrls unit has been added:

    unit MyToolBar;
    
    interface
    
    uses
      ..., Vcl.ComCtrls;
    
    type
      TToolBar = class(Vcl.ComCtrls.TToolBar)
        // override what you need...
      end;
    
    implementation
    
    // implement custom TToolBar as needed...
    
    end.
    

    unit MyForm;
    
    interface
    
    uses
      ..., Vcl.ComCtrls, ..., MyToolBar;
    
    type
      TMyForm = class(TForm)
        ToolBar1: TToolBar; // <- do not change this!
        ...
      end;
    
    implementation
    
    // normal TForm implementation code as needed ...
    
    end.
    


  • 此方法适用于每个项目只要。如果要在多个项目中使用自定义的 TToolBar 类,那么最好将其安装到IDE中,像@KenWhite所描述的那样,而不是标准的 TToolBar 。返回命名它 TMyToolBar (或任何),不要将它命名为 TToolBar ,因为它不会用作插值器。确保软件包在其项目选项中标记为运行时和设计时间(创建单独的运行时和设计时间包)不在本讨论范围之内)。 TMyToolBar 将在设计时可用,您可以像任何其他组件一样放在 TForm 上。如果没有,那么你没有正确设置。

    This approach works on a per-project basis only. If you want to use your custom TToolBar class in multiple projects, then you are better off installing it into the IDE, like @KenWhite describes, and use it instead of the standard TToolBar. Go back to naming it TMyToolBar (or whatever), do not name it TToolBar anymore since it is not going to be used as an interposer. Make sure the Package is marked as "Runtime and Designtime" in its Project Options (creating separate runtime-only and designtime-ony Packages is outside the scope of this discussion). TMyToolBar will be available at design-time for you to drop on your TForm like any other component. If it is not, then you did not set it up correctly.

    这篇关于Delphi子类的可视化组件并使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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