[ Pobierz całość w formacie PDF ]
.Mo¿na by s¹­dziæ, ¿e jej opublikowanie by³oby rozs¹dniejsze, gdy¿ rozwi¹za³obywszystkie problemy ze strumieniowaniem przycisku.To prawda, lecz pojawia siêpewien problem: otó¿ strumieniowanie przebiega nieco inaczej w stosunku dokomponentów (przycisk), ni¿ w stosunku do innych pochodnych klasy TPersistent(element kolekcji) — odczyt komponentu ze strumienia po³¹czony jest ztworzeniem jego egzemplarza, natomiast dla innych klas wywodz¹cych siê zTPersistent odczytywana zawartoœæ wpisywana jest do istniej¹cego egzemplarza.Wzwi¹zku z tym poszczególne przyciski TddgRunButton nie s¹ przechowywane wstrumieniu, lecz dynamicznie tworzone przez konstruktory elementówTRunBtnItem.TRunButtons — kolekcja komponentuKolejnym krokiem jest zdefiniowanie, na bazie klasy TCollection, nowej kolekcjiprzechowuj¹cej obiekty TRunBtnItem.Kolekcjê tê nazwaliœmy TRunButtons:typeTRunButtons = class(TCollection)privateFLaunchPad: TLaunchPad; // wskazanie na w³aœcicielafunction GetItem(Index: Integer): TRunBtnItem;procedure SetItem(Index: Integer; Value: TRunBtnItem);protectedprocedure Update(Item: TCollectionItem); override;publicconstructor Create(LaunchPad: TLaunchPad);function Add: TRunBtnItem;procedure UpdateRunButtons;property Items[Index: Integer]: TRunBtnItem readGetItem write SetItem; default;end;Zwróæ uwagê na specyficzne w³aœciwoœci i metody zwi¹zane z obs³ug¹ elementówko­lekcji TRunBtnItem; stanowi¹ one jej w³aœciwoœæ tablicow¹.Kolekcja zawieraponadto wskazanie na przedmiotowy pasek przycisków (FLaunchPad).Bli¿szezwi¹zki pomiê­dzy paskiem, kolekcj¹ i jej elementami stan¹ siê bardziejzrozumia³e, gdy przyjrzymy siê ich szczegó³om implementacyjnym.Implementacja — wspó³praca kolekcji i jej elementów z komponentem macierzystymTworzenie, zwalnianie i zapis w strumieniu kolekcji, jej elementów i przyciskówpoleceñ ilustruje kod przedstawiony na wydruku 12.10.Wydruk 12.10.Implementacja komponentu TddgLaunchPadunit LnchPad;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls,Forms, Dialogs, RunBtn, ExtCtrls;typeTddgLaunchPad = class;TRunBtnItem = class(TCollectionItem)privateFCommandLine: string; // PolecenieFLeft: Integer; // Pozycja przyciskuFTop: Integer; //FRunButton: TddgRunButton; // Reference to a TddgRunButtonFWidth: Integer; // Wskazanie na przyciskFHeight: Integer;procedure SetCommandLine(const Value: string);procedure SetLeft(Value: Integer);procedure SetTop(Value: Integer);publicconstructor Create(Collection: TCollection); override;destructor Destroy; override;procedure Assign(Source: TPersistent); override;property Width: Integer read FWidth;property Height: Integer read FHeight;published{ W³aœciwoœci publikowane, podlegaj¹ce strumieniowaniu }property CommandLine: String read FCommandLinewrite SetCommandLine;property Left: Integer read FLeft write SetLeft;property Top: Integer read FTop write SetTop;end;TRunButtons = class(TCollection)privateFLaunchPad: TddgLaunchPad; // Wskazanie na komponent-w³aœcicielafunction GetItem(Index: Integer): TRunBtnItem;procedure SetItem(Index: Integer; Value: TRunBtnItem);protectedprocedure Update(Item: TCollectionItem); override;publicconstructor Create(LaunchPad: TddgLaunchPad);function Add: TRunBtnItem;procedure UpdateRunButtons;property Items[Index: Integer]: TRunBtnItem readGetItem write SetItem; default;end;TddgLaunchPad = class(TScrollBox)privateFRunButtons: TRunButtons;TopAlign: Integer;LeftAlign: Integer;procedure SetRunButtons(Value: TRunButtons);procedure UpdateRunButton(Index: Integer);publicconstructor Create(AOwner: TComponent); override;destructor Destroy; override;procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;publishedproperty RunButtons: TRunButtons read FRunButtons write SetRunButtons;end;implementation{ TRunBtnItem }constructor TRunBtnItem.Create(Collection: TCollection);{ parametr Collection reprezentuje kolekcjê bêd¹c¹ w³aœcicielem tworzonegoelementu}begininherited Create(Collection);{ Tworzenie i inicjacja egzemplarza przycisku orazdeklaracja paska jako w³aœciciela przycisku}FRunButton := TddgRunButton.Create(TRunButtons(Collection).FLaunchPad);FRunButton.Parent := TRunButtons(Collection).FLaunchPad;FWidth := FRunButton.Width; // szerokoœæ i wysokoœæ przyciskuFHeight := FRunButton.Height; //end;destructor TRunBtnItem.Destroy;beginFRunButton.Free; // zwolnij obiekt przyciskuinherited Destroy; //end;procedure TRunBtnItem.Assign(Source: TPersistent);{Niniejsza metoda realizuje kopiowanie charakterystycznedla klasy TRunBtnItem.Je¿eli element jest innej klasy,realizowane jest standardowe kopiowanie (inherited Assign)}beginif Source is TRunBtnItem thenbegin{ £añcuchy znaków (do których zalicza siê CommandLine) s¹ w DelphiwskaŸnikami, standardowa Assign() spowodowa³aby wiêc powieleniewskazania na istniej¹cy ³añcuch; aby tego unikn¹æ, dokonujemyjawnego kopiowania ³añcucha - czyni to metoda dostêpowaw³aœciwoœci CommandLine}CommandLine := TRunBtnItem(Source).CommandLine;{ Skopiowanie pozosta³ych pól }FLeft := TRunBtnItem(Source).Left;FTop := TRunBtnItem(Source).Top;endelseinherited Assign(Source);end;procedure TRunBtnItem.SetCommandLine(const Value: string);{ This is the write accessor method for TRunBtnItem.CommandLine [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • coubeatki.htw.pl