unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, DB, ADODB, StdCtrls;
type
PMenuRecord = ^TMenuRecord;
TMenuRecord = Record
MenuId:string[20];
end;
TForm1 = class(TForm)
MainMenu1: TMainMenu;
ADOConnection1: TADOConnection;
Button1: TButton;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
N1231: TMenuItem;
N1: TMenuItem;
ADOQuery3: TADOQuery;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
function createItem(s:string;ParentMenu:Tmenuitem;MainMEnu:TMainmenu
;Mid:string):TmenuItem;
procedure createChileItem(mid:string;layer:string;parentItem:TMenuItem);
procedure execItem(sender:Tobject);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.execItem(sender:tobject);
var
mid:string;
adoquery:TAdoquery;
begin
if sender is TMenuItem then
begin
mid := TMenuItem(sender).name;
adoquery := TAdoquery.Create(nil);
adoQuery.Connection := self.ADOConnection1;
try
with adoquery do
begin
sql.Add('select m_name_call from menu where m_id='''+mid+'''');
open;
showmessage(mid);
end;
finally
adoQuery.Free;
end;
end;
end;
function TForm1.createItem(s:string;ParentMenu:Tmenuitem;
MainMEnu:TMainmenu;Mid:string):TMEnuItem;
var
Item1:TMenuItem;
adoquery:TAdoquery;
begin
adoquery := TAdoquery.Create(nil);
adoQuery.Connection := self.ADOConnection1;
item1 := TMenuITem.Create(mainMenu);
item1.Name := mid;
parentMenu.Add(item1);
item1.Caption := s;
result := item1;
try
with adoquery do
begin
sql.Add('select m_name_call from menu where m_id='''+mid+'''');
open;
if Fields[0].IsNull then
item1.OnClick := nil
else
item1.OnClick := self.execItem;
end;
finally
adoquery.Free;
end;
end;
procedure TForm1.createChileItem(mid:string;layer:string;parentItem:TMenuItem);
var
Adoquery:TAdoQuery;
aItem:TMenuitem;
begin
adoquery := TAdoquery.Create(nil);
adoquery.Connection := self.ADOConnection1;
try
with adoquery do
begin
sql.Add('select m_id,M_name from menu where m_parent_id='''+mid+'''');
open;
while not eof do
begin
aItem:=self.createItem(fields[1].AsString,parentItem,self.MainMenu1,Fields[0].asstring);
self.createChileItem(Fields[0].asstring,'1',aItem);
next;
end;
end;
finally
AdoQuery.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
menuRecord:PMenuRecord;
mid : string;
aItem:TMenuitem;
i:integer;
begin
with adoQuery1 do
begin
if active then close;
sql.Clear;
sql.Add('select M_Id,M_name from Menu where M_layer=1');
open;
i:=2;
while not eof do
begin
mid := fields[0].AsString;
aitem:=self.createItem(Fields[1].asstring,self.MainMenu1.Items,self.MainMenu1,mid);
aitem.MenuIndex := i;
self.createChileItem(Fields[0].asstring,'2',aItem);
next;
i:=i+1;
end;
end;
n1.MenuIndex := i; //改变菜单的位置
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
n1231.MenuIndex :=1;
end;
end.