Adding Items To the ControlBox Menu

The following code will add an “About” menu item.


Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Long
Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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 Const WM_SYSCOMMAND = &H112
Public Const MF_SEPARATOR = &H800&
Public Const MF_STRING = &H0&
Public Const GWL_WNDPROC = (-4)
Public Const IDM_ABOUT As Long = 1010
Public lProcOld As Long


In your module put:

Public Function SysMenuHandler(ByVal hWnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If iMsg = WM_SYSCOMMAND Then
        If wParam = IDM_ABOUT Then
            MsgBox "Subclassing works"
            Exit Function
        End If
    End If
    'Important to call original message handler!
    'Your app may not be the only subclasser around...
    SysMenuHandler = CallWindowProc(lProcOld, hWnd, iMsg, wParam, lParam)
End Function

In your Form put:

Private Sub Form_Load()
Dim lSysMenu As Long
Dim lRet As Long
    lSysMenu = GetSystemMenu(hWnd, 0&)
    lRet = AppendMenu(lSysMenu, MF_SEPARATOR, 0&, vbNullString)
    lRet = AppendMenu(lSysMenu, MF_STRING, IDM_ABOUT, "About...")
    lProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SysMenuHandler)
End Sub
Private Sub Form_Unload(Cancel As Integer)
    'Set back control to original message handler, or you
    'will experience the mother of all crashes!
    SetWindowLong Me.hWnd, GWL_WNDPROC, lProcOld
End Sub

REMEMBER to not terminate your program with an End statement. You must unload it properly, like above.