Mega Code Archive

 
Categories / Delphi / Examples
 

How to save the positions of the TCoolBar bands

Title: How to save the positions of the TCoolBar bands Question: Using the TCoolbar and TCoolband objects in an application is not that easy because there is no automatic way to store the layout (the positions) of this control.. but every "real" application has this request... Answer: - How to save the positions of the TCoolBar bands "Using the TCoolbar and TCoolband objects in an application is not that easy because there is no automatic way to store the layout (the positions) of this control.. but every "real" application has this request..." This question was mentioned in the 76th newsletter of delphi3000.com. Well, let's face the problem. What properties of TCoolBand objects should we store? Uh, no! First, let's check, if someone did this work already. We can change bands layout in design time and store it to the .dfm file, right?. So? So we do not need to write lots of code, Borland programmers did it for us. There are neat pieces of code in the help: function ComponentToString(Component: TComponent): string; var BinStream:TMemoryStream; StrStream: TStringStream; s: string; begin BinStream := TMemoryStream.Create; try StrStream := TStringStream.Create(s); try BinStream.WriteComponent(Component); BinStream.Seek(0, soFromBeginning); ObjectBinaryToText(BinStream, StrStream); StrStream.Seek(0, soFromBeginning); Result:= StrStream.DataString; finally StrStream.Free; end; finally BinStream.Free end; end; (it's from example for ObjectBinaryToText procedure, I just knew it before) Now we need TCoolBar, couple TToolBar and TMemo. Memo1.Lines.Text := ComponentToString(CoolBar1); object CoolBar1: TCoolBar Left = 0 Top = 0 Width = 688 Height = 30 AutoSize = True Bands = item Break = False Control = Form1.ToolBar2 ImageIndex = -1 MinHeight = 26 Width = 139 end item Break = False Control = Form1.ToolBar1 ImageIndex = -1 Width = 543 end end Looks quite understandable. Binary file will be less meaningful, but there is almost nothing to code. procedure Store(aCoolbar: TCoolbar; aFilename: string); var Stream: TFileStream; begin try Stream := TFileStream.Create(aFilename, fmCreate); Stream.WriteComponent(aCoolbar); finally Stream.Free; end; end; procedure Restore(aCoolbar: TCoolbar; aFilename: string); var Stream: TFileStream; begin try Stream := TFileStream.Create(aFilename, fmOpenRead); Stream.ReadComponent(aCoolbar); finally Stream.Free; end; end;