[ Pobierz całość w formacie PDF ]
.8 Continued}//---------------------------------------------------------------------------//PTypeInfo __fastcall TNonVCLTypeInfo::Bool(){return *(Typinfo::GetPropInfo(__typeinfo(TNonVCLTypesClass), BoolProperty ))->PropType;}//---------------------------------------------------------------------------//PTypeInfo __fastcall TNonVCLTypeInfo::AnsiString(){return *(Typinfo::GetPropInfo(__typeinfo(TNonVCLTypesClass), AnsiStringProperty ))->PropType;}//---------------------------------------------------------------------------//Using our previous example of registering a property editor for an unsigned int prop-erty called Size in a component called TTestComponent, the registration function isRegisterPropertyEditor(TNonVCLTypeInfo::UnsignedInt(),__classid(TTestComponent), Size ,__classid(TUnsignedProperty));The previous code is simple, easy to understand, and easy to write.This should beyour preferred method of registering property editors for non-VCL based properties.It was mentioned earlier that determining a TTypeInfo* for a non-VCL propertyimplemented in Object Pascal is not the same as one implemented in C++.Anexample of this is the PasswordChar property of TMaskEdit.To register a new propertyeditor for all char types requires two registrations: one for Object Pascal imple-mented properties and one for C++ implementations.The previous approach (aspecial class containing the appropriate non-VCL type properties) works fine for C++implementations, but to get the correct TTypeInfo* for the Object Pascal implementa-tions, the TTypeInfo* pointer must be determined directly from the VCL class, in thiscase from the PasswordChar property of TMaskEdit.This was the very first way we usedto obtain a TTypeInfo*.If we want to register a new char property editor calledTCharPropertyEditor for all components and all properties of type char, the registra-tions required are07 0672324806 CH05 12/12/02 2:42 PM Page 328328 CHAPTER 5 Creating Property and Component EditorsTPropInfo* VCLCharPropInfo = Typinfo::GetPropInfo(__typeinfo(TMaskEdit), PasswordChar );// Register the property editor for native VCL (Object Pascal) componentsRegisterPropertyEditor(*VCLCharPropInfo->PropType,0, ,__classid(TCharPropertyEditor));// Register the property editor for C++ implemented componentsRegisterPropertyEditor(TNonVCLTypeInfo::Char(),0, ,__classid(TCharPropertyEditor));Obtaining a TTypeInfo* (PTypeInfo) for a Non-VCL Type by ManualCreationCreating a TTypeInfo* manually is an alternative approach to obtaining a TTypeInfo*from a VCL class for a non-VCL type.It is shown largely for comparison purposesand also because it is a commonly used technique.However, it should generally beavoided in preference to the first method.Manually creating the required PTypeInfopointer can be done in place before the call to RegisterPropertyEditor(), or the codecan be placed in a function that will return the pointer.There are two ways to write the code to do this.One is to declare a static TTypeInfostructure locally, assign the appropriate values to it, and use a reference to it as thePTypeInfo argument.The other is to allocate a TTypeInfo structure dynamically, assignthe appropriate values, and then use the pointer as the PTypeInfo argument.Bothmethods for generating a suitable PTypeInfo for an AnsiString property are shown inListing 5.9.Note that this code and other similar functions are found in theGetTypeInfo unit on the CD-ROM.LISTING 5.9 Manually Creating a TTypeInfo*//---------------------------------------------------------------------------//// As Functions ////---------------------------------------------------------------------------//TTypeInfo* AnsiStringTypeInfo(void){static TTypeInfo TypeInfo;TypeInfo.Name = AnsiString ;07 0672324806 CH05 12/12/02 2:42 PM Page 329Registering Custom Property Editors 329LISTING 5.9 ContinuedTypeInfo.Kind = tkLString;return &TypeInfo;}// ORTTypeInfo* AnsiStringTypeInfo(void){TTypeInfo* TypeInfo = new TTypeInfo;TypeInfo->Name = AnsiString ;TypeInfo->Kind = tkLString;return TypeInfo;}//---------------- In the Registration code simply write:--------------------//RegisterPropertyEditor(AnsiStringTypeInfo(),0 , ,__classid(TAnsiStringPropertyEditor));//---------------------------------------------------------------------------//// In Place Before RegisterPropertyEditor() ////---------------------------------------------------------------------------//static TTypeInfo AnsiStringTypeInfo;TypeInfo.Name = AnsiString ;TypeInfo
[ Pobierz całość w formacie PDF ]