当前位置:主页>delphi技巧/代码下载> 文章内容

如何遍历主菜单的各个菜单项?

发布时间:2010-01-22 | QQ免费站
1楼: 我用了以下代码却报错: var i, j: Integer; begin for i:=0 to Mainform.MainMenu1.Items.Count -1 do for j:=0 to Mainform.MainMenu1.Items[i].Items.Count -1 do ShowMessage(Mainform.MainMenu1.Items[i,j].Caption); 字串7

2楼: for i:=0 to form1.ComponentCount-1 do begin if form1.Components[i] is tmenuitem then begin tmenuitem(form1.Components[i]).Caption:=‘caption属性遍历‘; end; end; 字串9

3楼: 我想得到是主菜单的每一项。
字串7

4楼: 说具体点,具体要做什么功能?

字串9

5楼: 以下你看看,是否满足! procedure TRixiDL.Button1Click(Sender: TObject); var userid,username,StrZzrq,StrMax,MsgStr:string; i1,i2,i3:integer; //目前支持的菜单能够达到的级别为3级菜单; begin //判断登陆的日期是否为最新的版本 StrZzrq:=‘20050309‘; Edit1.Text:=Trim(Uppercase(Edit1.Text)); Sfs.Active:=False; Sfs.SQL.Clear; Sfs.SQL.Add(‘select max(xtzzrq) xtzzrq from xtm12‘); Sfs.Open; Sfs.First; StrMax:=Trim(UpperCase(Sfs.FieldByName(‘xtzzrq‘).AsString)); if StrMax<>StrZzrq then begin MsgStr:=‘您目前使用的程序不是最新的,建议更新程序,请与信息部联系!‘ + #13#10#13#10 + ‘如果执行后面的程序不报错,可以不更新的 咨询电话:xxx-xxxxxx-51‘+ #13#10#13#10 + ‘或则在ie中输入“http://876.90.11.24/thxz/RilkxiDesign.exe”下载最新程序,覆盖原文件即可‘+ #13#10#13#10 + ‘选择“YES”,下载该软件,同时程序退出,选择“NO”,忽略下载该软件,程序自动执行下一程序‘+ #13#10#13#10 + ‘原程序的最后修改日期是:‘+StrZzrq+‘ 现在程序的最新日期是:‘+StrMax; if MessageDlg(MsgStr,mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin shellexecute(Handle,nil,pchar(‘http://211.11.131.242/thxz/R11Design.exe‘),nil,nil,sw_normal); // WinExec(‘C:\Program Files\Internet Explorer\IEXPLORE.EXE http://211.10.231.242/thxz/RixiDesign.exe‘,SW_Normal); Close; end; end; //判断该用户是否为禁用,如果为禁用,则不能进入系统 Sfs.Active:=False; Sfs.SQL.Clear; StrZzrq:=format(‘select * from master.dbo.xtm02 where xtdwdm=‘‘%s‘‘ and userid=‘‘%s‘‘ ‘, [ComboBox1.Text,Edit1.Text]); Sfs.SQL.Add(StrZzrq); Sfs.Open; Sfs.First; StrMax:=Trim(UpperCase(Sfs.FieldByName(‘userqx‘).AsString)); if StrMax=‘Y‘ then begin if Edit1.Text<>‘ADMIN‘ then begin Application.MessageBox(‘该用户已经禁用,不能登陆,该用户请与分管领导联系‘, ‘提示‘, MB_OK + MB_ICONINFORMATION + MB_DEFBUTTON2); exit; end; end; if Edit1.Text=‘‘ then //如果用户名为空,请输入用户名 begin showmessage(‘用户名不能为空,请重新输入!!!‘); Edit1.SetFocus; exit; end; if Edit2.Text=‘‘ then //如果口令为空,请输入口令 begin showmessage(‘口令不能为空,请重新输入!!!‘); Edit2.SetFocus; exit; end; userid:=Edit1.Text; //给用户名和用户代码赋值 username:=Edit2.Text; Query1.Active:=False; Query1.SQL.Clear; Query1.SQL.Add(‘select * from master.dbo.xtm02‘); Query1.Active:=true; if Query1.Locate(‘userid;username‘, VarArrayOf([userid,username]), [loCaseInsensitive]) then begin Sfs.Active:=False; Sfs.SQL.Clear; Sfs.SQL.Add(‘select a.xtdwdm,a.userid,a.username,b.xtcddm,b.xtczqx from ‘ + ‘master.dbo.xtm02 a inner join xtm22 b on a.xtdwdm=b.xtdwdm and a.xtczjb=b.xtczjb ‘ + ‘where a.xtdwdm=:xtdwdm and a.userid=:userid ‘); Sfs.ParamByName(‘xtdwdm‘).AsString:=ComboBox1.Text; Sfs.ParamByName(‘userid‘).AsString:=Edit1.Text; Sfs.Open; if RixiMenu.MainMenu1.Items.Count>1 then begin for i1:=0 to RixiMenu.MainMenu1.Items.Count-1 do begin //判断改用户的权限是否存在? if Sfs.Locate(‘xtdwdm;userid;xtczqx;xtcddm‘,VarArrayOf([ComboBox1.Text,Edit1.Text,‘Y‘,RixiMenu.MainMenu1.Items[i1].Name]),[loCaseInsensitive]) then begin RixiMenu.MainMenu1.Items[i1].Enabled:=True; end else begin RixiMenu.MainMenu1.Items[i1].Visible:=False; end; for i2:=0 to RixiMenu.MainMenu1.Items[i1].Count-1 do begin if Sfs.Locate(‘xtdwdm;userid;xtczqx;xtcddm‘,VarArrayOf([ComboBox1.Text,Edit1.Text,‘Y‘,RixiMenu.MainMenu1.Items[i1].Items[i2].Name]),[loCaseInsensitive]) then begin RixiMenu.MainMenu1.Items[i1].Items[i2].Enabled:=True; end else begin RixiMenu.MainMenu1.Items[i1].Items[i2].Visible:=False; end; for i3:=0 to RixiMenu.MainMenu1.Items[i1].Items[i2].Count-1 do begin if Sfs.Locate(‘xtdwdm;userid;xtczqx;xtcddm‘,VarArrayOf([ComboBox1.Text,Edit1.Text,‘Y‘,RixiMenu.MainMenu1.Items[i1].Items[i2].Items[i3].Name]),[loCaseInsensitive]) then begin RixiMenu.MainMenu1.Items[i1].Items[i2].Items[i3].Enabled:=True; end else begin RixiMenu.MainMenu1.Items[i1].Items[i2].Items[i3].Visible:=False; end; end end end end else begin for i1:=0 to RixiMenu.MainMenu1.Items.Count-1 do begin //判断改用户的权限是否存在? if Sfs.Locate(‘xtdwdm;userid;xtczqx;xtcddm‘,VarArrayOf([ComboBox1.Text,Edit1.Text,‘Y‘,RixiMenu.MainMenu1.Items[i1].Name]),[loCaseInsensitive]) then begin RixiMenu.MainMenu1.Items[i1].Enabled:=True; end else begin RixiMenu.MainMenu1.Items[i1].Enabled:=False; end; for i2:=0 to RixiMenu.MainMenu1.Items[i1].Count-1 do begin if Sfs.Locate(‘xtdwdm;userid;xtczqx;xtcddm‘,VarArrayOf([ComboBox1.Text,Edit1.Text,‘Y‘,RixiMenu.MainMenu1.Items[i1].Items[i2].Name]),[loCaseInsensitive]) then begin RixiMenu.MainMenu1.Items[i1].Items[i2].Enabled:=True; end else begin RixiMenu.MainMenu1.Items[i1].Items[i2].Enabled:=False; end; for i3:=0 to RixiMenu.MainMenu1.Items[i1].Items[i2].Count-1 do begin if Sfs.Locate(‘xtdwdm;userid;xtczqx;xtcddm‘,VarArrayOf([ComboBox1.Text,Edit1.Text,‘Y‘,RixiMenu.MainMenu1.Items[i1].Items[i2].Items[i3].Name]),[loCaseInsensitive]) then begin RixiMenu.MainMenu1.Items[i1].Items[i2].Items[i3].Enabled:=True; end else begin RixiMenu.MainMenu1.Items[i1].Items[i2].Items[i3].Enabled:=False; end; end end end end; //mxt005 mxt003 该用户如果为Admin (主菜单必须存在 mxt000) if trim(Uppercase(Edit1.Text))=‘ADMIN‘ then begin RixiMenu.MXT000.Visible:=True; RixiMenu.MXT000.Enabled:=True; RixiMenu.MXT003.Visible:=True; RixiMenu.MXT003.Enabled:=True; RixiMenu.MXT004.Visible:=True; RixiMenu.MXT004.Enabled:=True; RixiMenu.MXT005.Enabled:=True; RixiMenu.MXT005.Visible:=True; end; RixiMenu.show; end else begin showmessage(‘输入的用户名或密码不正确,请核实后再输入!‘); Edit1.SetFocus; exit; end; //提取当前时间--服务器时间 Sfs.Active:=False; Sfs.SQL.Clear; StrZzrq:=format(‘select * from master.dbo.xtm02 where xtdwdm=‘‘%s‘‘ and userid=‘‘%s‘‘ ‘, [ComboBox1.Text,Edit1.Text]); Sfs.SQL.Add(StrZzrq); Sfs.Open; Userlb.Text:=Trim(Sfs.fieldByName(‘userlb‘).AsString); UserXm.Text:=Trim(Sfs.fieldByName(‘userxm‘).AsString); RixiDL.Hide; end;
字串3

