【问题标题】:Which is the proper way to set and use MenuInfo Structure?设置和使用 MenuInfo 结构的正确方法是什么?
【发布时间】:2013-11-12 23:07:33
【问题描述】:

我有点困惑,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 func 和结构:

<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

【问题讨论】:

  • msdn.microsoft.com/en-us/library/windows/desktop/…(它不是一个对象,它是一个画笔的句柄)
  • 你使用一个WinApi函数,你需要使用同样的方法创建一个Brush引用。 pinvoke.net/search.aspx?search=brush&namespace=[All]
  • 您正在尝试混合几种不同类型的句柄(例如,HBITMAP != HBRUSHGraphics.Bitmap != BITMAP)。如果您解释为什么要在 .NET 代码中使用 WinAPI MENUINFO 结构(专为原始 API 调用而设计),这可能有助于澄清事情。你想在这里完成什么?
  • @Ken White 原因是我正在尝试使用 APIS 管理系统菜单,然后我需要知道如何使用 menuinfo 来更改样式、背景和其他内容,谢谢
  • 请大家看看我的更新,刷子对我不起作用,我做错了什么?

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


【解决方案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 网站从未提供有效/有效的示例。

【讨论】:

    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2018-08-12
    相关资源
    最近更新 更多