65.9K
CodeProject 正在变化。 阅读更多。
Home

在 VB 的菜单中使用图标!

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.15/5 (13投票s)

2004年4月16日

CPOL

1分钟阅读

viewsIcon

93322

downloadIcon

3956

在 VB 6.0 开发的应用程序的菜单中添加图标。

Sample Image - VBMenuIcon.jpg

引言

我又来了,带来另一个编程技巧。VB(VB 6 及更早版本)不提供对菜单中图标的任何支持。因此,要完成此任务,需要调用 Windows 的原生 API。

目的

一些程序员认为这是不必要的。但用户在大多数情况下会使用工具栏。有趣的是,他们可以将菜单选项与工具栏中的条目关联起来。

为了使应用程序易于使用,它应该尽可能友好,不要要求用户进行复杂的推理。

人们使用计算机来激活您的任务并节省时间。

石头的道路

完成此任务的最佳方法是在可视化的环境中,您可以从可用菜单列表中选择所需的菜单项,并在其他(并行)位置选择所需的图标(由其他工具制作)。

但是,我将教您一种不太友好但功能强大的方法。

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 SetMenuIcon()
On Error Resume Next
Dim hMenu As Long
Dim hSubMenu As Long
Dim Ret As Long
'Get main menu ID
hMenu = GetMenu(hwnd)
'
'MENU FILE
'Get sub menu 0 (File items)
hSubMenu = GetSubMenu(hMenu, 0)
'set bitmap to menu item, by ordinal
Ret = SetMenuItemBitmaps(hSubMenu, 0, MF_BYPOSITION, iNew.Picture, iNew.Picture)
Ret = SetMenuItemBitmaps(hSubMenu, 1, MF_BYPOSITION, iOpen.Picture, iOpen.Picture)
Ret = SetMenuItemBitmaps(hSubMenu, 2, MF_BYPOSITION, iSave.Picture, iSave.Picture)
'Skip the separator (it's 3)
Ret = SetMenuItemBitmaps(hSubMenu, 4, MF_BYPOSITION, iSave.Picture, iSave.Picture)
'Skip the separator (it's 5)
Ret = SetMenuItemBitmaps(hSubMenu, 6, MF_BYPOSITION, iExit.Picture, iExit.Picture)
' The rest of the code was removed to maintain clear.  
' See in the file 'ZIP the complete and functional example.
End Sub

图标可以是 14x15 像素。

如果您有任何疑问或建议,请给我发送电子邮件。

联系方式

电子邮件:willians@bb.com.br
MSN:willian_cpp_br@hotmail.com
ICQ# 89506722
电话:+55 (64) 612-6030
传真:+55 (64) 612-6010

历史

  • 2004 年 4 月 15 日:初始发布
© . All rights reserved.