Solved

How to get Highlighted Menu Item Text in Visual Basic ?

Posted on 2001-09-17
7
309 Views
Last Modified: 2013-11-25
How to get Highlighted Menu Item in Visual Basic
( I want to Display Menu Item Text in Status Bar - Similar to Visual C++ Menu )

Amit

0
Comment
Question by:amit_panjwani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 300 total points
ID: 6487971
Requires subclassing:

Option Explicit


Private Sub Form_Load()
   Show
'    frmMenuSub.Show
   Call sGetMenuHandles(Me.hWnd)
   gProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf fMenuProc)
End Sub

Private Sub Form_Unload(Cancel As Integer)
   Call SetWindowLong(hWnd, GWL_WNDPROC, gProcOld)
End Sub

Module code:

Option Explicit
Public gProcOld As Long
Public x As Long
Public y As Long

Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any,
ByVal Length As Long)
Public Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private mMenuIDs() As Long
Dim mhMenu As Long
Dim mhSubMenu As Long

Public Const GWL_WNDPROC = (-4)
Public Const WM_MENUSELECT = &H11F


Public Const MF_BYCOMMAND& = &H0&
Public Const MF_BYPOSITION& = &H400&


Public 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


Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal
nIndex As Long, ByVal dwNewLong As Long) As Long
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

Public Function fMenuProc(ByVal hWnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam
As Long) As Long
   Dim str As String
   Dim words(1 To 2) As Integer
   Dim strCaption As String
   Dim lngRet As Long
   If iMsg = WM_MENUSELECT Then
       
       'Stop
       x = wParam
       y = lParam
       
       'frmMenuSub.List1.AddItem Format(x)
       Debug.Print Format(x), y
       
       CopyMemory words(1), x, 4
       
       strCaption = String(255, 0)
       'Low word has MnuID
       If words(1) = -1 Then
           'Head menu
           lngRet = GetMenuString(mhMenu, words(1), strCaption, 255, MF_BYCOMMAND)
       Else
           'Submenu
           lngRet = GetMenuString(mhSubMenu, words(1), strCaption, 255, MF_BYCOMMAND)
       End If
       strCaption = Left$(strCaption, lngRet)
       

       
       
       Select Case strCaption

       Case "bbb"
           str = "BBB"
       Case "ccc"
           str = "CCC"
       Case "ddd"
           str = "DDD"
       End Select




       frmMenu.Label1 = str
   End If
   
   fMenuProc = CallWindowProc(gProcOld, hWnd, iMsg, wParam, lParam)
End Function



Public Sub sGetMenuHandles(hWnd As Long)
   
   mhMenu = GetMenu(hWnd)
   mhSubMenu = GetSubMenu(mhMenu, 0)
       

End Sub

You would have to tailor it to your app.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 6489166
<listening..>
0
 
LVL 4

Author Comment

by:amit_panjwani
ID: 6490873
I will get back as soon as I work out PaulHews proposed solution.

Amit
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 38

Expert Comment

by:PaulHews
ID: 6491005
Let me know if you need any help.
0
 
LVL 4

Author Comment

by:amit_panjwani
ID: 6496217
Thank you  PaulHews,

I could not get time to check code, I will check and get back if I need more help, meanwhile , Thank you for offering soltution.


Amit Panjwani

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6496416
Ok, hopefully I'll still be receiving notifications.  There is a bug in EE that turns off some notifications after an answer is accepted.  My email address is in my profile if you need help and I'm not responding here.
0
 
LVL 4

Author Comment

by:amit_panjwani
ID: 6499570
Sure, Thank you.

Amit Panjwani
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question