Saving and Restoring TCoolbar bands positions

Here's a way how to save & restore TCoolbar bands positions in Delphi5.

Saving Positions:

Of each band in the coolbar, write the name of the control it owns, the
break value, the width of the coolband as well as the width of the control
that's placed on it in a place where you can find it back later.

procedure WriteCoolbarState(item: TCoolBar);
var
   r: Integer
begin
    for r:=0 to item.Bands.Count-1 do
    begin

SaveStringSomewhere(item.Name+'Control'+intToStr(r),item.Bands[r].Control.Na
me);

SaveBooleanSomewhere(item.Name+'Break'+intToStr(r),item.Bands[r].Break);

SaveIntegerSomewhere(item.Name+'ControlWidth'+intToStr(r),item.Bands[r].Cont
rol.Width);

SaveIntegerSomewhere(item.Name+'Width'+intToStr(r),item.Bands[r].Width);
    end;
end;

Restoring Positions:

Of all bands in the the coolbar, read first back the Index properties.  Now
of all bands read back the rest of the properties (Break, width and width of
the TWinControl that's placed on it). Note that you cannot rely on the Index
property for finding back a coolband because this property changes at
runtime according to its position. Instead find back the cool band in
another way, for instance by finding back the TWinControl that's placed on
it like in this example.

procedure ReadCoolbarState(item: TCoolBar);
var
    r,n,v: Integer;
    b: Boolean;
    str: String;
begin
    Result:=FALSE;

    item.Bands.BeginUpdate;

    for r:=0 to item.Bands.Count-1 do
    begin
        if (ReadStringBack(item.Name+'Control'+intToStr(r),str)=TRUE) then
        begin
            for n:=0 to item.Bands.Count-1 do
            begin
                if (item.Bands[n].Control.Name=str) then
                begin
                    item.Bands[n].Index:=r;
                    Break;
                end;
            end;
        end;
    end;

    for r:=0 to item.Bands.Count-1 do
    begin
        if (ReadStringBack(item.Name+'Control'+intToStr(r),str)=TRUE) then
        begin
            for n:=0 to item.Bands.Count-1 do
            begin
                if (item.Bands[n].Control.Name=str) then
                begin
                    if
(ReadIntegerBack(item.Name+'ControlWidth'+intToStr(r),v)=TRUE) then
                        item.Bands[n].Control.Width:=v;
                    if
(ReadIntegerBack(item.Name+'Width'+intToStr(r),v)=TRUE) then
                        item.Bands[n].Width:=v;
                    if
(ReadBooleanBack(item.Name+'Break'+intToStr(r),b)=TRUE) then
                        item.Bands[n].Break:=b;
                    Break;
                end;
            end;
        end;
    end;

    item.Bands.EndUpdate;
end;

Now you still need to provide your own SaveDataSomewhere and ReadDataBack
functions for saving and restoring your data.

function ReadStringBack(identifier: String; var rst: String): Boolean;
function ReadIntegerBack(identifier: String; var rst: Integer): Boolean;
function ReadBooleanBack(identifier: String; var rst: Boolean): Boolean;

function SaveStringSomewhere(identifier: String;val: String): Boolean;
function SaveIntegerSomewhere(identifier: String;val: Integer): Boolean;
function SaveBooleanSomewhere(identifier: String;val: Boolean): Boolean;

Greetings,

Daniel Terhell
Resplendence Sp
dan...@resplendence.com
http://www.resplendence.com