Adding items to System Menu
Some times it is a nice feature to Add custom menu to the System Menu of an application. Normally .Net framework doesn’t allow to add menu
items in system menu, unless using Windows Win32 API. Here is some code, which uses Win32 API to add and use Menu items to System Menu.
Private Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
End FunctionPrivate Shared Function InsertMenu(ByVal hMenu As IntPtr, ByVal uPosition As Integer, ByVal uFlags As Integer, ByVal uIDNewItem As Integer, ByVal lpNewItem As String) As Boolean
End Function
Public Const WM_SYSCOMMAND As Integer = 274
Private Const MF_SEPARATOR As Integer = 2048
Private Const MF_BYPOSITION As Integer = 1024
Private m_SysMenuHandle As IntPtr
Sub New(ByVal Parent As System.Windows.Forms.Form)
m_SysMenuHandle = GetSystemMenu(Parent.Handle, False)
End Sub
Function AddMenuItem(ByVal MenuText As String, ByVal MenuId As Integer, ByVal Position As Integer) As Integer
If Not m_SysMenuHandle.Equals(IntPtr.Zero) Then
InsertMenu(m_SysMenuHandle, Position, MF_BYPOSITION, MenuId, MenuText)
End If
Return MenuId
End Function
Function AddMenuItem(ByVal MenuText As String, ByVal Position As Integer) As Integer
Dim RandomMenuId As New Random(System.DateTime.Now.Millisecond)
Dim MenuId As Integer = RandomMenuId.Next(1000, 10000)
If Not m_SysMenuHandle.Equals(IntPtr.Zero) Then
Me.AddMenuItem(MenuText, MenuId, Position)
End If
Return MenuId
End Function
Function AddSeparator(ByVal Position As Integer) As Boolean
Return InsertMenu(m_SysMenuHandle, Position, MF_BYPOSITION Or MF_SEPARATOR, 0, String.Empty)
End Function
And you can use the overrideable method “WndProc”, to handle the menu click events
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = SysMenu.WM_SYSCOMMAND Then
If m.WParam.ToInt32.Equals(MenuId) Then
MessageBox.Show("System Menu - Application Demo" & _
Environment.NewLine & Environment.NewLine & "Under GNU GPL", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End If
End Sub
And in the Code you can use this like the following
objSysMenu = New SysMenu(Me)
objSysMenu.AddSeparator(5) 'Adding a separator to the Menu
MenuId = objSysMenu.AddMenuItem("About...", 6) 'Adding Menu Item
About menu item added in System Menu

