1

I have a code that append menu in the system menu(right click form title bar). How will i add click event in the two menus that i appended? this is the code i use:

Public Class AppendFormSysMenu
    <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Private Shared Function AppendMenu(ByVal hMenu As IntPtr, ByVal uFlags As Int32, ByVal uIDNewItem As IntPtr, ByVal lpNewItem As String) As Boolean
    End Function

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

    Private SYSMENU_SAVE_FORM_ID As Integer = &H1
    Private SYSMENU_RESTORE_FORM_ID As Integer = &H2

    <Flags()>
    Public Enum MenuFlags As Integer
        MF_BYPOSITION = 1024
        MF_REMOVE = 4096
        MF_SEPARATOR = 2048
        MF_STRING = 0
    End Enum

    Public Sub insertSeparator(frm As Form)
        Dim hMenu = GetSystemMenu(frm.Handle, False)
        AppendMenu(hMenu, MenuFlags.MF_SEPARATOR, 0, Nothing)
    End Sub

    Public Sub insertSaveMenu(frm As Form, ByVal strMenuItem As String)
        Dim hMenu = GetSystemMenu(frm.Handle, False)
        AppendMenu(hMenu, MenuFlags.MF_STRING, SYSMENU_SAVE_FORM_ID, strMenuItem)
    End Sub

    Public Sub insertRestoreMenu(frm As Form, ByVal strMenuItem As String)
        Dim hMenu = GetSystemMenu(frm.Handle, False)
        AppendMenu(hMenu, MenuFlags.MF_STRING, SYSMENU_RESTORE_FORM_ID, strMenuItem)
    End Sub

End Class

This is the code i use in the form:

Dim AppendFormMenu As New AppendFormSysMenu
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    AppendFormMenu.insertSeparator(Me)
    AppendFormMenu.insertSaveMenu(Me, "Save Form Position")
    AppendFormMenu.insertRestoreMenu(Me, "Restore Form Position")
End Sub
Ally
  • 467
  • 5
  • 24
Marlofs
  • 33
  • 8
  • 2
    http://stackoverflow.com/questions/1557904/adding-a-custom-context-menu-item-to-windows-form-title-bar – Slai Mar 15 '17 at 11:59
  • Hi Slai, thanks. How can I override WndProc if i'm using an external class? I will use the class in multiple forms. – Marlofs Mar 15 '17 at 12:19
  • not sure, but seems like you might need a separate question for that if no one answers this one – Slai Mar 15 '17 at 13:31

0 Answers0