Delphi subclass visual component and use it

后端 未结 3 558
情深已故
情深已故 2020-12-03 16:05

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

3条回答
  •  情深已故
    2020-12-03 16:23

    To change a component on the existing form, it has to actually be a component that the IDE can create an instance of at design-time. This means the IDE has to be aware of it first, of course.

    The way to do this is to create your own descendant component, and actually install it into the IDE in a design-time package. You can then drop it on your form instead of the standard version, or replace it on existing forms with a little work. (You do have to create your version and install it first, though.)

    Start with File->New->Package (Delphi) from the IDE's menu. Save the package as you would any other project (for instance, MyComponents.dpk).

    Now use File->New->Other->Delphi Files, and double-click Component in the right pane. The New Component wizard will start, where you can choose the existing component you want to descend from (or design a new one).

    Follow the steps of the wizard, and you'll end up with the basic shell of your component:

    unit MyToolBar1;
    
    interface
    
    uses
      System.SysUtils, System.Classes, Vcl.Controls, Vcl.ToolWin, Vcl.ComCtrls;
    
    type
      TMyToolBar = class(TToolBar)
      private
        { Private declarations }
      protected
        { Protected declarations }
      public
        { Public declarations }
      published
        { Published declarations }
      end;
    
    procedure Register;
    
    implementation
    
    procedure Register;
    begin
      RegisterComponents('Samples', [TMyToolBar]);
    end;
    
    end.
    

    Implement whatever functionality you want in the new descendant, and then save the file.

    Right-click on the package in the Project Manager (by default the upper right window in the IDE), and choose Install from the context menu. This will compile and build the package, and automatically install it in the IDE. (The example I've shown would put the new component on the Samples page in the palette based on what's indicated in the RegisterComponents call.)

    After doing the above, you can change an existing form (make a backup of the .pas and .dfm files first!). I'll use the TToolBar you mentioned, and the sample replacement I've posted the shell for in the instructions below.

    Manually change the classname in the source code editor from TToolBar to TMyToolBar.

    Right-click on the form, and choose View as Text from the context menu.

    Find the TToolBar, and change it from TToolBar to TMyToolBar.

    Right-click again, and choose View as Form from the context menu. If you've done these steps correctly, clicking on the toolbar should show you TMyToolBar in the Object Inspector. If you don't see it (or if you get error messages) you've done something wrong; you can close the tab by right-clicking it at the top of the Code Editor and choosing Close tab, and answer "No" to the prompt about saving changes, and then if necessary restore from the backup copies I told you to make first.

提交回复
热议问题