unigui菜单【3】
unigui菜单TuniTreeView
根据数据库表中的内容,显示菜单的处理:
function TMainForm.CreateMenu: Integer; var myMenuPoint : PMyMenuRec; i :Integer; menu_id ,menu_name, menu_url: string; mynodes : TUniTreeNodes; mynode,tmp_node : TUniTreeNode; begin //建立菜单树 MenuTree.Items.Clear; mynodes := MenuTree.Items; MenuTree.BeginUpdate; with UniMainModule do begin //取表中菜单 fdqryBase.Open('select* from sm_menu order by menu_id'); fdqryBase.First; while not fdqryBase.Eof do begin menu_id := fdqryBase.FieldByName('menu_id').AsString; menu_name := fdqryBase.FieldByName('menu_name').AsString; if fdqryBase.FieldByName('menu_url').AsString = '' then begin //确认是否根菜单 mynode := mynodes.Add(nil,fdqryBase.FieldByName('menu_name').AsString); end else begin tmp_node := mynodes.add(mynode,menu_name); New(myMenuPoint); myMenuPoint^.menu_id := menu_id; myMenuPoint^.menu_name := menu_name; myMenuPoint^.menu_url := fdqryBase.FieldByName('menu_url').AsString; tmp_node.Data := myMenuPoint; end; fdqryBase.Next; end; end; MenuTree.EndUpdate; for i := 0 to MenuTree.Items.Count-1 do begin mynode := MenuTree.Items[i]; if mynode.HasChildren then begin mynode.ImageIndex := 0; mynode.SelectedIndex := 0; end else begin mynode.ImageIndex := 2; mynode.SelectedIndex := 3; end; end; MenuTree.FullCollapse; end;
其中item的node有二个属性caption和data。把指针data用起来,在点击时自动调用显示form。
type PMyMenuRec = ^TMyMenuRec; TMyMenuRec = Record menu_id:string; menu_name:string; menu_url:string; end;
点击菜单项的处理
procedure TMainForm.MenuTreeClick(Sender: TObject); begin if Assigned(MenuTree.Selected.data) then begin if PMyMenuRec(MenuTree.Selected.data).menu_url<>'' then ShowFrame(PMyMenuRec(MenuTree.Selected.data).menu_name,PMyMenuRec(MenuTree.Selected.data).menu_url); end; end;
function TMainForm.ShowFrame(Caption, FormClassName: string): Integer; var i: integer; sheet: TUniTabSheet; begin for i := 0 to page.PageCount - 1 do begin if page.Pages[i].Caption = Caption then begin page.ActivePageIndex := i; exit; end; end; sheet := TUniTabSheet.Create(Self); sheet.Closable := True; sheet.Caption := Caption; sheet.PageControl := page; FCurrentFrame := TUniFrameClass(FindClass(FormClassName)).Create(Self); FCurrentFrame.Align := alClient; FCurrentFrame.Parent := sheet; page.ActivePage := sheet; end;
表菜单记录。
10 登记客户信息 NULL 1001 新客户 TUniFrmeWebEmbedBase 1002 修改客户 222 20 客户查询 NULL 2001 按地区 43 2002 按行业 3452 30 客户信息统计 NULL 40 客户新机报表 NULL
表结构
CREATE TABLE [dbo].[SM_menu]( [menu_id] [nvarchar](32) NOT NULL, [menu_name] [nvarchar](30) NULL, [menu_url] [nvarchar](50) NULL, CONSTRAINT [PK153] PRIMARY KEY CLUSTERED ( [menu_id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]