Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

overriding - Delphi subclass visual component and use it

I would like to subclass TToolBar with another class called MyTToolBar so that I can override a method. I'm new to Delphi, 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.

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.

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

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...

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

  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.
    
  2. 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.
    

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...