RAD Studio |  |

Creating and Registering the Graphic Component

You create every component in the same way: create a unit, derive a component class, register it, compile it, and install it on the Tool palette. This process is outlined in Creating a new component.

For this example, follow the general procedure for creating a component, with these specifics:

1. Call the component's unit Shapes.
2. Derive a new component type called TSampleShape, descended from TGraphicControl.
3. Register TSampleShape on the Samples category of the Tool palette.

The resulting unit should look like this:
Copy Code

    unit Shapes;
    interface
    uses SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms;
    type
      TSampleShape = class(TGraphicControl)
      end;
    procedure Register;
    implementation
    procedure Register;
    begin
      RegisterComponent('Samples', [TSampleShape]);
    end;
    end.

Copy Code

    //---------------------------------------------------------------------------
    #include <vcl.h>
    #pragma hdrstop
    #include "Shapes.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init);
    //---------------------------------------------------------------------------
    // ValidCtrCheck is used to assure that the components created do not have
    // any pure virtual functions.
    //
    static inline void ValidCtrCheck(TSampleShape *)
    {
      new TSampleShape(NULL);
    }
    //---------------------------------------------------------------------------
    __fastcall TSampleShape::TGraphicControl(TComponent* Owner)
    : TGraphicControl(Owner)
    {
    }
    //---------------------------------------------------------------------------
    namespace Shapes
    {
      void __fastcall PACKAGE Register()
      {
        TComponentClass classes[1] = {__classid(TSampleShape)};
        RegisterComponents("Samples", classes, 0);
      }
    }

Copy Code

    //---------------------------------------------------------------------------
    #ifndef ShapesH
    #define ShapesH
    //---------------------------------------------------------------------------
    #include <sysutils.hpp>
    #include <controls.hpp>
    #include <classes.hpp>
    #include <forms.hpp>
    //---------------------------------------------------------------------------
    class PACKAGE TSampleShape : public TGraphicControl
    {
    private:
    protected:
    public:
    __published:
    };
    //---------------------------------------------------------------------------
    #endif

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!