Find all our projects in development below.
All source code is GNU General Public License (GPL)
Browsing clsMenuItem.cls (5.15 KB)
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsMenuItem"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
' clsMenuItem: exposes a menu item's properties
'
' Copyright (c) 2008 JaviteSoft ( http://www.javitesoft.com )
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Private Declare Function CheckMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
Private Declare Function GetMenuState Lib "user32" (ByVal hMenu As Long, ByVal wID As Long, ByVal wFlags As Long) As Long
Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Long, ByVal wIDItem As Long, ByVal lpString As String, ByVal nMaxCount As Long, ByVal wFlag As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Const MF_SEPARATOR = &H800&
Private Const MF_CHECKED = &H8&
Private Const MF_UNCHECKED = &H0&
Private Const MF_ENABLED = &H0&
Private Const MF_DISABLED = &H2&
Private Const MF_GRAYED = &H1&
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_COMMAND = &H111
Private m_ParentItem As Object
Private m_hMenu As Long
Friend Function Init(ByVal ParentItem As Object, ByVal hMenu As Long) As clsMenuItem
Set m_ParentItem = ParentItem
m_hMenu = hMenu
Set Init = Me
End Function
Public Function IsValid() As Boolean
IsValid = Not (m_ParentItem.GetChildState(Me) = -1)
End Function
Public Function GetMenuHandle() As Long
GetMenuHandle = m_hMenu
End Function
Public Function GetWindowHandle() As Long
GetWindowHandle = m_ParentItem.GetWindowHandle()
End Function
Public Function GetChildState(ByVal menuChild As clsMenuItem) As Long
GetChildState = GetMenuState(m_hMenu, menuChild.GetMenuHandle(), 0&)
End Function
Public Property Get Text() As String
Dim lpString As String
Dim nLen As Long
lpString = String(250, vbNullChar)
nLen = Len(lpString)
GetMenuString m_ParentItem.GetMenuHandle(), _
m_hMenu, lpString, nLen, 0&
If InStr(lpString, vbNullChar) Then _
lpString = Left(lpString, InStr(lpString, vbNullChar) - 1)
Text = lpString
End Property
Public Property Let Text(ByVal strText As String)
ModifyMenu m_ParentItem.GetMenuHandle(), m_hMenu, _
GetMenuState(m_ParentItem.GetMenuHandle(), m_hMenu, MF_CHECKED Or MF_DISABLED), _
m_hMenu, strText
End Property
Public Property Get Enabled() As Boolean
Enabled = Not (m_ParentItem.GetChildState(Me) And MF_DISABLED)
End Property
Public Property Let Enabled(ByVal bEnabled As Boolean)
EnableMenuItem m_ParentItem.GetMenuHandle(), m_hMenu, IIf(bEnabled, MF_ENABLED, MF_DISABLED Or MF_GRAYED)
End Property
Public Property Get Checked() As Boolean
Checked = m_ParentItem.GetChildState(Me) And MF_CHECKED
End Property
Public Property Let Checked(ByVal bChecked As Boolean)
CheckMenuItem m_ParentItem.GetMenuHandle(), m_hMenu, IIf(bChecked, MF_CHECKED, MF_UNCHECKED)
End Property
Public Property Get IsSeparator() As Boolean
IsSeparator = m_ParentItem.GetChildState(Me) And MF_SEPARATOR
End Property
Public Property Get ChildCount() As Long
ChildCount = GetMenuItemCount(m_hMenu)
End Property
Public Function GetChildMenu(ByVal nPos As Long) As clsMenuItem
Dim menuHandle As Long
Dim newMenu As New clsMenuItem
menuHandle = GetSubMenu(m_hMenu, nPos)
If menuHandle = 0 Then menuHandle = GetMenuItemID(m_hMenu, nPos)
Set GetChildMenu = newMenu.Init(Me, menuHandle)
End Function
Public Function ClickChildItem(ByVal nPos As Long) As Long
ClickChildItem = SendMessage(Me.GetWindowHandle(), WM_COMMAND, _
GetMenuItemID(m_hMenu, nPos), ByVal m_hMenu)
End Function
Public Function ClickChildItem2(ByVal menuChild As clsMenuItem) As Long
ClickChildItem2 = SendMessage(Me.GetWindowHandle(), WM_COMMAND, _
menuChild.GetMenuHandle(), ByVal m_hMenu)
End Function
Public Function ClickItem() As Long
ClickItem = m_ParentItem.ClickChildItem2(Me)
End Function
Public Function BeginChildEnum() As clsMenuEnum
Set BeginChildEnum = New clsMenuEnum
BeginChildEnum.Init Me
End Function
Public Sub EndChildEnum(ByVal objEnum As clsMenuEnum)
Set objEnum = Nothing
End Sub