rainstormmaster的blog
rainstormmaster的blog

首先设计好你的菜单
然后在窗体上加入一个按钮
代码如下:
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

posted on 2005-12-26 18:10  学剑学诗两不成  阅读(2520)  评论(2编辑  收藏  举报