Find all our projects in development below.
All source code is GNU General Public License (GPL)
Browsing clsMenuObject.vb (5.53 KB)
Option Explicit On
Public Class clsMenuObject
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Integer, ByVal wIDEnableItem As Integer, ByVal wEnable As Integer) As Integer
Private Declare Function CheckMenuItem Lib "user32" (ByVal hMenu As Integer, ByVal wIDCheckItem As Integer, ByVal wCheck As Integer) As Integer
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Integer) As Integer
Private Declare Function GetMenuState Lib "user32" (ByVal hMenu As Integer, ByVal wID As Integer, ByVal wFlags As Integer) As Integer
Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Integer, ByVal nPos As Integer) As Integer
Private Declare Function GetMenuString Lib "user32" Alias "GetMenuStringA" (ByVal hMenu As Integer, ByVal wIDItem As Integer, ByVal lpString As String, ByVal nMaxCount As Integer, ByVal wFlag As Integer) As Integer
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Integer) As Integer
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer, ByVal wIDNewItem As Integer, ByVal lpString As String) As Integer
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Integer, ByVal nPos As Integer) As Integer
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 Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_COMMAND = &H111
Public ReadOnly hWnd As Integer = 0
Public ReadOnly parentMenu As Integer = 0
Public ReadOnly hMenu As Integer = 0
Public Sub New(ByVal objWindow As clsWindowObject)
Me.New(objWindow.hWnd, GetMenu(objWindow.hWnd))
End Sub
Public Sub New(ByVal hWnd As Integer)
Me.New(hWnd, GetMenu(hWnd))
End Sub
Public Sub New(ByVal hWnd As Integer, ByVal hMenu As Integer)
Me.hWnd = hWnd
Me.hMenu = hMenu
End Sub
Public Sub New(ByVal parentMenu As clsMenuObject, ByVal hMenu As Integer)
Me.New(parentMenu.hWnd, parentMenu.hMenu, hMenu)
End Sub
Public Sub New(ByVal hWnd As Integer, ByVal parentMenu As Integer, ByVal hMenu As Integer)
Me.hWnd = hWnd
Me.parentMenu = parentMenu
Me.hMenu = hMenu
End Sub
Public ReadOnly Property IsValid() As Boolean
Get
Return Not (Me.State = -1)
End Get
End Property
Public Property Text() As String
Get
Dim lpString As String = ""
Dim nLen As Integer = 0
lpString = Strings.Space(255)
nLen = lpString.Length
GetMenuString(parentMenu, hMenu, lpString, nLen, 0)
If lpString.Contains(vbNullChar) Then _
lpString = lpString.Substring(0, lpString.IndexOf(vbNullChar))
Return lpString.Trim
End Get
Set(ByVal value As String)
ModifyMenu(parentMenu, hMenu, Me.State, hMenu, value)
End Set
End Property
Public Property Enabled() As Boolean
Get
Return Not CBool(Me.State And MF_GRAYED)
End Get
Set(ByVal value As Boolean)
EnableMenuItem(parentMenu, hMenu, IIf(value, MF_ENABLED, MF_DISABLED Or MF_GRAYED))
End Set
End Property
Public Property Checked() As Boolean
Get
Return Me.State And MF_CHECKED
End Get
Set(ByVal value As Boolean)
CheckMenuItem(parentMenu, hMenu, IIf(value, MF_CHECKED, MF_UNCHECKED))
End Set
End Property
Public ReadOnly Property IsSeparator() As Boolean
Get
Return Me.State And MF_SEPARATOR
End Get
End Property
Public ReadOnly Property IsTopLevel() As Boolean
Get
Return parentMenu = 0
End Get
End Property
Public ReadOnly Property ChildCount() As Integer
Get
Return GetMenuItemCount(hMenu)
End Get
End Property
Public ReadOnly Property State() As Integer
Get
Return GetMenuState(parentMenu, hMenu, MF_SEPARATOR Or MF_CHECKED Or MF_DISABLED Or MF_GRAYED)
End Get
End Property
Public Function GetChildState(ByVal menuChild As clsMenuObject) As Integer
Return GetMenuState(hMenu, menuChild.hMenu, MF_SEPARATOR Or MF_CHECKED Or MF_DISABLED Or MF_GRAYED)
End Function
Public Function GetChildMenu(ByVal nPos As Integer) As clsMenuObject
Dim menuHandle As Integer = GetSubMenu(hMenu, nPos)
If menuHandle = 0 Then menuHandle = GetMenuItemID(hMenu, nPos)
Return New clsMenuObject(Me, menuHandle)
End Function
Public Overloads Function ClickChildItem(ByVal nPos As Integer) As Integer
Return SendMessage(hWnd, WM_COMMAND, GetMenuItemID(hMenu, nPos), hMenu)
End Function
Public Overloads Function ClickChildItem(ByVal menuChild As clsMenuObject) As Integer
Return SendMessage(hWnd, WM_COMMAND, menuChild.hMenu, hMenu)
End Function
Public Function ClickItem() As Integer
Return SendMessage(hWnd, WM_COMMAND, hMenu, parentMenu)
End Function
End Class