将项目插入表单系统菜单

时间:2012-02-26 12:06:51

标签: vb.net

我的测试:

Public Class Form1

   Public Const WM_SYSCOMMAND As Int32 = &H112
   Public Const MF_BYPOSITION As Int32 = &H400
   Public Const MYMENU1 As Int32 = 1000
   Public Const MYMENU2 As Int32 = 1001

   Dim hSysMenu As Long


   Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
   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

   Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
      MyBase.WndProc(m)
      If (m.Msg = WM_SYSCOMMAND) Then
         Select Case m.WParam.ToInt32
            Case MYMENU1
               MsgBox("1")
            Case MYMENU2
               MsgBox("2")
         End Select
      End If
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      hSysMenu = GetSystemMenu(Me.Handle, False)
      InsertMenu(hSysMenu, 5, MF_BYPOSITION, MYMENU1, "My Menu 1")
      InsertMenu(hSysMenu, 6, MF_BYPOSITION, MYMENU2, "My Menu 2")

   End Sub
End Class

我收到一个错误:

检测到PInvokeStackImbalance 消息:对PInvoke函数'WindowsApplication1!WindowsApplication1.Form1 :: GetSystemMenu'的调用使堆栈失衡。这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。

1 个答案:

答案 0 :(得分:1)

在声明中将Long替换为Integer