//Dynamic Menu demo
procedure DrawMenu(MainMenu: TMainMenu);
procedure DrawSubMenu(PItem: TMenuItem; iID: Integer);
var
SqlStr: string;
Item: TMenuItem;
adoMenu: TADOQuery;
begin
adoMenu := TADOQuery.Create(nil);
adoMenu.Connection := DataConn.adoConn;
SqlStr := ' Select FModuleID,FPModuleID,FFileName,FTitle,FIsSubMain,bIsHide '
+ ' From MenuModules'
+ ' Where FPModuleID=' + IntToStr(iID)
+ ' Order By FSeqID';
if adoMenu.Active then adoMenu.Close;
adoMenu.SQL.Clear;
adoMenu.SQL.Add(SqlStr);
adoMenu.Open;
if not adoMenu.IsEmpty then
begin
while not adoMenu.Eof do
begin
if not adoMenu.FieldByName('bIsHide').AsBoolean then
begin
Item := TMenuItem.Create(nil);
Item.Hint := adoMenu.FieldByName('FFileName').AsString;
//Item.HelpContext
Item.Caption := adoMenu.FieldByName('FTitle').AsString;
PItem.Add(Item);
if adoMenu.FieldByName('FIsSubMain').AsBoolean then
DrawSubMenu(Item, adoMenu.FieldByName('FModuleID').AsInteger)
else
begin
Item.Enabled := False;
Item.OnClick := DoMenuItem;
end;
end;
adoMenu.Next;
end;
end;
adoMenu.Close;
FreeAndNil(adoMenu);
end;
var
Item: TMenuItem;
SqlStr: string;
begin
Item := MainMenu.Items;
for iCol := Item.Count - 1 downto 1 do //Unused Index=1 MenuItem and Last Name hint is Help menuItem
begin
if Uppercase(Item[iCol].Hint) = 'HELP' then Continue;
Item.Delete(iCol);
end;
SqlStr := ' Select FModuleID,FPModuleID,FFileName,FTitle,FIsSubMain,bIsHide '
+ ' From MenuModules'
+ ' Where FPModuleID Not In (Select FModuleID From MenuModules)'
+ ' Order By FSeqID';
if adoQuery.Active then adoQuery.Close;
adoQuery.SQL.Clear;
adoQuery.SQL.Add(SqlStr);
adoQuery.Open;
if not adoQuery.IsEmpty then
begin
while not adoQuery.Eof do
begin
if not adoQuery.FieldByName('bIsHide').AsBoolean then
begin
Item := TMenuItem.Create(nil);
Item.Hint := adoQuery.FieldByName('FFileName').AsString;
//Item.HelpContext
Item.Caption := adoQuery.FieldByName('FTitle').AsString;
MainMenu.Items.Insert(MainMenu.Items.Count - 1, Item);
if adoQuery.FieldByName('FIsSubMain').AsBoolean then
DrawSubMenu(Item, adoQuery.FieldByName('FModuleID').AsInteger)
else
begin
Item.Enabled := False;
Item.OnClick := DoMenuItem;
end;
end;
adoQuery.Next;
end;
end;
adoQuery.Close;
end;
procedure SetAllowMenuItem;
procedure SetItemProperty(Item: TMenuItem);
var
iCol: Integer;
begin
if Item.Count > 0 then
begin
for iCol := 0 to Item.Count - 1 do
begin
SetItemProperty(Item[iCol]);
end;
end else
Item.Enabled := //Get from Item property
end;
var
Item: TMenuItem;
iCol: Integer;
begin
Item := MainMenu.Items;
for iCol := 1 to Item.Count - 1 do //Unused Index=1 MenuItem and Last Name hint is Help menuItem
begin
if Uppercase(Item[iCol].Hint) = 'HELP' then Continue;
if Item[iCol].Count > 0 then
SetItemProperty(Item[iCol])
else
Item[iCol].Enabled := //Get from Item property
end;
end;
procedure DisableMenu;
procedure SetItemDisable(Item: TMenuItem);
var
iCol: Integer;
begin
if Item.Count > 0 then
begin
for iCol := 0 to Item.Count - 1 do
begin
SetItemDisable(Item[iCol]);
end;
end else
Item.Enabled := False;
end;
var
Item: TMenuItem;
iCol: Integer;
begin
Item := MainMenu.Items;
for iCol := 1 to Item.Count - 1 do //Unused Index=1 MenuItem and Last Name hint is Help menuItem
begin
if Uppercase(Item[iCol].Hint) = 'HELP' then Continue;
if Item[iCol].Count > 0 then
SetItemDisable(Item[iCol])
else
Item[iCol].Enabled := False;
end;
end;