SystemMenu添加单击事件

时间:2017-03-15 11:49:08

标签: vb.net

我有一个代码,用于在系统菜单中添加菜单(右键单击表单标题栏)。如何在我附加的两个菜单中添加click事件?这是我使用的代码:

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

这是我在表单中使用的代码:

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

0 个答案:

没有答案