LzmTW.uSystem.uWindows.uForms.uMainForm.Menu

Author:水如烟  

Imports  System.ComponentModel
Imports  System.ComponentModel.Design.Serialization
Imports  System.Reflection
Imports  System.Globalization

Namespace  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu

    
< Serializable(), TypeConverter( GetType (MenuItemConverter)) >  _
    
Public   Class  MenuItem
        
Inherits  uCollection.SinceLink.SinceLinkItemBase( Of   Integer )

        
Private  gToolStripType  As  ToolStripType  =  ToolStripType.ToolStripMenuItem
        
Private  gText  As   String
        
Private  gDeclare  As   String
        
Private  gToolTipText  As   String
        
Private  gShortcutKeys  As  Keys  =  Keys.None
        
Private  gEventAction  As   String
        
Private  gVisible  As   Boolean   =   True
        
Private  gEnabled  As   Boolean   =   True

        
Sub   New ()
        
End Sub

        
Sub   New ( ByVal  code  As   String ByVal  name  As   String )
            
MyBase .New(code, name)
        
End Sub

        
Public   Property  ToolStripType()  As  ToolStripType
            
Get
                
Return  gToolStripType
            
End   Get
            
Set ( ByVal  value  As  ToolStripType)
                gToolStripType 
=  value
            
End   Set
        
End Property

        
Public   Property  Text()  As   String
            
Get
                
Return  gText
            
End   Get
            
Set ( ByVal  value  As   String )
                gText 
=  value
            
End   Set
        
End Property

        
Public   Property  [ Declare ]()  As   String
            
Get
                
Return  gDeclare
            
End   Get
            
Set ( ByVal  value  As   String )
                gDeclare 
=  value
            
End   Set
        
End Property

        
Public   Property  ToolTipText()  As   String
            
Get
                
Return  gToolTipText
            
End   Get
            
Set ( ByVal  value  As   String )
                gToolTipText 
=  value
            
End   Set
        
End Property

        
Public   Property  ShortcutKeys()  As  Keys
            
Get
                
Return  gShortcutKeys
            
End   Get
            
Set ( ByVal  value  As  Keys)
                gShortcutKeys 
=  value
            
End   Set
        
End Property

        
Public   Property  EventAction()  As   String
            
Get
                
Return  gEventAction
            
End   Get
            
Set ( ByVal  value  As   String )
                gEventAction 
=  value
            
End   Set
        
End Property

        
Public   Property  Visible()  As   Boolean
            
Get
                
Return  gVisible
            
End   Get
            
Set ( ByVal  value  As   Boolean )
                gVisible 
=  value
            
End   Set
        
End Property

        
Public   Property  Enabled()  As   Boolean
            
Get
                
Return  gEnabled
            
End   Get
            
Set ( ByVal  value  As   Boolean )
                gEnabled 
=  value
            
End   Set
        
