devxlogo

Provide Status Messages for Menus

Provide Status Messages for Menus

Subclassing a form lets you give a helpful message whenever a user highlights a menu item. Use the Caption property to identify the menu item, then display the help message in a label (lblStatus), which is on the form:

 ' --- Form codePrivate Sub Form_Load()	origWndProc = SetWindowLong(hwnd, GWL_WNDPROC, _		AddressOf AppWndProc)End SubPrivate Sub Form_Resize()	lblStatus.Move 0, ScaleHeight - lblStatus.Height, _		ScaleWidthEnd SubPrivate Sub Form_Unload(Cancel As Integer)	SetWindowLong hwnd, GWL_WNDPROC, origWndProcEnd Sub'--- Module codeType MENUITEMINFO	cbSize As Long	fMask As Long	fType As Long	fState As Long	wID As Long	hSubMenu As Long	hbmpChecked As Long	hbmpUnchecked As Long	dwItemData As Long	dwTypeData As String	cch As LongEnd TypePublic 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 LongPublic Declare Sub CopyMemory Lib "kernel32" Alias _	"RtlMoveMemory" (hpvDest As Any, hpvSource As Any, _	ByVal cbCopy As Long)Public Declare Function GetMenuItemInfo Lib "user32" Alias _	"GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As _	Long, ByVal b As Boolean, lpMenuItemInfo As _	MENUITEMINFO) As LongPublic Declare Function SetWindowLong Lib "user32" Alias _	"SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As _	Long, ByVal dwNewLong As Long) As LongPublic Const GWL_WNDPROC = (-4)Public Const WM_MENUSELECT = &H11FPublic Const MF_SYSMENU = &H2000&Public Const MIIM_TYPE = &H10Public Const MIIM_DATA = &H20Public origWndProc As LongPublic Function AppWndProc(ByVal hwnd As Long, ByVal Msg _	As Long, ByVal wParam As Long, ByVal lParam As Long) _	As Long	Dim iHi As Integer, iLo As Integer	Select Case Msg		Case WM_MENUSELECT			Form1.lblStatus.Caption = ""			CopyMemory iLo, wParam, 2			CopyMemory iHi, ByVal VarPtr(wParam) + 2, 2			If (iHi And MF_SYSMENU) = 0 Then				Dim m As MENUITEMINFO, aCap As String				m.dwTypeData = Space$(64)				m.cbSize = Len(m)				m.cch = 64				m.fMask = MIIM_DATA Or MIIM_TYPE				If GetMenuItemInfo(lParam, CLng(iLo), _					False, m) Then					aCap = m.dwTypeData & Chr$(0)					aCap = Left$(aCap, _						InStr(aCap, Chr$(0)) - 1)					Select Case aCap						Case "&Open": _							Form1.lblStatus.Caption = _							"Open a file"						Case "&Save": _							Form1.lblStatus.Caption = _							"Save a file"					End Select				End If			End If	End Select	AppWndProc = CallWindowProc(origWndProc, hwnd, Msg, _		wParam, lParam)End Function
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist