VB2005 如何添加菜单
1个回答
展开全部
用VB6做这个很麻烦的,要调用API,一个例子:
Private Declare Function GetMenu 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 SetMenuItemBitmaps Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, _
ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
Const MF_BYPOSITION = &H400&
Private Sub Form_Load()
Dim mHandle As Long, lRet As Long, sHandle As Long, sHandle2 As Long
'取得菜单的句柄并赋值给mHandle
mHandle = GetMenu(hwnd)
'取得mHandle句柄所指菜单的第一个弹出式菜单(文件&F)的句柄并赋值给sHandle
sHandle = GetSubMenu(mHandle, 0)
'将弹出式菜单的第0-4项加上图片,为什么跳过2呢?因为2是分割线
lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, imOpen.Picture, imSave.Picture)
lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, imSave.Picture, imSave.Picture)
lRet = SetMenuItemBitmaps(sHandle, 3, MF_BYPOSITION, imPrint.Picture, imPrint.Picture)
lRet = SetMenuItemBitmaps(sHandle, 4, MF_BYPOSITION, imPrintSetup.Picture, imPrintSetup.Picture)
'取得mHandle句柄所指菜单的第二个弹出式菜单(编辑&E)的句柄并赋值给sHandle
sHandle = GetSubMenu(mHandle, 1)
'取得sHandle句柄所指菜单的第一个次级菜单(次级菜单&S)的句柄并赋值给sHandle2
sHandle2 = GetSubMenu(sHandle, 0)
'将次级菜单中的第1项加上图片
lRet = SetMenuItemBitmaps(sHandle2, 0, MF_BYPOSITION, imCopy.Picture, imCopy.Picture)
'提示:在SetMenuItemBitmaps()我们把后两项设为相同的图片,如果设为不同的两张图片会有什么效果呢?
' 原来这两张图片分别表示复选和撤消复选时的状态,你只须在菜单项被点击的函数中加入以下语句:
' Private Sub mnuOpen_Click()
' If mnuOpen.Checked = True Then
' mnuOpen.Checked = False
' Else: mnuOpen.Checked = True
' End If
' End Sub
' 然后在SetMenuItemBitmaps()我们把后两项设为不同的图片即可,有兴趣的话试一试。
End Sub
Private Declare Function GetMenu 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 SetMenuItemBitmaps Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, _
ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
Const MF_BYPOSITION = &H400&
Private Sub Form_Load()
Dim mHandle As Long, lRet As Long, sHandle As Long, sHandle2 As Long
'取得菜单的句柄并赋值给mHandle
mHandle = GetMenu(hwnd)
'取得mHandle句柄所指菜单的第一个弹出式菜单(文件&F)的句柄并赋值给sHandle
sHandle = GetSubMenu(mHandle, 0)
'将弹出式菜单的第0-4项加上图片,为什么跳过2呢?因为2是分割线
lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, imOpen.Picture, imSave.Picture)
lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, imSave.Picture, imSave.Picture)
lRet = SetMenuItemBitmaps(sHandle, 3, MF_BYPOSITION, imPrint.Picture, imPrint.Picture)
lRet = SetMenuItemBitmaps(sHandle, 4, MF_BYPOSITION, imPrintSetup.Picture, imPrintSetup.Picture)
'取得mHandle句柄所指菜单的第二个弹出式菜单(编辑&E)的句柄并赋值给sHandle
sHandle = GetSubMenu(mHandle, 1)
'取得sHandle句柄所指菜单的第一个次级菜单(次级菜单&S)的句柄并赋值给sHandle2
sHandle2 = GetSubMenu(sHandle, 0)
'将次级菜单中的第1项加上图片
lRet = SetMenuItemBitmaps(sHandle2, 0, MF_BYPOSITION, imCopy.Picture, imCopy.Picture)
'提示:在SetMenuItemBitmaps()我们把后两项设为相同的图片,如果设为不同的两张图片会有什么效果呢?
' 原来这两张图片分别表示复选和撤消复选时的状态,你只须在菜单项被点击的函数中加入以下语句:
' Private Sub mnuOpen_Click()
' If mnuOpen.Checked = True Then
' mnuOpen.Checked = False
' Else: mnuOpen.Checked = True
' End If
' End Sub
' 然后在SetMenuItemBitmaps()我们把后两项设为不同的图片即可,有兴趣的话试一试。
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询