6楼: 你要对菜单的每一项进行操作,那你肯定知道每一项的name属性或是caption属性撒,可以根据它的不同对菜单的每一项进行操作撒: for i:=0 to form1.ComponentCount-1 do begin if form1.Components[i] is tmenuitem then begin case tmenuitem(form1.Components[i]).name of N1 : tmenuitem(form1.Components[i]).caption:=‘‘; N2 : tmenuitem(form1.Components[i]).caption:=‘‘; N3 : tmenuitem(form1.Components[i]).caption:=‘‘; ................. else end; end; end; 不是吗?
字串6

7楼: 写个递归函数吧,随便好多级都可以.:) procedure TForm1.Button1Click(Sender: TObject); procedure FindMenuItem(Mi:TMenuItem); var i: integer; begin for i := 0 to Mi.Count-1 do begin if Mi.Items[i].Count>0 then FindMenuItem(Mi.Items[i]) else if Mi.Items[i].Caption<>‘-‘ then ShowMessage(Mi.Items[i].Caption); end; end; begin FindMenuItem(MainMenu.Items); end; 字串3

8楼: 你要对菜单的每一项进行操作,那你肯定知道每一项的name属性或是caption属性撒,可以根据它的不同对菜单的每一项进行操作撒: for i:=0 to form1.ComponentCount-1 do begin if form1.Components[i] is tmenuitem then begin case tmenuitem(form1.Components[i]).name of N1 : 对 N1 的操作; N2 : 对 N2 的操作; ................. else end; end; end; 不是吗?

字串5

9楼: 我要的就是主菜单的遍历效果,已经搞定了。多谢各位的指点。 var i, j: Integer; begin for i:=0 to Mainform.MainMenu1.Items.Count -1 do for j:=0 to Mainform.MainMenu1.Items[i].Count -1 do ShowMessage(Mainform.MainMenu1.Items[i].Items[j].Caption);
字串9