I'm at my wits' end... off & on for the past two months I've been trying to append items to a form's system menu. I've done this in VB6 and C++ but I can't seem to do it in VB.Net.
I've tried using AppendMenu and InsertMenu (API calls). Both seem to work fine as long as I only want to insert separators. InsertMenu seems to insert separators regardless of what parameters I pass into it. AppendMenu will work for separators as well but as soon as I try to append a menu item (c/w text) I get a null reference exception.
Here's the code I'm using:
[tt]Public Declare Function GetSystemMenu Lib "user32" Alias "GetSystemMenu" _
(ByVal hwnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
Public Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" _
(ByVal hMenu As IntPtr, ByVal nPosition As Long, ByVal wFlags As Long, _
ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Boolean
Public Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" _
(ByVal hMenu As IntPtr, ByVal wFlags As Long, _
ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Boolean
Public Const MF_BYCOMMAND = &H0&
Public Const MF_BYPOSITION = &H400&
Public Const MF_SEPARATOR = &H800&
Public Const MF_STRING = &H0&
Public Const MF_POPUP = &H10&
'Failing code:
Dim SysMenu As IntPtr = GetSystemMenu(Me.Handle, False)
AppendMenu(SysMenu, MF_SEPARATOR, 0, String.Empty)
AppendMenu(SysMenu, MF_STRING, IDM_ABOUT, "About...")
'InsertMenu(SysMenu, 0, MF_BYPOSITION Or MF_SEPARATOR, 0, String.Empty)
'InsertMenu(SysMenu, 0, MF_BYPOSITION Or MF_STRING, IDM_ABOUT, "About...")
[/tt]
I've tried using AppendMenu and InsertMenu (API calls). Both seem to work fine as long as I only want to insert separators. InsertMenu seems to insert separators regardless of what parameters I pass into it. AppendMenu will work for separators as well but as soon as I try to append a menu item (c/w text) I get a null reference exception.
Here's the code I'm using:
[tt]Public Declare Function GetSystemMenu Lib "user32" Alias "GetSystemMenu" _
(ByVal hwnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
Public Declare Function InsertMenu Lib "user32" Alias "InsertMenuA" _
(ByVal hMenu As IntPtr, ByVal nPosition As Long, ByVal wFlags As Long, _
ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Boolean
Public Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" _
(ByVal hMenu As IntPtr, ByVal wFlags As Long, _
ByVal wIDNewItem As Long, ByVal lpNewItem As String) As Boolean
Public Const MF_BYCOMMAND = &H0&
Public Const MF_BYPOSITION = &H400&
Public Const MF_SEPARATOR = &H800&
Public Const MF_STRING = &H0&
Public Const MF_POPUP = &H10&
'Failing code:
Dim SysMenu As IntPtr = GetSystemMenu(Me.Handle, False)
AppendMenu(SysMenu, MF_SEPARATOR, 0, String.Empty)
AppendMenu(SysMenu, MF_STRING, IDM_ABOUT, "About...")
'InsertMenu(SysMenu, 0, MF_BYPOSITION Or MF_SEPARATOR, 0, String.Empty)
'InsertMenu(SysMenu, 0, MF_BYPOSITION Or MF_STRING, IDM_ABOUT, "About...")
[/tt]