Dynamic Menu demo

來源:互聯網
上載者:User

//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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.