如何在菜单中添加ICO图片??

stickvc 2003-12-13 11:38:44
高手,
如何用最简单方法在菜单中添加ICO图片??请举例啊!!!
...全文
293 点赞 收藏 15
写回复
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
SoHo_Andy 2003-12-15
'-------------------------------------------------
' 让菜单中出现图标一法
'-------------------------------------------------
'-------------------------------------------------
'程序应用三个API函数实现了在菜单项中加入小图标
'GetMenu、GetSubMenu、SetMenuItemBitmaps
'-------------------------------------------------
Option Explicit
'【VB声明】
' Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long

'【说明】
' 取得窗口中一个菜单的句柄

'【返回值】
' Long,依附于指定窗口的一个菜单的句柄(如果有菜单);否则返回零

'【参数表】
' hwnd ----------- Long,窗口句柄。对于vb,这应该是一个窗体句柄。注意可能不是子窗口的句柄
Private Declare Function GetMenu Lib "user32" _
(ByVal hwnd As Long) As Long
'-------------------------------------------------
'【VB声明】
' Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long

'【说明】
' 取得一个弹出式菜单的句柄,它位于菜单中指定的位置

'【返回值】
' Long,位于指定位置的弹出式菜单的句柄(如果有的话);否则返回零

'【参数表】
' hMenu ---------- Long,菜单的句柄

' nPos ----------- Long,条目在菜单中的位置。第一个条目的编号为0
Private Declare Function GetSubMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPos As Long) As Long
'-------------------------------------------------
'【VB声明】
' 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

'【说明】
' 设置一幅特定位图,令其在指定的菜单条目中使用,代替标准的复选符号(√)。位图的大小必须与菜单复选符号的正确大小相符,这个正确大小可以由GetMenuCheckMarkDimensions函数获得

'【返回值】
' Long,非零表示成功,零表示失败。会设置GetLastError

'【备注】
' 使用的位图可能由多个条目共享。一旦不再需要,位图必须由应用程序清除,因为windows不能自动对它进行清除

'【参数表】
' hMenu ---------- Long,菜单句柄

' nPosition ------ Long,欲设置位图的一个菜单条目的标识符。如在wFlags参数中指定了MF_BYCOMMAND,这个参数就代表欲改变的菜单条目的命令ID。如设置的是MF_BYPOSITION,这个参数就代表菜单条目在菜单中的位置(第一个条目的位置为零)

' wFlags --------- Long,常数MF_BYCOMMAND或MF_BYPOSITION,取决于nPosition参数

' hBitmapUnchecked - Long,撤消复选时为菜单条目显示的一幅位图的句柄。如果为零,表示不在未复选状态下显示任何标志

' hBitmapChecked - 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_Bitmap, imgOpen.Picture, imgOpen.Picture)
lRet = SetMenuItemBitmaps(sHandle, 1, MF_Bitmap, imgSave.Picture, imgSave.Picture)
lRet = SetMenuItemBitmaps(sHandle, 2, MF_Bitmap, imgPrint.Picture, imgPrint.Picture)
lRet = SetMenuItemBitmaps(sHandle, 3, MF_Bitmap, imgPrintSetup.Picture, imgPrintSetup.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
回复
stickvc 2003-12-15
我用的也是XP的小图标,我只对一个小分菜单测试!!!!
lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, imOpen.Picture, imOpen.Picture)
不能显示啊
如何是好呢??

回复
aalei 2003-12-15
应该是吧,我用的就是小图标
回复
aalei 2003-12-15
2000下OK!!!!!!!!!!!
“文件”下有 “打开”“保存”----“分割符”(这样才能全部显示)----“打印”“打印设置”
回复
stickvc 2003-12-15
不同图标结果完全不同,
用API函数编程,有一小数图标可显示,
大多数不能显示的????
回复
stickvc 2003-12-15
楼上的高手,
你有无试过啊?
我用的是XP图标但不能显示啊!!!!
回复
aalei 2003-12-15
先在form中添加4个Image1,放上相应的图片
分别是 imOpen,imSave,imSave,imPrintSetup,imCopy
添加2个主菜单,分别为“文件”,“编辑”
“文件”下有 “打开”“保存”“打印”“打印设置”
“编辑”下有 “功能”,“功能”下有“拷贝”


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&

' VB 5 doesn't need this declration, but VB 4 does.
' Private Declare Function VarPtr Lib "VB40032.DLL" (variable As Any) As Long

Private Sub Form_Load()
Dim mHandle As Long, lRet As Long, sHandle As Long, sHandle2 As Long
mHandle = GetMenu(hwnd)
sHandle = GetSubMenu(mHandle, 0)
lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, imOpen.Picture, imOpen.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)
sHandle = GetSubMenu(mHandle, 1)
sHandle2 = GetSubMenu(sHandle, 0)
lRet = SetMenuItemBitmaps(sHandle2, 0, MF_BYPOSITION, imCopy.Picture, imCopy.Picture)
End Sub
回复
stickvc 2003-12-15
上面的高手
我按照你的方法几个API函数来编写,仍不见菜单有图标啊!!!!
请多多教导!!!!!
回复
TT008 2003-12-13
用Toolbar和ImageList
在ImageList中先插入图片
再在Toolbar的属性中加入按钮并选取ImageList中的图片即可
回复
dzb1997 2003-12-13
Option Explicit

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 = GetMenu(hwnd)
sHandle = GetSubMenu(mHandle, 0)
lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, imOpen.Picture, imOpen.Picture)
lRet = SetMenuItemBitmaps(sHandle, 1, MF_BYPOSITION, imSave.Picture, imSave.Picture)
lRet = SetMenuItemBitmaps(sHandle, 3, MF_BYPOSITION, imPrint.Picture, imPrint.Picture)
sHandle = GetSubMenu(mHandle, 1)
sHandle2 = GetSubMenu(sHandle, 0)
End Sub
回复
bacp 2003-12-13
呵呵
我也有一个XP菜单
不要钱
可以而且很好用

你说不要用第三方软件
我好像也有一这个这样的程序
哪一天帮你找找啊
回复
flashboy 2003-12-13
我自己写了个控件,不要钱!要的与我联系
回复
pcwak 2003-12-13
Option Explicit

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 = GetMenu(hwnd)
sHandle = GetSubMenu(mHandle, 0)
lRet = SetMenuItemBitmaps(sHandle, 0, MF_BYPOSITION, imOpen.Picture, imOpen.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)
sHandle = GetSubMenu(mHandle, 1)
sHandle2 = GetSubMenu(sHandle, 0)
lRet = SetMenuItemBitmaps(sHandle2, 0, MF_BYPOSITION, imCopy.Picture, imCopy.Picture)
End Sub
回复
87721054 2003-12-13
第一,他说的控件不是3方控件
第二,图标菜单在网上有很多例子,你自己去找一下,很多的
回复
stickvc 2003-12-13
楼上的高手,
我说的是菜单,并不是工具条啊???
另外,请不要用到第三方的控件,因为其多数要注册破解!11
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告