我有这个mii
变量,它分配一个 MenuItemInfo 结构来设置(或获取)系统菜单的项目。
Private mii As New MenuItemInfo
<System.Runtime.InteropServices.
StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential,
CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Public Structure MENUITEMINFO
Public cbSize As Integer
Public fMask As Integer
Public fType As Integer
Public fState As Integer
Public wID As Integer
Public hSubMenu As IntPtr
Public hbmpChecked As IntPtr
Public hbmpUnchecked As IntPtr
Public dwItemData As IntPtr
Public dwTypeData As String
Public cch As Integer
Public hbmpItem As IntPtr
End Structure
当我尝试使用该变量存储有关使用 API 函数的现有菜单项的信息时,会出现问题GetMenuItemInfo
,该变量没有改变任何内容,所有结构成员仍然为空。
这是 API 函数(请注意Byref
已正确设置)
''' <summary>
''' Gets the handle of the form's system menu.
''' </summary>
''' <param name="hMenu">
''' The handle to the menu that contains the menu item.
''' </param>
''' <param name="uItem">
''' The identifier or position of the menu item to get information about.
''' The meaning of this parameter depends on the value of fByPosition.
''' </param>
''' <param name="fByPosition">
''' The meaning of uItem.
''' If this parameter is FALSE, uItem is a menu item identifier,
''' If this parameter is TRUE, it is a menu item position.
''' </param>
''' <param name="lpmii">
''' A pointer to a MenuItemInfo structure that specifies the information to retrieve.
''' Note that you must set the cbSize member to sizeof(MENUITEMINFO) before calling this function.
''' </param>
<System.Runtime.InteropServices.
DllImport("user32.dll")> _
Private Shared Function GetMenuItemInfo(
ByVal hMenu As IntPtr,
ByVal uItem As UInteger,
ByVal fByPosition As Boolean,
ByRef lpmii As MenuItemInfo) As Boolean
End Function
这就是我试图检索点击的菜单项信息的方式,(请查看里面的评论):
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case &H112 ' WM_SYSCOMMAND
mii = New MenuItemInfo
mii.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(mii)
' NOTE:
' MSDN says that I need to put the dwTypeData to a null value,
' if I want to retrieve a TEXT type item so...
mii.dwTypeData = Nothing
' NOTE:
' m.WParam contains the ID of the clicked Item,
' so I use True to set is an Identifier and not a position.
Dim success? As Boolean = _
GetMenuItemInfo(MenuHandle, m.WParam, True, mii)
MsgBox(success) ' Result: True
MsgBox(mii.wID) ' Result: 0 (empty)
MsgBox(mii.dwTypeData) ' Result: (empty)
MsgBox(m.wParam) ' Result: (The expected item ID)
End Select
' Return control to base message handler.
MyBase.WndProc(m)
End Sub
我该如何解决这个问题?
更新:
现在我的代码看起来像这样,它检索任何项目的 ID,但不检索字符串:
Dim mii As New MenuItemInfo()
mii.cbSize = Marshal.SizeOf(GetType(MenuItemInfo))
mii.fMask = Mask.ID Or Mask.TEXT ' &H40 Or &H2
' mii.fType = ItemType.TEXT ' &H0
Dim success? As Boolean = GetMenuItemInfo(MenuHandle, m.WParam, False, mii)
MsgBox(mii.wID) ' Result: (The Expected ID)
MsgBox(mii.dwTypeData) ' Result: (empty string)