大家好,小弟的个事要问:
 MainMenu1: TMainMenu;
这个建立菜单的。我想从数据库中生成。让它动态从数据库中产生。

解决方案 »

  1.   

    procedure TForm1.ReadTree(tnode:TTreeNode;Fvalue: String);
    Var
      i: integer;
      Flist:TStringList;
      Flist1:TStringList;
      str:string;
    begin
      qryTree.close;
      qryTree.sql.clear;
      qryTree.sql.add('select Parentid,id,context from tbtree');
      qryTree.sql.add('where isnull(Parentid,'''')=');
      qryTree.sql.add(''''+Fvalue+'''');
      qryTree.Open;
      qryTree.First;
      Flist:=TStringList.Create;
      Flist1:=TStringList.Create;
      while not qryTree.eof do
        begin
          Flist.Add(trim(qryTree.fieldbyname('context').asstring));
          Flist1.Add(qryTree.fieldbyname('id').asstring);
          qryTree.next;
        end;
        for i:= 0 to flist.Count-1 do
        begin
          s:=flist1.Strings[i];
          str:=flist.Strings[i];
          snode:=Treeview1.items.addchild(tnode,str);
          ReadTree(snode,s);
        end;
      flist.free;
      flist1.free;
    end;在FORMCREATE事件里调用就是了,数据结构为
    Parentid(父节点),id(了节点),Context(显示的内容)
    结贴吧!呵呵
      

  2.   

    动态创建菜单
      mmiTemp:TMenuItem;
    begin
            mmiTemp:=TMenuItem.Create(mmMain);
            mmiTemp.Caption:= '标题';//
            mmiTemp.OnClick:= actLoadExecute;//设定action
            mmiImport.Add(mmiTemp);//加入到上层的菜单
    end;
      

  3.   

    var
      Form1: TForm1;
      MyMainMenu: TMainMenu;
      {TMainMenu encapsulates a menu bar and its accompanying drop-down
       menus for a form}
      MyPopUpMenu: TPopUpMenu;
      {TPopupMenu encapsulates the properties, methods, and events of
       a pop-up menu}
      MySubItems: array[0..3] of TMenuItem;
      {TMenuItem describes the properties of an item in a menu.
       Each TMainMenu or TPopupMenu component can contain multiple
       menu items.}
      MyPopUpItems: array[0..3] of TMenuItem;
      i: Integer;
    implementation{$R *.dfm}
    procedure TForm1.MyPopupHandler(Sender:TObject);
    begin
     with Sender as TMenuItem do
     begin
       //在信息对话框中显示选中菜单项的Caption属性
       ShowMessage(Caption);
     end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      MyItem:array[0..2] of TMenuItem;
    begin
      MyMainMenu:=TMainMenu.Create(Self);
      //创建三个子菜单
      for i:=0 to 2 do begin
       MyItem[i]:=TMenuItem.Create(Self);
       MyItem[i].Caption:='主菜单'+IntToStr(i)+
                          '(&'+IntToStr(i)+')';
       MyMainMenu.Items.Add(MyItem[i]);
    //Add()--Adds one or more menu items to the end of the
    //       Items property array。添加主菜单的第一层菜单项
      end;
      for i:=0 to 3 do begin
        MySubItems[i]:=TMenuItem.Create(Self);
        MySubItems[i].Caption:='子菜单项'+IntToStr(i)+
                              '(&'+IntToStr(i+1)+')';
        MyMainMenu.Items[0].Add(MySubItems[i]);
        //在主菜单第一层菜单项添加第二层菜单项
       
        //指定菜单项的OnClick事件的处理过程
        MySubItems[i].OnClick:=MyPopUpHandler;
      end;
      //将第2个菜单项设置为分隔条
      MySubItems[1].Caption:='-';
      //设置竖向分隔条
      MySubItems[3].Break:=mbBreak;
      {Break--Determines whether the menu item starts
              a new column in the menu
       mbBarBreak--The Parent menu breaks into another column
                   with the menu item appearing at the top of
                   the new column. 而且在新列与旧列之间多一条竖线
       mbBreak--在新列与旧列之间没有竖线
           }
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      MyPopUpMenu:=TPopUpMenu.Create(self);
      for i:=0 to 3 do
      begin
       MyPopUpItems[i]:=TMenuItem.Create(Self);
       //设置菜单项的Caption属性
       MyPopUpItems[i].Caption:='弹出菜单项'+IntToStr(i)+
                                '(&'+IntToStr(i+1)+')';
       MyPopUpMenu.Items.Add(MyPopUpItems[i]);
       //指定菜单项的OnClick事件的处理过程
       MyPopUpItems[i].OnClick:=MyPopupHandler;
      end;
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      //通过按钮激活弹出式菜单
      if  MyPopUpMenu<>nil then
        MyPopUpMenu.Popup(Form1.Left+60,Form1.Top+60);
      //Displays the pop-up menu onscreen.参数是弹出菜单的位置
    end;
      

  4.   

    试试
    关注一下
    type
      TMenuNotifyEvent = procedure(Sender: TMenuItem) of object;{说明一个菜单单击事件类型}
      TButNotifyEvent = procedure(Sender: TButton) of object;{说明一个按钮单击事件类型}
      TForm1 = class(TForm)
        MainMenu1: TMainMenu;
        N11: TMenuItem;
        N21: TMenuItem;
        Button1: TButton;
        procedure AddMenuText(MenuText: String);
        procedure MenuClick(Sender:TMenuItem);
        procedure ButClick(Sender:TButton);
        procedure FormCreate(Sender: TObject);
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      NewMenuItem:TMenuItem;{声明一个全局变量}implementation{$R *.DFM}procedure TForm1.AddMenuText(MenuText: String);
    {动态加入菜单}
    var
      EventName:TMenuNotifyEvent;
      NewMenuItem:TMenuItem;
    begin
      NewMenuItem:=TMenuItem.Create(self);{创建菜单实例}
      NewMenuItem.Caption:=MenuText;//菜单项内容
      EventName:=MenuClick;
      NewMenuItem.OnClick:=TNotifyEvent(EventName);{指定菜单单击事件}
      MainMenu1.Items[MainMenu1.Items.Count-1].Add(NewMenuItem);{将菜单实例加到主菜单中}
    end;procedure TForm1.MenuClick(Sender:TMenuItem);
    {菜单单击事件函数}
    begin
      showmessage(Sender.Caption);
    end;procedure TForm1.FormCreate(Sender: TObject);
    label Write;
    var
    Search:TSearchRec;
      EventName:TButNotifyEvent;
    begin
      with Search do
        begin
          if FindFirst(Getcurrentdir+'\*.*',0,Search)=0 then
          begin
          Write:
          AddMenuText(Name);
          if FindNext(Search)=0 then
            goto Write;
          end;
        end;
      EventName:=ButClick;
      Button1.OnClick:=TNotifyEvent(EventName);{指定菜单单击事件}
    end;
    procedure TForm1.ButClick(Sender:TButton);
    {菜单单击事件函数}
    begin
      showMessage(inttostr(Sender.Left + Sender.Width));
      showmessage(Sender.Caption);
    end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if(Button = mbRight) then
        
    end;