I'm making an app for the company I work for and I was wondering how to customise the window's context menu like PuTTY's (aka, it has "New Session..." etc.). I've looked all over Google and can't find the answer I'm looking for.
Asked
Active
Viewed 1,705 times
0
-
1do you mean the system menu ? i mean the one that pops up when you click the window title bar ? – unloco Aug 08 '12 at 23:11
-
when I click the window title bar (The one with "close", etc.) – JohnHoulderUK Aug 08 '12 at 23:20
-
okay i've done that once i'll try to recall the method for you, i remember i used winapi to achieve it – unloco Aug 08 '12 at 23:21
-
possible duplicate of [need help in windows API InsertMenuItem](http://stackoverflow.com/questions/6952343/need-help-in-windows-api-insertmenuitem), [How can I customize the system menu of a Windows Form?](http://stackoverflow.com/questions/4615940/how-can-i-customize-the-system-menu-of-a-windows-form) – Cody Gray - on strike Aug 09 '12 at 01:04
1 Answers
2
make a new module and add Imports System.Runtime.InteropServices
on top
then declare this
<Flags()> _
Public Enum MenuFlags As Integer
MF_BYPOSITION = 1024
MF_REMOVE = 4096
MF_SEPARATOR = 2048
MF_STRING = 0
End Enum
<DllImport("user32.dll", CallingConvention:=CallingConvention.Cdecl)> _
Public Function GetSystemMenu(ByVal hWnd As IntPtr, Optional ByVal bRevert As Boolean = False) As IntPtr
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Function AppendMenu(ByVal hMenu As IntPtr, ByVal uFlags As MenuFlags, ByVal uIDNewItem As Int32, ByVal lpNewItem As String) As Boolean
End Function
then on your form load handler add this code
Dim sysmenu As IntPtr = GetSystemMenu(Me.Handle)
AppendMenu(sysmenu, MenuFlags.MF_STRING, &H1FFF, "Hello")
then, in order to be able to capture the user click on your new menu item, you have to implement this function which will capture all messages, just add it to your form code
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_SYSCOMMAND Then
If m.WParam.ToInt32 = &H1FFF Then
' your menu item is clicked, call a function here
End If
End If
MyBase.WndProc(m)
End Sub

unloco
- 6,928
- 2
- 47
- 58
-
@GtoXic: this code only adds a new item to the bottom of the menu, i'll edit so you get a solution on how to place the menu item wherever you want – unloco Aug 09 '12 at 00:20
-
here http://stackoverflow.com/questions/6952343/need-help-in-windows-api-insertmenuitem – unloco Aug 09 '12 at 00:31