'初使化菜单 Public Sub New() Me.BackColor = System.Drawing.Color.DeepPink Me.BorderStyle = BorderStyle.Outset Me.Width = Unit.Pixel(250) 'Me.Width = Unit.Pixel(50) Me.BackGround = "" Me.BorderWidth = Unit.Pixel(0) Me.Type = 1 End Sub
'自定义OnChange事件,该事件用来表示用户所调用菜单项所完成的功能 Public Event Change As EventHandler '触发OnChange事件。 Protected Overridable Sub OnChange(ByVal e As EventArgs) RaiseEvent Change(Me, e) End Sub ' 当由类实现时,使服务器控件能够处理将窗体发送到服务器时引发的事件。 Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent OnChange(New EventArgs()) End Sub
'通过Render方法把组件中的源码直接输出到html文件中,在客户端让IE直接执行。主要要判定出用户所选择的菜单项,分情况处理。显示该菜单体,隐藏其它菜单体。 ' /// <summary> '/// 将此控件呈现给指定的输出参数。 '/// </summary> '/// <param name="output"> 要写出到的 HTML 编写器 </param> Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter) MyBase.Render(output) output.Write(Chr(13) + Chr(10)) Dim str As String str = "<script language='javascript'> " + Chr(13) + Chr(10) & _ "function OpenTable(obj1,index,Type){ " + Chr(13) + Chr(10) & _ " var obj,coll,MenuObj,PareObj; " + Chr(13) + Chr(10) & _ " switch ( Type){ " + Chr(13) + Chr(10) & _ " case 0: //'下菜单 " + Chr(13) + Chr(10) & _ " PareObj=obj1.offsetParent; MenuObj=PareObj.offsetParent; break; " + Chr(13) + Chr(10) & _ " case 1: //'左菜单 " + Chr(13) + Chr(10) & _ " PareObj=obj1.offsetParent; MenuObj=PareObj.offsetParent; break; " + Chr(13) + Chr(10) & _ " case 2: // '无菜单 " + Chr(13) + Chr(10) & _ " PareObj=obj1.offsetParent;MenuObj=PareObj.offsetParent; break;" + Chr(13) + Chr(10) & _ " case 3: " + Chr(13) + Chr(10) & _ " coll=obj1.all.tags('TABLE'); " + Chr(13) + Chr(10) & _ " for (i=1;i<coll.length;i++) " + Chr(13) + Chr(10) & _ " coll[i].style.visibility='hidden';return; " + Chr(13) + Chr(10) & _ " case 4: //'Form " + Chr(13) + Chr(10) & _ " coll= document.all.tags('span'); " + Chr(13) + Chr(10) & _ " for (i=0;i<coll.length ;i++) " + Chr(13) + Chr(10) & _ " { a=coll[i].id.split('_'); " + Chr(13) + Chr(10) & _ " if (a[a.length-1]==obj1) break; } //window.alert (obj1);//var obj1=document.getElementById(obj1); " + Chr(13) + Chr(10) & _ " obj1=coll[i];coll=obj1.all.tags('TABLE'); " + Chr(13) + Chr(10) & _ " for (i=1;i<coll.length;i++) " + Chr(13) + Chr(10) & _ " coll[i].style.visibility='hidden'; return;} " + Chr(13) + Chr(10) & _ " coll=MenuObj.all.tags('TABLE');" + Chr(13) + Chr(10) & _ " for (i=0;i<coll.length;i++) " + Chr(13) + Chr(10) & _ " if (coll[i] ==PareObj ) break; " + Chr(13) + Chr(10) & _ " obj=coll[i+index+1]; " + Chr(13) + Chr(10) & _ " for (j=i+2;j<coll.length;j++) " + Chr(13) + Chr(10) & _ " coll[j].style.visibility='hidden'; " + Chr(13) + Chr(10) & _ " if (Type==2) return; " + Chr(13) + Chr(10) & _ " var top,left,hight,width; " + Chr(13) + Chr(10) & _ " top=obj1.offsetTop;left=obj1.offsetLeft;hight=obj1.offsetHeight; width=obj1.offsetWidth; " + Chr(13) + Chr(10) & _ " while(obj1=obj1.offsetParent) " + Chr(13) + Chr(10) & _ " { top+=obj1.offsetTop; left+=obj1.offsetLeft; } " + Chr(13) + Chr(10) & _ " var PareLeft=obj.offsetParent.offsetLeft;PareTop=obj.offsetParent.offsetTop;PareWidth=obj.offsetWidth; " + Chr(13) + Chr(10) & _ " if (Type==0) " + Chr(13) + Chr(10) & _ " {obj.style.top=top+hight-PareTop; obj.style.left=left-PareLeft;} " + Chr(13) + Chr(10) & _ " else {obj.style.top=top-PareTop; obj.style.left=left+width-PareLeft;} " + Chr(13) + Chr(10) & _ " obj.style.visibility='visible'; " + Chr(13) + Chr(10) & _ " } " + Chr(13) + Chr(10) & _ " </script>" + Chr(13) + Chr(10 output.Write(str)
End Sub '通知使用基于合成的实现的服务器控件创建它们包含的任何子控件,以便为回发或呈现做准备。 Protected Overrides Sub CreateChildControls() '生成主菜单 Dim MainTable As New Table() Dim MainNodes() As MenuNode MainNodes = Me.GetSubNodes(0) MainTable = CreateMainMenu(MainNodes) Me.Controls.Add(MainTable)
'得到子菜单数及每个子菜单的行数 Dim subCount As Int16 = MainNodes.Length - 1
'递规生成子菜单 Dim i As Int16 For i = 0 To subCount - 1 CreateSubMenu(MainNodes(i).ID) Next
' 透明处理 Me.Attributes("style") = Me.Attributes("style") + ";background-color: transparent " Me.Attributes("onclick") = "OpenTable(this ,0,3 )" Dim Form1 = cls.GetParent(Me, "HtmlForm") Form1.Attributes("onclick") = "OpenTable('" + Me.ID + "',0,4 )" Me.BorderWidth = Unit.Pixel(0) 'For i = 1 To Me.coll.Count ' Me.coll.Remove(1) 'Next End Sub
'生成主菜单。由结点生成菜单体 Private Function CreateMainMenu(ByVal Nodes() As MenuNode) As Table Dim i, j As Int16 '计算父菜单数(及面板数),子菜单数 Dim Count As Int16 = Nodes.Length - 1 Dim Table1 As New Table() Table1 = Me.CreateTable(1, 2 * Count - 1) Me.SetTableStyle(Table1)
'生成主表 For i = 0 To Count - 1 Dim Node As New MenuNode() Node = Nodes(i) Dim Cell1 As New TableCell() j = 2 * i Cell1 = Table1.Rows(0).Cells(j)
'是否有图片,有则生成图片对象,否则生成Label代替 If Node.ImageUrl <> "" Then Cell1.Controls.Add(Me.CreateImage(Node)) End If
Me.SetCellStyle(Cell1, Node)
'Dim SubName As String 'SubName = Me.ID + "_" + "T%" + Node.ID 'i.ToString ' Me.Parent.Parent.Parent.Parent.Controls()
'鼠标经过时把所有子菜单隐藏(, 把所对应的子菜单显示) If Me.HasSub(Node) Then Cell1.Attributes("onmouseover") = "this.style.background='" + Me.GetColorValue(SelectBackColor) + "';OpenTable(this," + i.ToString + ",0 )" Else Cell1.Attributes("onmouseover") = "this.style.background='" + Me.GetColorValue(SelectBackColor) + "';OpenTable(this," + i.ToString + ",2 )"
Dim button1 As New LinkButton() button1.ID = Nodes(i).ID
Me.Controls.Add(button1) button1.CommandArgument = Nodes(i).ID.ToString AddHandler button1.Click, AddressOf TableRow_Click
Cell1.Attributes("onclick") = "javascript:__doPostBack('" + Me.ID + ":" + button1.ID + "','')"
End If Cell1.Attributes("onmouseout") = "this.style.background='" + Me.GetColorValue(Me.BackColor) + "'"
If i < Count - 1 Then Table1.Rows(0).Cells(2 * i + 1).Width = Unit.Pixel(25) End If Next If Table1.Rows(0).Cells.Count > 0 Then Table1.Rows(0).Cells(0).ID = "Cell0" End If Table1.ID = "T%" Return Table1 End Function
'递归生成子菜单 Private Sub CreateSubMenu(ByVal ID As Int16) Dim Nodes() As MenuNode Nodes = Me.GetSubNodes(ID) Dim Count As Int16 = Nodes.Length - 1 Dim TableRow(Count) As TableRow '计算父菜单数(及面板数),子菜单数 Dim Table1 As New Table() Me.SetTableStyle(Table1) Dim i As Int16 For i = 0 To Count - 1 Dim Node As New MenuNode() Node = Nodes(i)
TableRow(i) = New TableRow() TableRow(i) = Me.CreateMenuItem(Node)
'Dim SubName As String 'SubName = Me.ID + "_" + "T%" + Node.ID ' (Index + Count - 1).ToString
'鼠标经过时把所有子菜单隐藏,把所对应的子菜单显示 If Me.HasSub(Node) Then TableRow(i).Attributes("onmouseover") = "this.style.background='" + Me.GetColorValue(SelectBackColor) + "';OpenTable(this," + i.ToString + " ,1 )" Else
TableRow(i).Attributes("onmouseover") = "this.style.background='" + Me.GetColorValue(SelectBackColor) + "';OpenTable(this," + i.ToString + " ,2 )" Dim button1 As New LinkButton() button1.ID = Nodes(i).ID Me.Controls.Add(button1) button1.CommandArgument = Nodes(i).ID.ToString AddHandler button1.Click, AddressOf TableRow_Click If Me.Type = EType.IsCheck Or Me.Type = EType.IsCheckBackColor Then TableRow(i).Attributes("onclick") = "this.cells(0).text='√'; javascript:__doPostBack('" + Me.ID + ":" + button1.ID + "','')" Else TableRow(i).Attributes("onclick") = "javascript:__doPostBack('" + Me.ID + ":" + button1.ID + "','')"
End If ' button1.Attributes("href") = "/" End If TableRow(i).Attributes("onmouseout") = "this.style.background='" + Me.GetColorValue(Me.BackColor) + "'" Table1.Controls.Add(TableRow(i)) Next Table1.Attributes("style") = "Z-INDEX:999;position:absolute;visibility:hidden ;BACKGROUND-COLOR:" + Me.GetColorValue(Me.BackColor)
Table1.ID = "T%" + ID.ToString 'Index.ToString Index = Index + 1 Me.Controls.Add(Table1) For i = 0 To Count - 1 ID = Nodes(i).ID If Me.HasSub(Nodes(i)) Then CreateSubMenu(ID) End If Next End Sub
'设置单元格风格 Private Sub SetCellStyle(ByRef cell1 As TableCell, ByVal item As MenuNode) cell1.BorderStyle = BorderStyle.None 'cell1.BorderWidth = me.BorderStyle cell1.Enabled = item.Enabled ' cell1.EnableViewState = item.EnableViewState ' cell1.Height = item.Height cell1.TabIndex = item.TabIndex cell1.ToolTip = item.ToolTip cell1.Visible = item.Visible
' cell1.BackColor = Me.BackColor cell1.Font.Bold = Me.Font.Bold cell1.Font.Italic = Me.Font.Italic cell1.Font.Name = Me.Font.Name cell1.Font.Overline = Me.Font.Overline cell1.Font.Size = Me.Font.Size cell1.Font.Strikeout = Me.Font.Strikeout cell1.Font.Underline = Me.Font.Underline cell1.ForeColor = Me.ForeColor
cell1.Text = GetHotKeyText(item) cell1.Wrap = False End Sub
'设置表的风格 Private Sub SetTableStyle(ByRef Table1 As Table) '其父是透明的则得到的是其父的父的颜色 'Table1.BorderColor = Me.BackColor ' System.Drawing.Color.White ' .Blue Table1.Attributes("style") = "Z-INDEX:999;BACKGROUND-COLOR:" + Me.GetColorValue(Me.BackColor) Table1.Attributes("onmouseover") = "this.style.cursor='hand'" ' Table1.Page.Application. Table1.BorderColor = Me.BorderColor Table1.BorderStyle = Me.BorderStyle Table1.BorderWidth = Unit.Pixel(2) Table1.Enabled = Me.Enabled Table1.EnableViewState = Me.EnableViewState Table1.TabIndex = Me.TabIndex Table1.ToolTip = Me.ToolTip Table1.Visible = Me.Visible Table1.CellPadding = 0 Table1.CellSpacing = 0 End Sub
'生成表格 (row*col) Private Function CreateTable(ByVal row As Int16, ByVal col As Int16) As Table Dim Table1 As New Table() Table1.BackImageUrl = Me.BackGround Dim i, j As Int16 For i = 0 To row - 1 Dim r As New TableRow() r = Me.CreateTableRow(col) Table1.Rows.Add(r) Next Table1.Attributes("onmouseover") = "this.style.cursor='hand'" Return Table1 End Function
'建立菜单项 Private Function CreateMenuItem(ByVal item As MenuNode) As TableRow Dim TableRow1 As New TableRow() TableRow1 = Me.CreateTableRow(3) Select Case Me._Type Case 0 To 2 'image TableRow1.Cells(0).BackColor = IconBackColor ' TableRow1.Cells(0).Text = "√" ' Case 1 'check Case 3 ' End Select
'填充数据 '第一列 '是否有图片,有则生成图片对象,否则生成Label代替 If item.ImageUrl <> "" Then TableRow1.Cells(0).Controls.Add(Me.CreateImage(item)) End If TableRow1.Cells(0).Width = Unit.Pixel(22)
'第二列 Me.SetCellStyle(TableRow1.Cells(1), item)
'第三列 TableRow1.Cells(2).Width = Unit.Pixel(30) If Me.HasSub(item) Then TableRow1.Cells(2).HorizontalAlign = HorizontalAlign.Right Me.SetCellStyle(TableRow1.Cells(2), item) TableRow1.Cells(2).Text = "▶" End If Return TableRow1 End Function
'是否有热键,返回有热键的文本 Private Function GetHotKeyText(ByVal item As MenuNode) As String Dim text As String text = item.Text If item.AccessKey <> Nothing Then text = text + "(<U>" + item.AccessKey + "</U>)" End If Return text End Function
'建立表行(1行,n列) Private Function CreateTableRow(ByVal n As Int16) As TableRow Dim TableRow1 As New TableRow() Dim i As Int16 For i = 0 To n - 1 Dim c As New TableCell() 'Me.SetCellStyle(c) TableRow1.Cells.Add(c) Next Return TableRow1 End Function
'当前索引放在最后一个位置 Private Function GetSubName(ByVal index As Int16, ByVal Count As Int16) As String Dim i As Int16 Dim Names As String For i = 0 To Count - 1 Names = Names + Me.ID + "_T" + i.ToString + "|" Next Names = Names + Me.ID + "_T" + index.ToString Return Names End Function
'由生成一个图标组件 Private Function CreateImage(ByVal item As MenuNode) As Image Dim Image1 As New Image() Image1.ImageUrl = item.ImageUrl 'Image1.Enabled = item.Enabled 'Image1.Visible = item.Visible Return Image1 End Function
Private Sub TableRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim ID As String = Trim(sender.CommandArgument) 'MessageBox(ID) _SelectIndex = CType(ID, Int16) OnChange(e) End Sub
Private Function HasSub(ByVal item As MenuNode) As Boolean Dim i As Int16 For i = 1 To coll.Count If coll.Item(i).ParentID = item.ID Then Return True End If Next Return False End Function '取得ID的所有子结点 Private Function GetSubNodes(ByVal ID As Int16) As MenuNode() Dim i, j, SubCount As Int16 '计算子菜单数 For i = 1 To coll.Count If coll.Item(i).ParentID = ID Then SubCount = SubCount + 1 End If Next Dim SubMenuNodeS(SubCount) As MenuNode '取出子菜子菜单 If SubCount > 0 Then For i = 1 To coll.Count If coll.Item(i).ParentID = ID Then SubMenuNodeS(j) = coll.Item(i) j = j + 1 End If Next End If Return SubMenuNodeS End Function
'计算子菜单数 Private Function GetSubCount() As Int16 Dim i, j, SubCount As Int16 '计算父菜单数(及面板数) For i = 1 To coll.Count If coll.Item(i).ParentID = 0 Or coll.Item(i).ParentID = Nothing Then SubCount = SubCount + 1 End If Next Return SubCount End Function
Private Function GetSpace(ByVal count As Short) As String Dim TxtSb As New System.Text.StringBuilder() Dim i As Short For i = 0 To count - 1 TxtSb.Append(" ") Next Return TxtSb.ToString End Function
Private Function GetColorValue(ByVal color1 As System.Drawing.Color) As String Dim ColorValue As String Dim r As String = Hex(color1.R) Dim g As String = Hex(color1.G) Dim b As String = Hex(color1.B) If r.Length = 1 Then r = "0" + r End If If g.Length = 1 Then g = "0" + g End If
If b.Length = 1 Then b = "0" + b End If ColorValue = r + g + b Return ColorValue End Function '把结点增加到集合中 Public Sub AddNode(ByVal item As MenuNode) 'Dim Form1 = Me.Parent.FindControl(Me.Parent.ID) '' If Form1 Then 'If Not Form1.IsPostBack Then coll.Add(item) 'End If End Sub Private Sub MessageBox(ByVal Text As String) Dim Txt As String = "<script>alert('" + Text + "');</script>" Me.Page.Response.Write(Txt) End Sub
Protected Overrides Sub Finalize() MyBase.Finalize() End Sub End Class |