通过数据库动态创建菜单代码如下:
unit UdynamicMenu;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, SUIMainMenu, DB, ADODB, Grids, DBGrids, StdCtrls;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
procedure CreateFrmclick(sender:Tobject);
//procedure CreateFunction(sender:Tobject);
// function GetMenuFunction(menuItemL:TMenuItem;FunctionType1:integer;MenuFunction1:string):integer;
function GetNextItem(Idx1:integer;tableName:string;cnnstr1:string;usrFlag:integer; Menu1:TSuiMainMenu;Item:TMenuItem):integer;
Function CreateMenu(tableName:string;cnnstr1:string;usrFlag:integer; Menu1:TSuiMainMenu):integer;
end;
var
Form1: TForm1;
tableName1,cnnstr:string;
implementation
uses UdynamicMenu1;
{$R *.dfm}
procedure TForm1.CreateFrmclick(sender: Tobject);
var
// Frm2:TFrmtest;
MenuName,MenuFc1:string;
query1:TADOQuery;
begin
//查找名为MenuFc1窗体后创建并显示出来
if sender is TMenuItem then
begin
MenuName:=TMenuItem(sender).caption ;
MenuName:=copy(MenuName,2,length(MenuName));
end
else exit;
query1:=TADOQUERY.Create(nil);
query1.ConnectionString:=cnnstr;
try
with query1 do
begin
close;
sql.Clear;
sql.Text :='select MenuFunction from '+tableName1+' where MenuName='+''''+MenuName+'''';
open;
if recordcount <>0 then
begin
MenuFc1:=fieldbyname('MenuFunction').AsString;
//application.CreateForm(TFormClass(FindClass(MenuFc1)),MenuFc);
if getclass(MenuFc1)<>nil then
TFormClass(findclass(MenuFc1)).Create(self).ShowModal;
end;
end;//end with
finally
query1.Free;
end; //end try;
end;
function TForm1.GetNextItem(Idx1: integer; tableName, cnnstr1: string;
usrFlag: integer; Menu1: TSuiMainMenu;Item:TMenuItem): integer;
var
query1:TADOQUERY;
menuItem1:TMenuitem;
Idx:integer;
begin
query1:=TADOQUERY.Create(nil);
query1.ConnectionString:=cnnstr1;
result:=0;
try
with query1 do
begin
close;
sql.Clear;
sql.Text :='select * from '+tableName+' where userflag='+inttostr(usrFlag)+' and MenuParent='+inttostr(Idx1)+' order by MenuOrder';
open;
if recordcount<>0 then
begin
first;
while not eof do
begin
menuItem1:=Tmenuitem.Create(self);
menuItem1.Caption :=fieldbyname('MenuName').AsString ;
// GetMenuFunction(menuItem1,fieldbyname('FunctionType').AsInteger,fieldbyname('MenuFunction').AsString);
if fieldbyname('FunctionType').AsInteger =1 then
menuItem1.OnClick :=CreateFrmclick;
//如果为0则执行内部过程函数,为2则执行外部命令使用SHELLEXECUTE
item.Insert(item.Count,menuItem1);
Idx:=fieldbyname('Idx').AsInteger;
GetNextItem(Idx,tableName,cnnstr1,usrFlag,Menu1,menuItem1);
next;
end;
result:=recordcount;
end;
end;
finally
// menuItem1.Free;
// menu1.Free;
query1.Free;
end;
end;
function TForm1.CreateMenu(tableName, cnnstr1: string; usrFlag: integer;
Menu1: TSuiMainMenu): integer;
var
query1:TADOQUERY;
menuItem1:TMenuitem;
Idx:integer;
pos:integer;
begin
tableName1:=tablename;
cnnstr:=cnnstr1;
query1:=TADOQUERY.Create(nil);
query1.ConnectionString:=cnnstr1;
try
with query1 do
begin
close;
sql.Clear;
sql.Text :='select * from '+tableName+' where MenuType=0 and userflag='+inttostr(usrFlag)+' order by MenuOrder';
open;
if recordcount<>0 then
begin
first;
while not eof do//begin while;
begin
menuItem1:=Tmenuitem.Create(self);
menuItem1.Caption :=fieldbyname('MenuName').AsString ;
pos:=menu1.Items.IndexOf(MenuItem1);
menu1.Items.Add(menuItem1);
Idx:=fieldbyname('Idx').AsInteger;
GetNextItem(Idx,tablename,cnnstr1,usrFlag,Menu1,menuItem1);
next;
end;//end while;
result:=1;
end;//end if
end;//end with
finally
// menuItem1.Free;
// menu1.Free;
query1.Free;
end;//end try
end;
initialization
begin
registerclasses([Tform1]);
end;
finalization
begin
UnRegisterClasses([Tform1]);
end;
end.