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

标签 c# .net vb.net winapi structure

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

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

那么...我如何获得必要的画笔句柄?我看不到 Brush 对象的任何句柄方法...是一个 Brush,这令人困惑。

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

UPDATE

这段代码不起作用,没有改变背景颜色。

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

最佳答案

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

<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 网站从来没有提供有效/有效的示例。

关于c# - 设置和使用 MenuInfo 结构的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19941839/

相关文章:

c# - 如何从 web.config 中的自定义部分读取值

c# - 调用线程无法访问此对象,因为另一个线程拥有它。WPF

c# - 为什么调试器的断点条件允许赋值语句作为 bool 条件?

c# - Telerik TreeView |使用 NodeTemplate 时,NodeExpand 事件提供了错误的节点值

c# - ChrW(e.KeyCode) 的 C# 等价物是什么?

c# - 从 C# 方法返回方法

c# - 发布到 Azure 错误 - 错误 : '' , 十六进制值 0x0F 是无效字符。 1 号线,位置 285

c# - SQL 网络接口(interface),错误 : 26 using Windows Azure

C# 任何 CPU 选项和 VirtualQueryEx

c# - 从 .NET 3.5 更改为 4.5.1 时无法加载文件或程序集 'Interop.Microsoft.Office.Core, Version=2.4.0.0, ...'