Solved

How to get Highlighted Menu Item Text in Visual Basic ?

Posted on 2001-09-17
7
295 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
  • 3
  • 3
7 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 300 total points
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
<listening..>
0
 
LVL 4

Author Comment

by:amit_panjwani
Comment Utility
I will get back as soon as I work out PaulHews proposed solution.

Amit
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 38

Expert Comment

by:PaulHews
Comment Utility
Let me know if you need any help.
0
 
LVL 4

Author Comment

by:amit_panjwani
Comment Utility
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
Comment Utility
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
Comment Utility
Sure, Thank you.

Amit Panjwani
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now