Title: This works: Save and Restore Coolbars
Question: How can we save and restore Coolbars?
Answer:
{
We need to define an array which holds all settings.
In our example we use a Coolbar with 4 Toolbars.
If you have more or less Toolbars, you need to adjust
LastIndex in the following line:
}
const
LastIndex = 3; // First index is 0!
// All the necessary data for our CoolBar with some ToolBars:
// Writing this data to the Registry as binary data is trivial
// and therefore left as an excercise to the reader.
// Writing configuration data to a file is deprecated
// in current Windows versions, btw.
CBData: record
position, width, flags: array[0..LastIndex] of integer;
end;
procedure TForm1.SaveCBStatus;
var
BandInfo: TReBarBandInfo;
n, i: integer;
TB: TToolBar;
begin
with CBData do
try
// Positions:
for i := 0 to LastIndex do
begin
TB := Coolbar1.controls[i] as TToolBar;
position[i] := 0;
while (TB Coolbar1.bands[position[i]].control)
and (position[i] inc(position[i]);
end;
// New row or hidden? (Flags)
for n := 0 to LastIndex do
begin
FillChar(BandInfo, SizeOf(TReBarBandInfo), 0);
BandInfo.cbSize := SizeOf(TReBarBandInfo);
BandInfo.fMask := RBBIM_STYLE or RBBIM_ID;
Coolbar1.Perform(RB_GETBANDINFO, n, Integer(@BandInfo));
flags[n] := BandInfo.fStyle;
// Width:
width[n] := CoolBar1.Bands[n].width;
end;
except
ShowMessage('Exception in SaveCBStatus');
end;
end;
procedure TForm1.RestoreCBStatus;
var
p: array[0..LastIndex] of TCoolband;
co: array[0..LastIndex] of integer; // current order
n, i: integer;
BandInfo: TReBarBandInfo;
TB: TToolBar;
begin
with CBData do
try
// remember current order:
for i := 0 to LastIndex do
begin
TB := Coolbar1.controls[i] as TToolBar;
co[i] := 0;
while (TB Coolbar1.bands[co[i]].control)
and (co[i] inc(co[i]);
end;
// replace with old order:
with Coolbar1 do
begin
for n := 0 to LastIndex do
p[n] := bands[co[n]];
// add as new ToolBars:
for n := 0 to LastIndex do
bands.insert(0);
for n := 0 to LastIndex do
bands.items[position[n]].assign(p[n]);
// remove original ToolBars:
for n := LastIndex+1 to LastIndex+LastIndex+1 do
bands.delete(LastIndex+1);
end;
// restore width and style:
for n := Coolbar1.Bands.Count - 1 downto 0 do
begin
FillChar(BandInfo, SizeOf(TReBarBandInfo), 0);
BandInfo.cbSize := SizeOf(TReBarBandInfo);
BandInfo.fMask := RBBIM_SIZE or RBBIM_CHILD or RBBIM_STYLE;
Coolbar1.Perform(RB_GETBANDINFO, n, Integer(@BandInfo));
BandInfo.cx := width[n];
BandInfo.fStyle := flags[n];
Coolbar1.Perform(RB_SETBANDInfo, n, Integer(@BandInfo));
end;
except
ShowMessage('Exception in RestoreCBStatus');
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin // Save the settings into CBData
SaveCBStatus;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin // Restore the settings from CBData
RestoreCBStatus;
end;