Mit der ActionList liegst du schon richtig.
Sagen wir du hast eine Ini-Datei, die so aussieht:
Code:
[TForm1.MenuItemX]
0 = ActOpenFile
1 = ActSave
2 = ActPrint
3 = "-"
4 = ActExit
Du könntest dir dann eine Klasse schreiben, die diese Ini-Datei auswertet und für jeden Eintrag dynamisch ein Menuitem erzeugt und mit der angegebenen Action verbindet.
Ich habe mir mal so eine Klasse programmiert; allerdings für Toolbars und nicht für Menues:
Delphi-Quellcode:
// Klasse zum Laden und Speichern von Konfigurationen in einer Ini-Datei
TActionListConfig = class(TObject)
private
FIni : TCustomIniFile;
class function GetIniSectionname(AToolBar: TToolBar):string;
public
constructor Create(ini:TCustomIniFile);
procedure SaveConfiguration(AToolBar: TToolBar; AActionList: TActionList; const AIniSection: string);
procedure LoadConfiguration(AToolBar: TToolBar; AActionList: TActionList; const AIniSection: string);
end;
...
constructor TActionListConfig.Create(ini: TCustomIniFile);
begin
inherited Create;
FIni := ini;
end;
class function TActionListConfig.GetIniSectionname(AToolBar: TToolBar): string;
begin
Result := AToolBar.Owner.ClassName + '_' + AToolBar.Name;
end;
procedure TActionListConfig.LoadConfiguration(AToolBar: TToolBar;
AActionList: TActionList; const AIniSection: string);
var
i: Integer;
s, section : string;
a: TBasicAction;
ActionName,ActionHotKey: string;
sc: TShortCut;
begin
Assert(Assigned(AToolBar) and Assigned(AActionList) and (AToolBar.Images = AActionList.Images));
if AIniSection <> '' then
section := AIniSection
else
section := GetIniSectionname(AToolBar);
if not FIni.SectionExists(section) then
Exit;
Toolbar_DeleteAllButtons(AToolBar);
for i := AActionList.ActionCount downto 0 do
begin
s := FIni.ReadString(section,IntToStr(i),'');
if s = '' then
Continue;
ActionHotKey := s;
ActionName := StrToken(ActionHotKey,',');
a := ActionList_FindActionByName(AActionList,ActionName);
if Assigned(a) and (a is TCustomAction) and (Pos(',',s) > 0) then
begin
if ActionHotKey = '' then
sc := 0
else
sc := TextToShortCut(StrRemoveChars(ActionHotKey,[' '] ));
TCustomAction(a).ShortCut := sc;
end;
Toolbar_AddButton(AToolBar,a);
end;
end;
procedure TActionListConfig.SaveConfiguration(AToolBar: TToolBar;
AActionList: TActionList; const AIniSection: string);
var
i: Integer;
s, section : string;
tb : TToolButton;
begin
Assert(Assigned(AToolBar) and Assigned(AActionList) and (AToolBar.Images = AActionList.Images));
if AIniSection <> '' then
section := AIniSection
else
section := GetIniSectionname(AToolBar);
FIni.EraseSection(section);
for i := 0 to AToolBar.ButtonCount-1 do
begin
tb := AToolBar.Buttons[i];
if tb.Style = tbsSeparator then
s := CAPTION_SEPARATOR
else
begin
s := tb.Action.Name;
if tb.Action is TCustomAction then
begin
with TCustomAction(tb.Action) do
begin
if ShortCut <> 0 then
begin
s := s + ',' + ShortCutToText(ShortCut);
end;
end;
end;
end;
FIni.WriteString(section,IntToStr(i),s);
end;
end;