Is it possible to add items to the standard Windows Form menu?

  • Thread starter Thread starter Bob
  • Start date Start date
Martin Robins said:
Care to share?

Public Class Form1
Inherits System.Windows.Forms.Form

Private Declare Function RemoveMenu Lib "user32" ( _
ByVal hMenu As IntPtr, _
ByVal nPosition As Integer, _
ByVal wFlags As Integer) _
As Boolean

Private Declare Function GetSystemMenu Lib "user32" ( _
ByVal hwnd As IntPtr, _
ByVal bRevert As Boolean _
) As IntPtr

Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" ( _
ByVal hMenu As IntPtr, ByVal wFlags As Integer, _
ByVal wIDNewItem As Integer, _
ByVal lpNewItem As String _
) As IntPtr

Private Const WM_SYSCOMMAND As Long = &H112
Private Const MF_BYPOSITION As Integer = &H400

Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles MyBase.Load

Dim hSysMenu As IntPtr = GetSystemMenu(Me.Handle, False)

RemoveMenu(hSysMenu, 6, MF_BYPOSITION)

AppendMenu(hSysMenu, 0, 1000, "New Item1")
AppendMenu(hSysMenu, 0, 1001, "New Item2")

End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
If m.Msg = WM_SYSCOMMAND Then
Select Case m.WParam.ToInt32
Case 1000
MsgBox("Hello1")
Case 1001
MsgBox("Hello2")
End Select
End If
End Sub

End Class
 
Back
Top