[ Pobierz całość w formacie PDF ]
.If there is any doubt, assign a value in the constructormethod.The following code shows a component declaration that specifies a default value forthe Align property and the implementation of the component s constructor that setsthe default value.In this case, the new component is a special case of the standardpanel component that will be used for status bars in a window, so its defaultalignment should be to the bottom of its owner.class PACKAGE TMyStatusBar : public TPanel{public:virtual __fastcall TMyStatusBar(TComponent* AOwner);__published:__property Align = {default=alBottom};};The constructor of the TMyStatusBar component is in the.CPP file:__fastcall TMyStatusBar::TMyStatusBar (TComponent* AOwner): TPanel(AOwner){Align = alBottom;}Determining what to storeYou can control whether C++Builder stores each of your components properties.Bydefault, all properties in the published part of the class declaration are stored.Youcan choose not to store a given property at all, or you can designate a function thatdetermines at runtime whether to store the property.To control whether C++Builder stores a property,1 Add an equal sign (=) after the property name.2 After the equal sign, add braces({}).3 Within the braces, type the keyword stored, followed by true, false, or the name ofa Boolean method.The following code shows a component that declares three new properties.One isalways stored, one is never stored, and the third is stored depending on the value of aBoolean method:class PACKAGE TSampleComponent : public TComponent{protected:bool __fastcall StoreIt();public:’__published:__property int Important = {stored=true}; // always stored__property int Unimportant = {stored=false}; // never stored__property int Sometimes = {stored=StoreIt}; // storage depends on function value};Cr eat i ng pr oper t i es 41-11 St o r i n g a n d l o a d i n g p r o p e r t i e sInitializing after loadingAfter a component reads all its property values from its stored description, it calls avirtual method named Loaded, which performs any required initializations.The callto Loaded occurs before the form and its controls are shown, so you do not need toworry about initialization causing flicker on the screen.To initialize a component after it loads its property values, override the Loadedmethod.Note The first thing to do in any Loaded method is call the inherited Loaded method.Thisensures that any inherited properties are correctly initialized before you initializeyour own component.Storing and loading unpublished propertiesBy default, only published properties are loaded and saved with a component.However, it is possible to load and save unpublished properties.This allows you tohave persistent properties that do not appear in the Object Inspector.It also allowscomponents to store and load property values that C++Builder does not know howto read or write because the value of the property is too complex.For example, theTStrings object can t rely on C++Builder s automatic behavior to store and load thestrings it represents and must use the following mechanism.You can save unpublished properties by adding code that tells C++Builder how toload and save your property s value.To write your own code to load and save properties, use the following steps:1 Create methods to store and load the property value.2 Override the DefineProperties method, passing those methods to a filer object.Creating methods to store and load property valuesTo store and load unpublished properties, you must first create a method to storeyour property value and another to load your property value.You have two choices:" Create a method of type TWriterProc to store your property value and a method oftype TReaderProc to load your property value.This approach lets you takeadvantage of C++Builder s built-in capabilities for saving and loading simpletypes.If your property value is built out of types that C++Builder knows how tosave and load, use this approach." Create two methods of type TStreamProc, one to store and one to load yourproperty s value.TStreamProc takes a stream as an argument, and you can use thestream s methods to write and read your property values.41-12 Dev el oper  s Gui de St o r i n g a n d l o a d i n g p r o p e r t i e sFor example, consider a property that represents a component that is created atruntime.C++Builder knows how to write this value, but does not do soautomatically because the component is not created in the form designer.Because thestreaming system can already load and save components, you can use the firstapproach.The following methods load and store the dynamically created componentthat is the value of a property named MyCompProperty:void __fastcall TSampleComponent::LoadCompProperty(TReader *Reader){if (Reader->ReadBoolean())MyCompProperty = Reader->ReadComponent(NULL);}void __fastcall TSampleComponent::StoreCompProperty(TWriter *Writer){if (MyCompProperty){Writer->WriteBoolean(true);Writer->WriteComponent(MyCompProperty);}elseWriter->WriteBoolean(false);}Overriding the DefineProperties methodOnce you have created methods to store and load your property value, you canoverride the component s DefineProperties method.C++Builder calls this methodwhen it loads or stores the component.In the DefineProperties method, you must callthe DefineProperty method or the DefineBinaryProperty method of the current filer,passing it the method to use for loading or saving your property value.If your loadand store methods are of type TWriterProc and type TReaderProc, then you call thefiler s DefineProperty method.If you created methods of type TStreamProc, callDefineBinaryProperty instead.No matter which method you use to define the property, you pass it the methods thatstore and load your property value as well as a boolean value indicating whether theproperty value needs to be written.If the value can be inherited or has a defaultvalue, you do not need to write it.For example, given the LoadCompProperty method of type TReaderProc and theStoreCompProperty method of type TWriterProc, you would override DefinePropertiesas follows:void __fastcall TSampleComponent::DefineProperties(TFiler *Filer){// before we do anything, let the base class define its properties [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • hanula1950.keep.pl