标题:[原创]VB中API菜單編輯器的實現````
取消只看楼主
学习VB才2天
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1653
专家分:0
注 册:2006-5-4
 问题点数:0 回复次数:1 
[原创]VB中API菜單編輯器的實現````

下面的这段代码将Picture1的图形作为菜单项。
首先建立一个模块,并加入下面的语句。
Declare Function GetMenu Lib "user32" _
(ByVal hwnd As Long) As Long
Declare Function GetSubMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPos As Long) As Long
Declare Function GetMenuItemID Lib "user32" _
(ByVal hMenu As Long, ByVal nPos As Long) As Long
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

Public Const MF_BITMAP = &H4&

Type MENUITEMINFO
cbSize As Long
fMask As Long
fType As Long
fState As Long
wID As Long
hSubMenu As Long
hbmpChecked As Long
hbmpUnchecked As Long
dwItemData As Long
dwTypeData As String
cch As Long
End Type

Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Declare Function GetMenuItemInfo Lib "user32" _
Alias "GetMenuItemInfoA" (ByVal hMenu As Long, _
ByVal un As Long, ByVal b As Boolean, _
lpMenuItemInfo As MENUITEMINFO) As Boolean

Public Const MIIM_ID = &H2
Public Const MIIM_TYPE = &H10
Public Const MFT_STRING = &H0&

form1中 ```


Private Sub Form1_load()

' 获得你的菜单的句柄
hMenu& = GetMenu(Form1.hwnd)

' 获得第一个子菜单的句柄
hSubMenu& = GetSubMenu(hMenu&, 0)

' 获得第一个菜单项
hID& = GetMenuItemID(hSubMenu&, 0)

'加入位图

SetMenuItemBitmaps hMenu&, hID&, MF_BITMAP, _
Picture1.Picture, _
Picture1.Picture
End Sub
上面的代码使菜单项为图片,如果你只希望菜单项的左边有一个小位图,而右边仍为文字。可以先在Picture1绘制图片,在利用Picture1.Print加上文字,然后用Picture1.Picture加入菜单项。





nrbRczPj.rar (14.86 KB) [原创]VB中API菜單編輯器的實現````


[此贴子已经被作者于2007-1-2 20:19:43编辑过]

搜索更多相关主题的帖子: Long API ByVal Lib 
2007-01-02 20:18
学习VB才2天
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1653
专家分:0
注 册:2006-5-4
得分:0 

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_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


[GLOW=255,DeepPink,3]我的免费网盘[/GLOW]
2007-01-02 20:21



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-113788-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.379379 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved