设置和使用MenuInfo结构的正确方法是什么?

时间:2013-11-12 23:07:33

标签: c# .net vb.net winapi structure

我有点困惑,MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/ms647575%28v=vs.85%29.aspx中的menuinfo结构说:

hbrBack
Type: HBRUSH
A handle to the brush to be used for the menu's background.

所以...我怎么能得到那个必要的刷柄?,我看不到刷子对象的任何句柄方法......是一个刷子,这很令人困惑。

而且,我不能只使用位图作为菜单背景?我尝试使用位图句柄,但菜单背景没有改变。

  

更新

此代码不起作用,不会更改背景颜色。

Public Sub SetMenuBackground()

    MenuHandle = GetSystemMenu(form.Handle, False)

    Dim brush As IntPtr =
        CreateSolidBrush(CUInt(ColorTranslator.ToWin32(Color.Red)))

    Dim mi As New MenuInfo
    mi.cbSize = Marshal.SizeOf(GetType(MenuInfo))
    mi.fMask = &H2 ' MIM_BACKGROUND
    mi.hbrBack = brush

    SetMenuInfo(MenuHandle, mi)

End Sub

API函数和结构:

<DllImport("user32.dll")> _
Private Shared Function SetMenuInfo(
        ByVal hmenu As IntPtr,
        <[In]> ByRef lpcmi As MenuInfo
) As Boolean
End Function

Private Structure MenuInfo
    Dim cbSize As Int32
    Dim fMask As Int32
    Dim dwStyle As Int32
    Dim cyMax As Int32
    Dim hbrBack As IntPtr
    Dim dwContextHelpID As Int32
    Dim dwMenuData As Int32
    Public Sub New(ByVal owner As Control)
        cbSize = Marshal.SizeOf(GetType(MENUINFO))
    End Sub
End Structure

1 个答案:

答案 0 :(得分:1)

我改变了结构,现在工作正常

<System.Runtime.InteropServices.
StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential,
CharSet:=System.Runtime.InteropServices.CharSet.Auto)>
Public Structure MenuInfo
    Public cbSize As UInteger
    Public fMask As UInteger
    Public dwStyle As UInteger
    Public cyMax As UInteger
    Public hbrBack As IntPtr
    Public dwContextHelpID As UInteger
    Public dwMenuData As UIntPtr
End Structure

我从中学到了什么?:pinvoke.net网站并没有提供有效/有效的例子。