- Imports System.Windows.Forms
- Imports System.Reflection
- ''' -----------------------------------------------------------------------------
- ''' Project : CommonTest
- ''' Class : LzmTW.Common.MenuItemClass
- '''
- ''' -----------------------------------------------------------------------------
- ''' <summary>
- ''' 菜单操作
- ''' </summary>
- ''' <remarks>
- ''' </remarks>
- ''' <history>
- ''' [lzmtw] 2005-10-21 Created
- ''' </history>
- ''' -----------------------------------------------------------------------------
- Public Class MenuItemClass
- Private Const FindBinding As BindingFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public
- Private Shared tmpHashtable As New Hashtable
- Private Shared tmpForm As Form
- ''' -----------------------------------------------------------------------------
- ''' <summary>
- ''' 返回主菜单所有菜单项的名称和标题
- ''' </summary>
- ''' <param name="FormIstance">窗体实例</param>
- ''' <param name="MainMenu">主菜单</param>
- ''' <returns>名称和标题列表</returns>
- ''' <remarks>
- ''' 对应于哈希表的键和值
- ''' </remarks>
- ''' <history>
- ''' [lzmtw] 2005-10-21 Created
- ''' </history>
- ''' -----------------------------------------------------------------------------
- Public Shared Function GetMenuItemsNameAndText(ByVal FormIstance As Form, ByVal MainMenu As MainMenu) As Hashtable
- tmpForm = FormIstance
- tmpHashtable.Clear()
- For Each o As MenuItem In MainMenu.MenuItems
- GetMenuItemsNameAndText(o)
- Next
- Return tmpHashtable
- End Function
- Private Shared Sub GetMenuItemsNameAndText(ByVal oMenuItem As MenuItem)
- tmpHashtable.Add(GetMenuItemName(tmpForm, oMenuItem), oMenuItem.Text)
- If oMenuItem.IsParent Then
- For Each m As MenuItem In oMenuItem.MenuItems
- GetMenuItemsNameAndText(m)
- Next
- End If
- End Sub
- ''' -----------------------------------------------------------------------------
- ''' <summary>
- ''' 依据菜单名称返回菜单对象
- ''' </summary>
- ''' <param name="FormIstance">窗体实例</param>
- ''' <param name="MenuItemName">菜单名称</param>
- ''' <returns>菜单</returns>
- ''' <remarks>
- ''' </remarks>
- ''' <history>
- ''' [lzmtw] 2005-10-21 Created
- ''' </history>
- ''' -----------------------------------------------------------------------------
- Public Shared Function GetMenuItemByName(ByVal FormIstance As Form, ByVal MenuItemName As String) As MenuItem
- Dim MenuItemFieldInfo As FieldInfo
- MenuItemFieldInfo = FormIstance.GetType.GetField("_" & MenuItemName, FindBinding)
- If MenuItemFieldInfo Is Nothing Then
- Return Nothing
- Else
- Return CType(MenuItemFieldInfo.GetValue(FormIstance), MenuItem)
- End If
- End Function
- ''' -----------------------------------------------------------------------------
- ''' <summary>
- ''' 给出菜单对象返回菜单名称
- ''' </summary>
- ''' <param name="FormIstance">窗体实例</param>
- ''' <param name="MenuItem">菜单</param>
- ''' <returns>菜单名称</returns>
- ''' <remarks>
- ''' </remarks>
- ''' <history>
- ''' [lzmtw] 2005-10-21 Created
- ''' </history>
- ''' -----------------------------------------------------------------------------
- Public Shared Function GetMenuItemName(ByVal FormIstance As Form, ByVal MenuItem As MenuItem) As String
- Dim MenuItemFieldInfo As FieldInfo
- For Each MenuItemFieldInfo In FormIstance.GetType.GetFields(FindBinding)
- If MenuItemFieldInfo.GetValue(FormIstance) Is MenuItem Then
- Return MenuItemFieldInfo.Name.Substring(1)
- End If
- Next
- Return String.Empty
- End Function
- End Class