End Property

        
Public   Shared   Function  CreateInstance( _
            
ByVal  code  As   String  _
            , 
ByVal  name  As   String  _
            , 
ByVal  toolstriptype  As  ToolStripType _
            , 
ByVal  text  As   String  _
            , 
ByVal  [ declare As   String  _
            , 
ByVal  tooltiptext  As   String  _
            , 
ByVal  shortcutkeys  As  Keys _
            , 
ByVal  eventaction  As   String  _
            , 
ByVal  visible  As   Boolean  _
            , 
ByVal  enabled  As   Boolean As  MenuItem

            
Dim  mItem  As   New  MenuItem(code, name)

            
With  mItem
                .ToolStripType 
=  toolstriptype
                .Text 
=  text
                .Declare 
=  [ declare ]
                .ToolTipText 
=  tooltiptext
                .ShortcutKeys 
=  shortcutkeys
                .EventAction 
=  eventaction
                .Visible 
=  visible
                .Enabled 
=  enabled
            
End   With

            
Return  mItem
        
End Function

        
Public   Shared   Function  CreateToolStripMenuItem( ByVal  name  As   String ByVal  text  As   String ByVal  tooltipText  As   String As  ToolStripMenuItem
            
Dim  mItem  As   New  MenuItem
            
With  mItem
                .Name 
=  name
                .Text 
=  text
                .ToolTipText 
=  tooltipText
            
End   With
            
Return   CType (MenuItemConverter.ConvertTo(mItem), ToolStripMenuItem)
        
End Function

        
Public   Overrides   Function  ToString()  As   String
            
Return   String .Join( " , " New   String () { _
                
Me .Code _
                , 
Me .Name _
                , 
Me .ToolStripType.ToString _
                , 
Me .Text _
                , 
Me .Declare _
                , 
Me .ToolTipText _
                , 
Me .ShortcutKeys.ToString _
                , 
Me .EventAction _
                , 
Me .Visible.ToString _
                , 
Me .Enabled.ToString})
        
End Function
    
End Class


    
Public   Class  MenuItemConverter
        
Inherits  ExpandableObjectConverter

        
Public   Overloads   Overrides   Function  CanConvertFrom( ByVal  context  As  ITypeDescriptorContext,  ByVal  sourceType  As  Type)  As   Boolean
            
If  sourceType  Is   GetType ( String Then
                
Return   True
            
End   If
            
Return   MyBase .CanConvertFrom(context, sourceType)
        
End Function

        
Public   Overloads   Shared   Function  ConvertFrom( ByVal  toolstripItem  As  ToolStripItem)  As  MenuItem
            
Dim  mItem  As   New  MenuItem

            
If   TypeOf  toolstripItem  Is  ToolStripMenuItem  Then
                mItem.ToolStripType 
=  ToolStripType.ToolStripMenuItem

            
ElseIf   TypeOf  toolstripItem  Is  ToolStripSeparator  Then
                mItem.ToolStripType 
=  ToolStripType.ToolStripSeparator

            
ElseIf   TypeOf  toolstripItem  Is  ToolStripComboBox  Then
                mItem.ToolStripType 
=  ToolStripType.ToolStripComboBox

            
ElseIf   TypeOf  toolstripItem  Is  ToolStripTextBox  Then
                mItem.ToolStripType 
=  ToolStripType.ToolStripTextBox

            
End   If

            
With  toolstripItem
                mItem.Name 
=  MenuFunction.GetShortName(.Name, mItem.ToolStripType)
                mItem.Text 
=  .Text
                mItem.ToolTipText 
=  .ToolTipText
                mItem.Declare 
=  mItem.Text

                
Dim  mIndex  As   Integer   =  mItem.Text.IndexOf( " ( " )
                
If  mIndex  <>   - 1   Then
                    mItem.Declare 
=  mItem.Declare.Substring( 0 , mIndex)
                
End   If
                mItem.Enabled 
=  .Enabled
                
'  mItem.Visible = .Visible 读无效

                
If  mItem.ToolStripType  =  ToolStripType.ToolStripMenuItem  Then
                    mItem.ShortcutKeys 
=   CType (toolstripItem, ToolStripMenuItem).ShortcutKeys
                
End   If

            
End   With

            
Return  mItem
        
End Function

        
Public   Overloads   Overrides   Function  ConvertFrom( ByVal  context  As  ITypeDescriptorContext,  ByVal  culture  As  CultureInfo,  ByVal  value  As   Object As   Object
            
If   TypeOf  value  Is   String   Then
                
Dim  tmpValues  As   String ()  =   CStr (value).Split( New   Char () { " , " c})
                
Return  MenuItem.CreateInstance( _
                  tmpValues(
0 ) _
                , tmpValues(
1 ) _
                , 
CType ([ Enum ].Parse( GetType (ToolStripType), tmpValues( 2 )), ToolStripType) _
                , tmpValues(
3 ) _
                , tmpValues(
4 ) _
                , tmpValues(
5 ) _
                , 
CType ([ Enum ].Parse( GetType (Keys), tmpValues( 6 )), Keys) _
                , tmpValues(
7 ) _
                , 
Boolean .Parse(tmpValues( 8 )) _
                , 
Boolean .Parse(tmpValues( 9 )))
            
End   If

            
Return   MyBase .ConvertFrom(context, culture, value)
        
End Function

        
Public   Overloads   Overrides   Function  ConvertTo( ByVal  context  As  ITypeDescriptorContext,  ByVal  culture  As  CultureInfo,  ByVal  value  As   Object ByVal  destinationType  As  Type)  As   Object
            
If  destinationType  Is   GetType ( String Then
                
Return   CType (value, MenuItem).ToString
            
End   If
            
Return   MyBase .ConvertTo(context, culture, value, destinationType)
        
End Function

        
Public   Overloads   Shared   Function  ConvertTo( ByVal  item  As  MenuItem)  As  ToolStripItem
            
Dim  mToolStripItem  As  ToolStripItem  =   Nothing

            
Select   Case  item.ToolStripType
                
Case  ToolStripType.ToolStripMenuItem
                    mToolStripItem 
=   New  ToolStripMenuItem
                    
CType (mToolStripItem, ToolStripMenuItem).ShortcutKeys  =  item.ShortcutKeys

                
Case  ToolStripType.ToolStripSeparator
                    mToolStripItem 
=   New  ToolStripSeparator

                
Case  ToolStripType.ToolStripComboBox
                    mToolStripItem 
=   New  ToolStripComboBox

                
Case  ToolStripType.ToolStripTextBox
                    mToolStripItem 
=   New  ToolStripTextBox

            
End   Select

            
With  mToolStripItem
                .Name 
=  MenuFunction.GetFullName(item.Name, item.ToolStripType)
                .Text 
=  item.Text
                .ToolTipText 
=  item.ToolTipText
                .Enabled 
=  item.Enabled
                .Visible 
=  item.Visible  ' 写有效
             End   With

            
Return  mToolStripItem
        
End Function
    
End Class

End Namespace

 

Namespace  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu

    
< Flags() >  _
    
Public   Enum  ToolStripType
        ToolStripMenuItem 
=   0
        ToolStripSeparator 
=   1
        ToolStripComboBox 
=   2
        ToolStripTextBox 
=   4
    
End Enum

End Namespace

 

Namespace  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu
    
Public   Class  MenuFunction
        
Private   Sub   New ()
        
End Sub

        
Public   Shared   Function  GetFullName( ByVal  shortName  As   String ByVal  toolstripType  As  ToolStripType)  As   String

            
Dim  mSuffix  As   String   =   Nothing

            
Select   Case  toolstripType
                
Case  Menu.ToolStripType.ToolStripMenuItem
                    mSuffix 
=   " ToolStripMenuItem "

                
Case  Menu.ToolStripType.ToolStripSeparator
                    mSuffix 
=   " ToolStripSeparator "

                
Case  Menu.ToolStripType.ToolStripComboBox
                    mSuffix 
=   " ToolStripComboBox "

                
Case  Menu.ToolStripType.ToolStripTextBox
                    mSuffix 
=   " ToolStripTextBox "

            
End   Select

            
Return   String .Concat(shortName, mSuffix)
        
End Function

        
Public   Shared   Function  GetShortName( ByVal  fullName  As   String ByVal  toolstripType  As  ToolStripType)  As   String
            
Dim  mSuffix  As   String   =   Nothing

            
Select   Case  toolstripType
                
Case  Menu.ToolStripType.ToolStripMenuItem
                    mSuffix 
=   " ToolStripMenuItem "

                
Case  Menu.ToolStripType.ToolStripSeparator
                    mSuffix 
=   " ToolStripSeparator "

                
Case  Menu.ToolStripType.ToolStripComboBox
                    mSuffix 
=   " ToolStripComboBox "

                
Case  Menu.ToolStripType.ToolStripTextBox
                    mSuffix 
=   " ToolStripTextBox "

            
End   Select

            
If   Not  fullName.EndsWith(mSuffix)  Then
                
Throw   New  Exception( " 格式不符,shortName须在前 " )
            
End   If

            
Return  fullName.Replace(mSuffix,  "" )
        
End Function
    
End Class
End Namespace

 

Namespace  LzmTW.uSystem.uWindows.uForms.uMainForm.Menu
    
Public   Class  ToolStripItemInformation
        
Private   Sub   New ()
        
End Sub

        
Public   Shared   Function  FindItems( ByVal  toolStripItemName  As   String ByVal  mainMenu  As  MenuStrip)  As  ToolStripItem()
            
Return  mainMenu.Items.Find(toolStripItemName,  True )
        
End Function

        
Public   Shared   Function  FindItem( ByVal  toolStripItemName  As   String ByVal  mainMenu  As  MenuStrip)  As  ToolStripItem
            
Dim  items  As  ToolStripItem()  =  FindItems(toolStripItemName, mainMenu)
            
If  items.Length  <>   1   Then
                
Return   Nothing
            
Else
                
Return  items( 0 )
            
End   If
        
End Function

        
Public   Shared   Function  GetCollection( ByVal  item  As  ToolStripItem)  As  ToolStripItemCollection
            
If  item.OwnerItem  Is   Nothing   Then
                
Return  item.Owner.Items
            
Else
                
Return   CType (item.OwnerItem, ToolStripDropDownItem).DropDownItems
            
End   If
        
End Function

        
Public   Shared   Function  Index( ByVal  item  As  ToolStripItem)  As   Integer
            
Return  GetCollection(item).IndexOf(item)
        
End Function

        
Public   Shared   Function  NextItem( ByVal  item  As  ToolStripItem)  As  ToolStripItem
            
Dim  mCollection  As  ToolStripItemCollection  =  GetCollection(item)
            
Dim  mIndex  As   Integer   =  mCollection.IndexOf(item)
            
If  mIndex  =  mCollection.Count  -   1   Then
                
Return   Nothing
            
Else
                
Return  mCollection.Item(mIndex  +   1 )
            
End   If
        
End Function

        
Public   Shared   Function  PreviousItem( ByVal  item  As  ToolStripItem)  As  ToolStripItem
            
Dim  mCollection  As  ToolStripItemCollection  =  GetCollection(item)
            
Dim  mIndex  As   Integer   =  mCollection.IndexOf(item)
            
If  mIndex  =   0   Then
                
Return   Nothing
            
Else
                
Return  mCollection.Item(mIndex  -   1 )
            
End   If
        
End Function

        
Public   Shared   Function  FirstItem( ByVal  item  As  ToolStripItem)  As  ToolStripItem
            
Dim  mCollection  As  ToolStripItemCollection  =  GetCollection(item)
            
Return  mCollection.Item( 0 )
        
End Function

        
Public   Shared   Function  LastItem( ByVal  item  As  ToolStripItem)  As  ToolStripItem
            
Dim  mCollection  As  ToolStripItemCollection  =  GetCollection(item)
            
Return  mCollection.Item(mCollection.Count  -   1 )
        
End Function

        
Public   Shared   Function  InsertBefore( ByVal  beforeItem  As  ToolStripItem,  ByVal  item  As  ToolStripItem)  As  ToolStripItem

            
Dim  mCollection  As  ToolStripItemCollection
            
Dim  mIndex  As   Integer

            mCollection 
=  GetCollection(beforeItem)
            mIndex 
=  mCollection.IndexOf(beforeItem)

            mCollection.Insert(mIndex, item)

            
Return  item
        
End Function

        
Public   Shared   Function  AppendAfter( ByVal  afterItem  As  ToolStripItem,  ByVal  item  As  ToolStripItem)  As  ToolStripItem
            
Dim  mCollection  As  ToolStripItemCollection
            
Dim  mIndex  As   Integer

            mCollection 
=  GetCollection(afterItem)
            mIndex 
=  mCollection.IndexOf(afterItem)
            mIndex 
+=   1

            mCollection.Insert(mIndex, item)

            
Return  item
        
End Function

        
Public   Shared   Function  AddParentItem( ByVal  menuStrip  As  MenuStrip,  ByVal  item  As  ToolStripItem)  As  ToolStripItem
            menuStrip.Items.Add(item)
            
Return  item
        
End Function

        
Public   Shared   Function  AddItem( ByVal  parentItem  As  ToolStripItem,  ByVal  item  As  ToolStripItem)  As  ToolStripItem
            
CType (parentItem, ToolStripDropDownItem).DropDownItems.Add(item)
            
Return  item
        
End Function

        
Public   Shared   Sub  Remove( ByVal  item  As  ToolStripItem)
            GetCollection(item).Remove(item)
            item.Dispose()
        
End Sub

        
Public   Shared   Sub  AddClickHandler( ByVal  item  As  ToolStripItem,  ByVal  clickAction  As  EventHandler)
            
AddHandler  item.Click, clickAction
        
End Sub

        
Public   Shared   Sub  RemoveClickHandler( ByVal  item  As  ToolStripItem,  ByVal  clickAction  As  EventHandler)
            
RemoveHandler  item.Click, clickAction
        
End Sub

        
Public   Shared   Sub  AddActionHandler( ByVal  item  As  ToolStripItem,  ByVal  EventName  As   String ByVal  instanceMethodName  As   String ByVal  instance  As   Object )
            uReflection.MemberInfoFunction.AddEventHandler(EventName, item, instanceMethodName, instance)
        
End Sub

        
Public   Shared   Sub  RemoveActionHandler( ByVal  item  As  ToolStripItem,  ByVal  EventName  As   String ByVal  instanceMethodName  As   String ByVal  instance  As   Object )
            uReflection.MemberInfoFunction.RemoveEventHandler(EventName, item, instanceMethodName, instance)
        
End Sub

    
End Class
End Namespace
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值