Private Sub GetMenuInfo(str As String)
' Str = Left(Str, InStr(1, Str, Chr(0)) - 1)
Dim strMenu As String
Dim n As Long
strMenu = ""
If Not Trim(str) = "" Then
For n = 1 To Len(str) - 1
If Mid(str, n, 1) = "(" Then
strMenu = Mid(str, 1, n - 1)
Exit For
End If
Next n
frmMain.stbStatus.Panels(2).Text = strMenu
End If
End Sub
'判断当前佩选择的菜单项,并在statusbar中显示信息
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
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
Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_MENUSELECT = &H11F
Public Const MF_BYCOMMAND = &H0&
Public Const MF_BYPOSITION = &H400&
'判断当前被选中的菜单项,并显示相应信息
Public Function wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim MenuItemStr As String, SubMenuStr As String
Dim hSubmenu As Long, MenuId As Long, i As Long
Dim tmpl As tLong, tmpt As TwoWord
'以下程式会截取WM_MENUSELECT处理完後,再将之送往原来的Window Procedure
If Msg = WM_MENUSELECT Then
SubMenuStr = String(255, 0)
MenuItemStr = String(255, 0)
hSubmenu = GetSubMenu(lParam, MenuId)
If hSubmenu = 0 Then '表示该item之下没有popupMenu了
Call GetMenuString(lParam, MenuId, MenuItemStr, 256, MF_BYCOMMAND)
MenuItemStr = Left(MenuItemStr, InStr(1, MenuItemStr, Chr(0)) - 1)
GetMenuInfo MenuItemStr
' If Not Trim(MenuItemStr) = "" Then
' frmMain.stbStatus.Panels(2).Text = MenuItemStr
' End If
Else
Call GetMenuString(hMenu, hSubmenu, SubMenuStr, 256, MF_BYCOMMAND)
SubMenuStr = Left(SubMenuStr, InStr(1, SubMenuStr, Chr(0)) - 1)
GetMenuInfo SubMenuStr
' If Not Trim(MenuItemStr) = "" Then
' frmMain.stbStatus.Panels(2).Text = SubMenuStr
' End If
End If
End If
'将之送往原来的Window Procedure
wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
'在statusbar显示当前菜单信息
Private Sub DisplayMenu()
Dim Ret As Long
hMenu = GetMenu(Me.hwnd)
'记录原本的Window Procedure的位址
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
'设定Combo1的window Procedure到wndproc
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf wndproc
End Sub