在上下文菜单中定位菜单项图像(MENUITEMINFO 的 hbmpItem)

Positioning of a menu item image (hbmpItem of a MENUITEMINFO) in a context menu

我正在将一个菜单项插入到主题文本控件的 Outlook 上下文菜单中。

我遇到的问题是菜单项的图像在 Outlook 2010 中的位置很奇怪。在 Outlook 2007 中它的位置不同。看起来菜单项在 Outlook 2010 中占据了选中图像的位置。

这显示了我的菜单项使用以下代码的外观。请注意图片左侧的大 space。

这显示了当我将 MIIM_CHECKMARKS 标志添加到 fMask 并将位图添加到 hbmpUnchecked 指针时的样子。

            Dim bmp As Drawing.Bitmap = My.Resources.olContextMenuIcon
            bmp.MakeTransparent(bmp.GetPixel(10, 10))

            hbitmap = bmp.GetHbitmap

            Dim mii As New NativeMethodsEX.MENUITEMINFO
            With mii
                .cbSize = Marshal.SizeOf(mii)
                .fMask = NativeMethodsEX.MIIM.MIIM_BITMAP Or NativeMethodsEX.MIIM.MIIM_STRING Or NativeMethodsEX.MIIM.MIIM_FTYPE Or NativeMethodsEX.MIIM.MIIM_STATE Or NativeMethodsEX.MIIM.MIIM_ID
                .wID = WM_APP
                .fType = NativeMethodsEX.MFT.MFT_STRING
                .dwTypeData = String.Concat("Wrong Position")
                .fState = NativeMethodsEX.MFS.MFS_ENABLED
                .hbmpItem = hbitmap
            End With

            If ShowTop Then
                NativeMethodsEX.InsertMenuItem(aHwnd, 0, True, mii)
                NativeMethodsEX.InsertMenu(aHwnd, 1, NativeMethodsEX.MFT.MFT_BYPOSITION Or NativeMethodsEX.MFT.MFT_SEPARATOR, Nothing, Nothing)
            Else
                Dim menuItemCount As Integer = NativeMethodsEX.GetMenuItemCount(aHwnd)
                NativeMethodsEX.InsertMenu(aHwnd, menuItemCount, NativeMethodsEX.MFT.MFT_BYPOSITION Or NativeMethodsEX.MFT.MFT_SEPARATOR, Nothing, Nothing)
                NativeMethodsEX.InsertMenuItem(aHwnd, menuItemCount + 1, True, mii)
            End If

            NativeMethodsEX.DrawMenuBar(subjectRegionHwnd)

那么我怎样才能告诉菜单项不要为选中/取消选中图像保留 space?

这个问题我有两个答案。

我在上面指出该问题存在于 Outlook 2010 的菜单中,但不存在于 Outlook 2007 中。事实并非如此。这些 office 版本当然在不同的计算机上,这是 windows 中的显示设置,这是问题的原因。当您在性能选项 > 视觉效果中设置 "Use Visual Styles on Windows and buttons" 关闭时(Win 7),您会得到上面的菜单。如果启用此设置,则菜单的外观和行为会大不相同。

但是,如果用户禁用了此设置,您该如何处理(不确定这是否与 Win10 相关)。

You need to set the menu style through the use of the Menuinfo 特别需要设置标志 MNS_NOCHECK。然后 space 消失了,因为菜单不再需要复选标记。

这个解决方案 can also be seen here 在另一个 Whosebug 答案中。