数据库动态创建菜单(100分)

  • 主题发起人 主题发起人 pan_dayu
  • 开始时间 开始时间
P

pan_dayu

Unregistered / Unconfirmed
GUEST, unregistred user!
请教如何编写动态创建菜单的单独模块或类?数据库字段如下:
Idx,(菜单编号)MenuName,(标题)MenuType(值1表示主窗体,2表示子窗体,3表示级联窗体)MenuParent,(上级菜单编号)MenuOrder,(菜单顺序)FunctionType,,(类型:0表示外部命令,1表示窗体,2表示,3表示)MenuFunction,(菜单事件,按FunctionType字段值添加相应事件)MenuImage,MenuHit,UserFlag(用户权限)。。。。
 
用类实现你的需求较好...
形如以下形式:
type
TMenuType = (mtParentForm, mtChildrenForm, mtMutiForm);
TFunctionType = (ftOuterFunction, ftFormFunction, ftInterFunction);
TUserFlag = (ufAdministrator, ufPowerUser, ufOperator, ufViewer);

TMenuCreation = class
private
FIdx: Integer;
FMenuName: String;
FMenuType: TMenuType;
FMenuParent: Integer;
FMenuOrder: Integer;
FFunctionType: TFunctionType;
FMenuImageIndex: Integer;
FMenuHint: String;
FUserFlag: TUserFlag;

procedure SetIdx(const Idx: Integer);
function GetIdx: Integer;
public
constructor Create(aQuery: TQuery); overload; //用aQuery取得数据库相关信息并动态创建菜单
published
property Idx: Integer read GetIdx write SetIdx;
...
end;
 
类框架不重要,关健是构造函数的具体代码,可不可以写具体点?主要是Menufunction字段值的各类型处理函数代码
 
问题已结束。。。我已经想出来了,
 
噢,既然问题解决了,楼主可否考虑放分?
 
既然问题解决了,就公布一下解决方法吧。
 
好的,我的代码放在屋里头的,晚点上传上去...大家给个意见.
 
接分接分^o^
 
通过数据库动态创建菜单代码如下:
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.
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
825
DelphiTeacher的专栏
D
D
回复
0
查看
831
DelphiTeacher的专栏
D
后退
顶部