Using Class References in C


Using Class References in C++

To build the VCL drag and drop application using C++ and class references, you first need to register the classes with a call to RegisterClasses. To acquire a class reference in C++, use the reserved word __classid:

__classid(class)

Since the RegisterClasses procedure registers an array of classes, it's best to create an array of classes and then pass the array to the procedure. Besides passing the array, you also need to pass the index of the last class in the array (class count – 1):

__fastcall TMainForm::TMainForm(TComponent* Owner)   : TForm(Owner) {    TComponentClass classes[9] = {       __classid(TLabel), __classid(TComboBox), __classid(TButton),       __classid(TMemo), __classid(TCheckBox), __classid(TListBox),       __classid(TRadioButton), __classid(TPanel), __classid(TEdit),    };    // the second parameter is the index of the last array item    RegisterClasses(classes, 8); } 

The second thing that has to be done is to write the GetControlCount function to calculate how many instances of a class already exist on the form. The function should accept a class reference, as it does in Delphi.

To determine the class type in Delphi, you can use the is operator. In C++, you have to call the ClassType() method, which returns the class reference for the object:

int __fastcall TMainForm::GetControlCount(TControlClass AClass) {    int cnt = 0;    for(int i = 0; i < ControlCount; i++)    {       // use ClassType() to get the class reference and       // then compare the two class references       if(Controls[i]->ClassType() == AClass)         cnt++;    }    return cnt; }

Instantiating Class References

Since there is no easy way to instantiate a class through a class reference in C++, we need to create a Delphi function that will do that for us and then simply add the Delphi unit to the C++ project.

This function should accept a control reference (TControlClass type), because we are only going to use it to create controls. It should also accept an owner parameter.

Here's the Delphi MetaUnit unit with the necessary function:

unit MetaUnit; interface uses Classes, Controls; function CreateFromReference(AClassReference: TControlClass;   AOwner: TControl): TControl; implementation function CreateFromReference(AClassReference: TControlClass;   AOwner: TControl): TControl; begin   Result := AClassReference.Create(AOwner); end; end. 

When you create the MetaUnit unit, add it to your C++ project and include it as image from book MetaUnit.hpp; the compiler will generate the necessary header file from the Delphi unit:

// Borland C++ Builder // Copyright (c) 1995, 2005 by Borland Software Corporation // All rights reserved // (DO NOT EDIT: machine generated header) 'Metaunit.pas' rev: 6.00 ... namespace Metaunit { extern PACKAGE Controls::TControl*    __fastcall CreateFromReference(TMetaClass* AClassReference,    Controls::TControl* AOwner); }   /* namespace Metaunit */ using namespace Metaunit;

Implementing Drag and Drop

Now that the biggest problem (instantiating class references) is solved, you can easily implement the same drag and drop behavior as you did in Delphi. The only difference is that instead of directly instantiating the class reference you call the CreateFromReference function to do the job.

Here's the entire OnDragDrop event handler:

void __fastcall TMainForm::FormDragDrop(TObject *Sender,    TObject *Source, int X, int Y) {    TControl* newControl;    TControlClass selClass;    AnsiString ctlName;    AnsiString selString;    selString = TypeList->Items->Strings[TypeList->ItemIndex];    selClass = GetClass(selString);    // use the CreateFromReference function from MetaUnit.hpp    newControl = CreateFromReference(selClass, this);    newControl->Parent = this;    ctlName = AnsiString(newControl->ClassName()) +       IntToStr(GetControlCount(selClass));    ctlName.Delete(1, 1); // remove "T" from the name    newControl->Name = ctlName;    newControl->Left = X;    newControl->Top = Y; }



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net