Microsoft KB Archive/216189

= How To Right Justify the Help Menu Item in Visual Basic =

Article ID: 216189

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q216189



SUMMARY
Many applications (especially older, 16-bit programs) have the Help menu item right-justified on the menu bar. That is, "Help" appears all the way to the right instead of the left, grouped with the other top level menu items. The Visual Basic Menu Editor does not supply this functionality, but it is fairly easy to implement this feature with a few WIN32 API calls, as demonstrated in this example.

NOTE: The use of a right-justified Help menu in an application is not the recommended style for creating Windows applications. This article provides this information without recommending its use.



Step-by-Step Example
 Open a new Standard EXE project. Form1 is created by default. On the Tools menu, click Menu Editor. Create a menu with "Help" as the last top level menu item.  Add the following code to the General Declarations section of Form1:

Option Explicit

Private Type 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 Long End Type

Private Const MF_STRING = &H0& Private Const MF_HELP = &H4000& Private Const MFS_DEFAULT = &H1000&

Private Const MIIM_ID = &H2 Private Const MIIM_SUBMENU = &H4 Private Const MIIM_TYPE = &H10 Private Const MIIM_DATA = &H20

Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetMenuItemInfo Lib "user32" _ Alias "GetMenuItemInfoA" _ (ByVal hMenu As Long, ByVal un As Long, ByVal B As Boolean, _  lpMenuItemInfo As MENUITEMINFO) As Long Private Declare Function SetMenuItemInfo Lib "user32" _ Alias "SetMenuItemInfoA" _ (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, _  lpcMenuItemInfo As MENUITEMINFO) As Long

Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) _ As Long

Private Sub Form_Load Dim mnuItemInfo As MENUITEMINFO, hMenu As Long Dim BuffStr As String * 80  ' Define as largest possible menu text.

hMenu = GetMenu(Me.hwnd)  ' Retrieve the menu handle. BuffStr = Space(80) With mnuItemInfo  ' Initialize UDT .cbSize = Len(mnuItemInfo)  ' 44 .dwTypeData = BuffStr & Chr(0) .fType = MF_STRING .cch = Len(mnuItemInfo.dwTypeData)  ' 80 .fState = MFS_DEFAULT .fMask = MIIM_ID Or MIIM_DATA Or MIIM_TYPE Or MIIM_SUBMENU End With ' Use the desired item's position for the '3' below (zero-based list). If GetMenuItemInfo(hMenu, 3, True, mnuItemInfo) = 0 Then MsgBox "GetMenuItemInfo failed. Error: " & Err.LastDllError,, _ "Error" Else mnuItemInfo.fType = mnuItemInfo.fType Or MF_HELP If SetMenuItemInfo(hMenu, 3, True, mnuItemInfo) = 0 Then MsgBox "SetMenuItemInfo failed. Error: " & Err.LastDllError,, _ "Error" End If  End If   DrawMenuBar (Me.hwnd)   ' Repaint top level Menu End Sub  Run the project. Notice that "Help" is right-justified at the far right-side of the menu bar and is completely functional.

