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

查看:23
本文介绍了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?

将 TToolBar 添加到 TForm 后,似乎无法更改类.我尝试通过 Object Inspector 以及 .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. 在与 TForm 类相同的单元中声明自定义 TToolBar 类:

  1. declare the custom TToolBar class in the same unit as your TForm class:

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 单元的 uses 子句之后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天全站免登陆