在VB中修改菜单背景色的方法

首先设计好你的菜单
然后在窗体上加入一个按钮
代码如下:
Option Explicit
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Private Const MIM_BACKGROUND As Long = &H2
Private Const MIM_APPLYTOSUBMENUS As Long = &H80000000

Private Type MENUINFO
    cbSize As Long
    fMask As Long
    dwStyle As Long
    cyMax As Long
    hbrBack As Long
    dwContextHelpID As Long
    dwMenuData As Long
End Type

Private Declare Function DrawMenuBar Lib "user32" _
    (ByVal hWnd As Long) As Long

Private Declare Function GetSubMenu Lib "user32" _
    (ByVal hMenu As Long, ByVal nPos As Long) As Long

Private Declare Function GetMenu Lib "user32" _
    (ByVal hWnd As Long) As Long

Private Declare Function SetMenuInfo Lib "user32" _
    (ByVal hMenu As Long, _
     mi As MENUINFO) As Long

Private Declare Function CreateSolidBrush Lib "gdi32" _
    (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Sub Command1_Click()
    Dim itemnum As Long
    Dim hMenu As Long
    Dim mi As MENUINFO
    Dim i As Long
    hMenu = GetMenu(Me.hWnd)
    itemnum = GetMenuItemCount(hMenu)
    With mi
        .cbSize = Len(mi)
        .fMask = MIM_BACKGROUND Or MIM_APPLYTOSUBMENUS
        .hbrBack = CreateSolidBrush(vbYellow)
    End With
    SetMenuInfo GetMenu(Me.hWnd), mi  'main menu bar
    DrawMenuBar Me.hWnd
    '其实上面的程序就可以满足你的要求了,不过,加上下面的语句,显得更有意思一些
    For i = 0 To itemnum - 1
        With mi
            .cbSize = Len(mi)
            .fMask = MIM_BACKGROUND Or MIM_APPLYTOSUBMENUS
             Randomize Timer
            .hbrBack = CreateSolidBrush(CLng(&HFFFFFF * Rnd(i)))
        End With
        SetMenuInfo GetSubMenu(hMenu, i), mi
    Next

End Sub

转载于:https://www.cnblogs.com/rainstormmaster/archive/2005/12/26/305000.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值