Пример показывает, как получить Caption выбранного элемента меню.
Инструкции
ВНИМАНИЕ: Данный пример довольно опасный, и может привести к зависанию Visual Basic!
Код
'КОД ФОРМЫ:
Option Explicit
Private Sub Form_Load()
gHW = Me.hwnd
Hook
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unhook
End Sub
Private Sub Timer1_Timer()
Me.Caption = SelectedMenuString
End Sub
'КОД МОДУЛЯ
Option Explicit
Public SelectedMenuString As String 'здесь будет заголовок выбранного элемента меню
Private 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
Private Declare Function SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetMenu Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu 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 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 Const MF_BYPOSITION = &H400&
Private Const MF_HILITE = &H80&
Private Const WM_MENUSELECT = &H11F
Private Const GWL_WNDPROC = -4
Public lpPrevWndProc As Long
Public gHW As Long
Public Sub Hook()
'Begin hooking into messages.
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, _
AddressOf WindowProc)
End Sub
Public Sub Unhook()
'Cease hooking into messages.
SetWindowLong gHW, GWL_WNDPROC, lpPrevWndProc
End Sub
Function AnyLit(hSubSubMenu As Long) As Long
Dim i As Long
Dim MenuCount As Long
'Get the number of items in the menu.
MenuCount = GetMenuItemCount(hSubSubMenu)
'Loop through the menu items.
For i = 0 To MenuCount - 1
'Check whether this item is highlighted.
If GetMenuState(hSubSubMenu, i, MF_BYPOSITION) And _
MF_HILITE Then
AnyLit = True
Exit Function
End If
Next i
'Return FALSE, no items highlighted.
AnyLit = False
End Function
Private Sub WalkSubMenu(hSubMenu As Long)
Dim i As Long
Dim MenuItems As Long
Dim hSubSubMenu As Long
Dim buffer As String
Dim result As Long
'Get the count of menu items in this menu.
MenuItems = GetMenuItemCount(hSubMenu)
'Loop through all the items on the menu.
For i = 0 To MenuItems - 1
'Determine whether this item is highlighted.
If GetMenuState(hSubMenu, i, MF_BYPOSITION) And _
MF_HILITE Then
'Attempt to get a submenu for each menu item.
hSubSubMenu = GetSubMenu(hSubMenu, i)
'Check for a submenu with something selected on it.
If hSubSubMenu And AnyLit(hSubSubMenu) Then
'There is a submenu with a selection so walk it.
WalkSubMenu hSubSubMenu
Else
buffer = Space(255)
result = GetMenuString(hSubMenu, i, buffer, Len(buffer), MF_BYPOSITION)
buffer = Left$(buffer, result)
SelectedMenuString = GetDescription(buffer)
Exit Sub
End If
End If
Next i
End Sub
Public Sub FindHilite(TheForm As Form)
Dim hMenu As Long
Dim hSubMenu As Long
Dim i As Long
Dim MenuCount As Long
'Clear any previous description.
SelectedMenuString = ""
'Get the menu handle.
hMenu = GetMenu(TheForm.hwnd)
'Check to see if there is no menu.
If hMenu <> 0 Then
'Get the number of top-level menus.
MenuCount = GetMenuItemCount(hMenu)
'Enumerate through all top-level menus.
For i = 0 To MenuCount - 1
'Ignore top-level menus not currently selected.
If GetMenuState(hMenu, i, MF_BYPOSITION) And _
MF_HILITE Then
'Get a handle to the submenu.
hSubMenu = GetSubMenu(hMenu, i)
'Walk the submenu.
WalkSubMenu hSubMenu
End If
Next i
End If
End Sub
Private Function GetDescription(MenuCaption As String) As String
GetDescription = MenuCaption
End Function
Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If uMsg = WM_MENUSELECT Then
FindHilite Form1
End If
WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)
End Function
Язык Visual Basic 6.0 является устаревшим. Многие примеры, размещенные на нашем сайте, были созданы еще во времена Windows 98 и могут не работать в современных операционных системах.
Если у вас возникнут какие-либо проблемы или вопросы, вы можете обратиться за помощью на наш форум.
Добавлять комментарии могут только зарегистрированные пользователи сайта.
Если у Вас уже есть учетная запись на Kbyte.Ru, пройдите процедуру авторизации.
Если Вы еще не зарегистрированы на Kbyte.Ru - зарегистрируйтесь.