plenilune168大侠,这样比较直接
这个是我写的动态菜单,而且要动态弹出窗体的代码
等下我把我所以的数据表发给你
unit MMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, DB, ADODB, Menus, ComCtrls, MLogin, MUserManage,
MAuthority, MLog, MDM_Main;
type
//窗体调用对象
TFunLoad=procedure of Object;
TFunLoadObject=class(TObject)
FormName:String;
Fun:TFunLOad;
end;
TMain = class(TForm)
StatusBar1: TStatusBar;
MainMenu1: TMainMenu;
QueryMenu: TADOQuery;
QueryMenu1: TADOQuery;
Timer1: TTimer;
QueryMain: TADOQuery;
QueryDetail: TADOQuery;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
FormShowFunArr:TStrings
//调用方法数组
procedure Form_Show_Usermanage
//用户管理
procedure Form_Show_Authority
//权限管理
procedure Form_Add(FormName:string
Fun:TFunLoad);//增加窗体调用方法
// procedure Form_Load(FormName:string)
overload
//根据FormName调用模块
procedure Form_Load(Index:integer)
//根据序号调用模块
procedure Form_Clear;
procedure DoMenuClick(Sender: TObject)
//清除窗体调用方法
// procedure FrmNew(Sender:TObject);
{ Public declarations }
end;
var
Main: TMain;
NewParItem,NewChildItem: TMenuItem;
J,K:String;
implementation
{$R *.dfm}
procedure TMain.Form_Show_Usermanage;
begin //调用方法:用户管理
with TUsermanage.Create(nil) do
try
ShowModal;
finally
Free;
end
end;
procedure TMain.Form_Show_Authority;
begin //调用方法:权限管理
with TAuthority.Create(nil) do
try
ShowModal;
finally
Free;
end
end;
procedure TMain.Form_Add(FormName:String
Fun:TFunLoad);
var
FunLoadObject:TFunLoadObject;
//增加窗体调用方法
begin
FunLoadObject:=TFunLoadObject.Create;
FunLoadObject.FormName:=FormName;
FunLoadObject.Fun:=Fun;
FormShowFunArr.AddObject(FormName,FunLoadObject);
end;
procedure Tmain.Form_Load(Index:integer);
var FunLoadObject:TFunLoadObject;
begin //根据序号调用模块
FunLoadObject:=TFunLoadObject(FormShowFunArr.Objects[Index]);
if Assigned(FunLoadObject.Fun) then FunLoadObject.Fun();
end;
{procedure TMain.Form_Load(FormName:string);
var
funLoadObject:TFunLoadObject;
I:Integer;
begin //根据FormName调用模块
I:=FormShowFunArr.IndexOf(FormName);
if I>=0 then
begin
FunLoadObject:=TFunLoadObject(FormShowFunArr.Objects);
if Assigned(FunLoadObject.Fun) then
FunLoadObject.Fun();
end;
end
}
procedure TMain.Form_Clear;
var
FunLoadObject:TFunLoadObject;
I:Integer;
begin //清除窗体调用方法
for I:=0 to FormShowFunArr.Count-1 do
begin
FunLoadObject:=TFunLoadObject(FormShowFunArr.Objects);
FreeAndNil(FunLoadObject);
end;
FormSHowFunArr.Clear;
end;
procedure TMain.DoMenuClick(Sender:TObject);
var
I:integer;
begin
I:=TMenuItem(Sender).Tag;
Form_Load(I);
end;
{procedure TMain.FrmNew(Sender: TObject);
//单击主菜单中的某个子菜单,弹出相应的子菜单
begin
QueryMain.Close;
QueryMain.SQL.Clear;
QueryMain.SQL.Add('select data1,formName from User1 as a,Menu01 as b,Menu02 as c,authtable as d where a.UserId=d.authUser and c.authId=d.qxauthId and b.MainId=c.Id and d.qxid=b.mainID and UserId='''+Login.EdtUser.Text+''' and authority=''Y'' order by qxID,qxAuthID ');
QueryMain.Open;
QueryMain.Active:=true;
With Sender as TmenuItem do
begin
if caption='用户管理' then
with TUsermanage.Create(nil) do
try
ShowModal;
finally
Free;
end;
if caption='权限管理' then
with TAuthority.Create(nil) do
try
ShowModal;
finally
Free;
end;
if caption='查询日志' then
with TLog.Create(nil) do
try
ShowModal;
finally
Free;
end;
if caption='系统退出' then
try
application.Terminate;
finally
end;
end;
end;
}
procedure TMain.FormShow(Sender: TObject);
var
I,s:integer;
//将符合条件的记录(包括用户名,权限),添加到MainMenu的Item上。
begin
//调出全部主菜单
StatusBar1.Panels[0].Text := ' 用户名:'+Login.EdtUser.Text;
QueryMain.Close;
QueryMain.SQL.Clear;
QueryMain.SQL.Add('select distinct data,MainId from User1 as a,Menu01 as b,Menu02 as c,authtable as d where a.UserId=d.authUser and c.authId=d.qxauthId and b.MainId=c.Id and d.qxID=b.MainId and UserId='''+Login.EdtUser.Text+''' and authority=''Y''');
QueryMain.Active:=true;
//调出全部子菜单
QueryDetail.Close;
QueryDetail.SQL.Clear;
QueryDetail.SQL.Add('select * from User1 as a,Menu01 as b,Menu02 as c,authtable as d where a.UserId=d.authUser and c.authId=d.qxauthId and b.MainId=c.Id and d.qxid=b.mainID and UserId='''+Login.EdtUser.Text+''' and authority=''Y'' order by qxID,qxAuthID ');
QueryDetail.Active:=true;
while not QueryMain.Eof do
begin
//增加主菜单
NewParItem:=TMenuItem.Create(self);
NewParItem.Caption:=QueryMain.fieldByName('data').AsString;
MainMenu1.Items.Add(NewParItem);
//过滤出对应当前主菜单的子菜单项
{ QueryDetail.Filtered:=false;
QueryDetail.Filter:=format('ID=%d',[QueryMain.FieldByName('MainID').AsInteger]);
QueryDetail.Filtered:=true
}
//循环增加子菜单
QueryDetail.First;
while not QueryDetail.Eof do
begin
NewChildItem:=TMenuItem.Create(self);
NewChildItem.Caption:=QueryDetail.FieldByName('data1').AsString;
NewChildItem.Add(NewChildItem);
NewChildItem.Tag:=FormShowFunArr.IndexOf(QueryDetail.fieldByName('FormName').AsString);
NewChildItem.OnClick:=DoMenuClick;
QueryDetail.Next;
end;
QueryMain.Next;
end;
// end;
{
QueryMain.Close;
QueryMain.SQL.Clear;
//Login.QueryAuth.SQL.Add('select data from Menu01');
QueryMain.SQL.Add('select distinct data,MainId from User1 as a,Menu01 as b,Menu02 as c,authtable as d where a.UserId=d.authUser and c.authId=d.qxauthId and b.MainId=c.Id and d.qxID=b.MainId and UserId='''+Login.EdtUser.Text+''' and authority=''Y''');
QueryMain.SQL.Add('order by MainID');
QueryMain.Active:=true;
while not QueryMain.Eof do
begin
NewItem:=TmenuItem.Create(self);
NewItem.Caption:=QueryMain.FieldValues['data'];
MainMenu1.Items.Add(NewItem);
NewItem.AutoHotkeys:=maManual;
QueryMain.Next;
end;
//将符合条件的记录(包括用户名,权限),添加到MainMenu子菜单栏上。
QueryMain.Close;
QueryMain.SQL.Clear;
QueryMain.SQL.Add('select * from User1 as a,Menu01 as b,Menu02 as c,authtable as d where a.UserId=d.authUser and c.authId=d.qxauthId and b.MainId=c.Id and d.qxid=b.mainID and UserId='''+Login.EdtUser.Text+''' and authority=''Y'' order by qxID,qxAuthID ');
QueryMain.Open;
QueryMain.Active:=true;
for i:=1 to QueryMain.RecordCount do
begin
QueryMain.Locate('data1',Trim(QueryMain.FieldValues['data1']),[]);
s:=QueryMain.FieldValues['ID'];
NewItem:=TMenuItem.Create(self);
NewItem.Caption:=Trim(QueryMain.FieldValues['data1']);
MainMenu1.Items[s-1].Add(NewItem);
//NewItem.OnClick:=FrmNew;
NewItem.Tag:=FormShowFunArr.IndexOf(QueryMain.FieldValues['FormName']);
NewItem.OnClick:=DoMenuClick;
QueryMain.Next;
end;}
end;
procedure TMain.FormClose(Sender: TObject
var Action: TCloseAction);
begin
application.Terminate;
end;
procedure TMain.Timer1Timer(Sender: TObject);
begin
StatusBar1.Panels[1].Text:=' 时间:'+timetostr(time);
end;
procedure TMain.FormCreate(Sender: TObject);
begin
FormShowFunArr:=TStringList.Create
//建立调用方法数组
//把调用方法及其FormName到调用方法数组
Form_Add('Form_Show_Usermanage',Self.Form_Show_Usermanage);
Form_Add('Form_Show_Authority',Self.Form_Show_Authority);
end;
end.