菜单控制
在菜单上增加图标 声明: DeclareFunctionGetMenuLibuser32?(ByValhwndAsLong)AsLong DeclareFunctionGetSubMenuLibuser32?(ByValhMenuAsLong,ByValnPosAsLong)AsLong DeclareFunctionGetMenuItemIDLibuser32?(ByValhMenuAsLong,ByValnPosAsLong)AsLong Decl
在菜单上增加图标
声明:
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 MENUITE
MINFO
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
clearcase/" target="_blank" >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 中增加一个 PictureBox1, AutoSize 为 True, 放一个小 Bmp (不是 Icon!推荐 13*13)。
Private Sub Command1_Click()
'Get the menuhandle of your app
hMenu& = GetMenu(Form1.hwnd)
'Get the handle of the first submenu (Hello)
hSubMenu& = GetSubMenu(hMenu&, 0)
'Get the menuId of the first entry (Bitmap)
hID& = GetMenuItemID(hSubMenu&, 0)
'Add the bitmap
SetMenuItemBitmaps hMenu&, hID&, MF_BITMAP, Picture1.Picture, Picture1.Picture
'You can add two bitmaps to a menuentry
'One for the checked and one for the unchecked
'state.
End Sub
菜单项的动态装入
菜单项的动态装入是指菜单项的个数不固定. 例子就是文件菜单中最近打开的文件的列表。在第一次打开文件之前,该列表是空的,并且不在文件菜单中出现;打开一个文件后,该列表不再是空的,并且文件菜单中出现代表被打开文件的菜单项。
具体的实现过程如下:
在文件菜单里增加一个菜单项,标题任意,并假设菜单项的Name属性是opened_files_ list;
更改菜单项opened_files_list的可见属性,使 opened_files_list.Visible=False
更改菜单项opened_files_list的下标属性,使 opened_files_list.Index=0
在程序中控制菜单项opened_files_list的动态装入。
假设要显示打开过的第二个文件的文件名,并且该文件名存放在一个文件名数组opened _file_name里。以下的代码就实现了这一功能:
Load opened_files_list(1)
opened_files_list(1).Caption="&2"+opened_file_name(1)
opened_files_list(1).Visible=True
需要注意的是,对于下标为0的菜单项,不能用Load方法。因为在程序执行时,该菜单项就 已经被装入到内存里了;另外,在菜单项的标题属性字符里的"&"字符具有特殊的含义,它的作 用是在显示该属性字符串的同时,并不显示"&"本身,而紧跟"&"的字符在显示时具有下划线, 并且该字符成为热键。
如果下标不为0的菜单项不再需要,为了减少对内存资源的占用,可以用Unload方法把它 从内存卸出;同样地,不能用Unload卸出下标为0的菜单项。
原文转自:http://www.ltesting.net
|