4 : begin
MainMenu.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Count - 1]
.Items[NumMenusOnLevel2]
.Items[NumMenusOnLevel3]
.Add(NewMenuItem);
if EnableIncMenuItem then Inc(NumMenusOnLevel4);
{ Assign new event, created at runtime, to
the speed Button OnClick event, GenericSpeedButtonClick }
if AttachEventHandler then
begin
EventName := GenericSubMenuItemClick;
NewMenuItem.OnClick := TNotifyEvent(EventName);
end;
end;
5 : begin
MainMenu.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Count - 1]
.Items[NumMenusOnLevel2]
.Items[NumMenusOnLevel3]
.Items[NumMenusOnLevel4]
.Add(NewMenuItem);
if EnableIncMenuItem then Inc(NumMenusOnLevel5);
{ Assign new event, created at runtime, to
the speed Button OnClick event, GenericSpeedButtonClick }
if AttachEventHandler then
begin
EventName := GenericSubMenuItemClick;
NewMenuItem.OnClick := TNotifyEvent(EventName);
end;
end
else
MessageDlg('Only 5 Submenus Allowed! Too Many Submenus!',
mtConfirmation,mbYesNoCancel , 0);
// Do Nothing
end; // Case
except
on E: EInOutError do
begin
MessageDlg('Unable to create menu item. ' +
E.Message, mtInformation, [mbOK],0);
NewMenuItem.Free;
end;
end;
end;
// Generic MenuItem click
// Can either set up a new MenuItem toolbar or execute a file
procedure TMainForm.GenericSubMenuItemClick(Sender:TMenuItem);
var
Command, Params, WorkDir: String;
begin
Command := Sender.Hint; // Get the file name and path from hint
Params := '';
WorkDir := '';
ExecuteFile(Command, Params, WorkDir);
end;
Procedure TMainForm.ParseExecutableAndCommandLine(var Command, Params: string);
var
Size : Integer;
begin
Size := Pos(' ', Command);
if Size <= 0 then begin
Params := '';
Exit;
end;
SetLength(Command, Size);
Command := Trim(Command);
Params := Copy(Params,Size,Length(Params) - 1);
Params := Trim(Params);
end;
Procedure TMainForm.CreateNewMenuItem;
var
EventName : TMenuNotifyEvent;
begin
try
NewMenuItem := TMenuItem.Create(NewMenuItem);
NewMenuItem.Caption := MenuCaption;
NewMenuItem.Hint := MenuExecutable; // Use Hint To Store Program Path
case SubMenuLevel of
0 : begin
MainMenu.Items[MainMenu
.Items
.Count - 1]
.Add(NewMenuItem);
{ Assign new event, created at runtime, to
the speed Button OnClick event, GenericSpeedButtonClick }
if AttachEventHandler then
begin
EventName := GenericSubMenuItemClick;
NewMenuItem.OnClick := TNotifyEvent(EventName);
end;
end;
1 : begin
MainMenu.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Add(NewMenuItem);
{ Assign new event, created at runtime, to
the speed Button OnClick event, GenericSpeedButtonClick }
if AttachEventHandler then
begin
EventName := GenericSubMenuItemClick;
NewMenuItem.OnClick := TNotifyEvent(EventName);
end;
end;
2 : begin
MainMenu.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Count - 1]
.Add(NewMenuItem);
if EnableIncMenuItem then Inc(NumMenusOnLevel2);
{ Assign new event, created at runtime, to
the speed Button OnClick event, GenericSpeedButtonClick }
if AttachEventHandler then
begin
EventName := GenericSubMenuItemClick;
NewMenuItem.OnClick := TNotifyEvent(EventName);
end;
end;
3 : begin
MainMenu.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Items[MainMenu
.Items[MainMenu
.Items
.Count - 1]
.Count - 1]
.Count - 1]
.Items[NumMenusOnLevel2]
.Add(NewMenuItem);
if EnableIncMenuItem then Inc(NumMenusOnLevel3);
{ Assign new event, created at runtime, to
the speed Button OnClick event, GenericSpeedButtonClick }
if AttachEventHandler then
begin
EventName := GenericSubMenuItemClick;
NewMenuItem.OnClick := TNotifyEvent(EventName);
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
var
Index : Integer;
begin
SubMenuLevel := 0;
NumMenusOnLevel2 := 0;
NumMenusOnLevel3 := 0;
NumMenusOnLevel4 := 0;
NumMenusOnLevel5 := 0;
for Index := 0 To 5 do
MenuNumber[Index] := 0;
InitializeMenus;
end;
procedure TMainForm.InitializeMenus;
var
IniLineString: string;
PmmIniFile: TextFile;
begin
try
AssignFile(PmmIniFile, PmmIniFileName);
Reset(PmmIniFile);
while not Eof(PmmIniFile) do
begin
Readln(PmmIniFile, IniLineString); // Read line out of the file
IniLineString := Trim(IniLineString);
ParseMenuInfo(IniLineString);
end;
finally
CloseFile(PmmIniFile);
end;
end;
procedure TMainForm.ParseMenuInfo(S: string);
var
FirstChar : char;
Index : Integer;
begin
if Length(S) > 0 then
begin // if length
FirstChar := UpCase(S[1]);
case FirstChar of
'I' : begin
// Do Nothing
end;
'M' : begin
// Make Main Menu
AttachEventHandler := False;
SubMenuLevel := 0;
for Index := 1 To 5 do
MenuNumber[Index] := 0;
MenuCaption := ParseMenuCaption(S);
CreateNewMainMenu;
end;
'P' : begin
EnableIncMenuItem := True;
AttachEventHandler := True;
MenuCaption := ParseMenuCaption(S);
MenuExecutable := ParseMenuExe(S);
CreateNewMenuItem;
end;
'D' : begin
// make separator
AttachEventHandler := False;
MenuCaption := '-';
MenuExecutable := '';
CreateNewMenuItem;
end;
'S' : begin
// Make submenu
EnableIncMenuItem := False;
AttachEventHandler := False;
MenuCaption := ParseMenuCaption(S);
MenuExecutable := '';
CreateNewMenuItem;
Inc(SubMenuLevel);
end;
'E' : begin
// End submenus
AttachEventHandler := False;
SubMenuLevel := SubMenuLevel - 1;
end;
else
MessageDlg('Error In INI File Structure! Check Letters!', mtConfirmation,
mbYesNoCancel , 0);
// Do Nothing
end;
end; // if length
end;
function TMainForm.ParseMenuCaption(S: string): string;
var
Size : Integer;
begin
if Length(S) = 0 then begin
ParseMenuCaption := '';
Exit;
end;
Size := Pos('=', S) - 1;
if Size <= 0 then begin
S := Copy(S,2,Length(S) - 1);
ParseMenuCaption := Trim(S);
Exit;
end;
SetLength(S, Size);
S := Copy(S,2,Length(S) - 1);
ParseMenuCaption := Trim(S);
end;
function TMainForm.ParseMenuExe(S: string): string;
var
StringBegin : Integer;
begin
StringBegin := Pos('=', S) + 1;
if StringBegin <= 0 then begin
ParseMenuExe := '';
Exit;
end;
S := Copy(S,StringBegin,Length(S) - 1);
ParseMenuExe := Trim(S);
end;