如果用api,这样实现弹出式菜单:Const MF_CHECKED = &H8& Const MF_APPEND = &H100& Const TPM_LEFTALIGN = &H0& Const MF_DISABLED = &H2& Const MF_GRAYED = &H1& Const MF_SEPARATOR = &H800& Const MF_STRING = &H0& Const TPM_RETURNCMD = &H100& Const TPM_RIGHTBUTTON = &H2& Private Type POINTAPI x As Long y As Long End Type Private Declare Function CreatePopupMenu Lib "user32" () As Long Private Declare Function TrackPopupMenuEx Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal HWnd As Long, ByVal lptpm As Any) As Long Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Dim hMenu As Long Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Dim Pt As POINTAPI Dim ret As Long hMenu = CreatePopupMenu() AppendMenu hMenu, MF_STRING, 1, "Hello !" AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, 2, "Testing ..." AppendMenu hMenu, MF_SEPARATOR, 3, ByVal 0& AppendMenu hMenu, MF_CHECKED, 4, "TrackPopupMenu" GetCursorPos Pt ret = TrackPopupMenuEx(hMenu, TPM_LEFTALIGN Or TPM_RETURNCMD Or TPM_RIGHTBUTTON, Pt.x, Pt.y, Me.HWnd, ByVal 0&) DestroyMenu hMenu Select Case ret Case 1 MsgBox "Hello !" Case 4 MsgBox "TrackPopupMenu ..." End Select End Sub
API中有很多关于菜单的函数:CreatePopupMenu VB声明 Declare Function CreatePopupMenu Lib "user32" Alias "CreatePopupMenu" () As Long 说明 创建一个空的弹出式菜单。可用AppendMenu或InsertMenu函数在窗口中添加条目,或者为一个现成的菜单添加弹出式菜单,并在新建的菜单中添加条目 返回值 Long,如成功,返回一个菜单句柄;零意味着错误 注解 并不推荐用这个函数来创建备用的vb菜单,除非是为TrackPopupMenu函数生成菜单。这个窗口中使用的命令ID必须与现有vb菜单控件的ID相符。或者用一个子类处理控件进行管理 InsertMenuItem VB声明 Declare Function InsertMenuItem Lib "user32" Alias "InsertMenuItemA" (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, ByVal lpcMenuItemInfo As MENUITEMINFO) As Long 说明 用一个MENUITEMINFO结构指定的特征插入一个新菜单条目 返回值 Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError 参数表 参数 类型及说明 hMenu Long,菜单的句柄 un Long,菜单条目的菜单ID。新条目会插入由这个参数指定的项目之前 bool Boolean,如un指定的是条目的位置,就为TRUE,如指定的是菜单ID,就为FALSE lpcMenuItemInfo MENUITEMINFO,用于设置指定菜单条目的特征 GetMenuItemInfo VB声明 Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Long 说明 用一个MENUITEMINFO结构取得(接收)与一个菜单条目有关的特定信息 返回值 Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError 参数表 参数 类型及说明 hMenu Long,菜单的句柄 un Long,菜单条目的菜单ID或位置 b Boolean,如un指定的是条目位置,就为TRUE;如指定的是一个菜单ID,则为FALSE lpMenuItemInfo MENUITEMINFO,这个结构用于装载请求的信息 SetMenuItemInfo VB声明 Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, lpcMenuItemInfo As MENUITEMINFO) As Long 说明 为一个菜单条目设置指定的信息,具体信息保存于MENUITEMINFO结构中 返回值 Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError 参数表 参数 类型及说明 hMenu Long,菜单句柄 un Long,菜单条目的菜单ID或位置 bool Boolean,如un指定了条目的位置,就为TRUE;如指定的是菜单ID,就为FALSE lpcMenuItemInfo MENUITEMINFO,用于设置目标菜单条目的特征 MENUITEMINFO 类型定义 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 Long cch As Long End Type 说明 这个结构包含了菜单条目的信息,不支持win nt 3.51(原文:This structure contains information about a menu entry. Not supported on NT 3.51)
字段表 字段 类型及说明 cbSize Long,结构大小,通常为44bytes(Size of this structure, currently at 44 bytes.) fMask Long,Specifies the information to set or get. Any combination of the following MIIM_CHECKMARKS Sets or gets the hbmpChecked and hbmpUnchecked fields MIIM_DATA Sets or gets the dwItemData field MIIM_ID ets or gets the wID field MIIM_STATE Sets or gets the fState field MIIM_SUBMENU Sets or gets the hSubMenu field MIIM_TYPE Sets or gets the fType and dwTypeData fields fType Long,Menu item type, any combination of the following with the exception that MFT_BITMAP, MFT_SEPARATOR, and MFT_STRING cannot be combined with one another:MFT_BITMAP: Displays the menu item using a bitmap. dwTypeData contains the bitmap handle.MFT_MENUBARBREAK: The menu item is placed on a new line for top level menus, a new column for popup menus. Places a line between the columns.MFT_MENUBREAK: Same as MFT_MENUBREAK without the vertical line.MFT_OWNERDRAW: The menu item is an owner-draw menu.MFT_RADIOCHECK: Uses a radio button (option button) bullet to indicate the checked state instead of a check . Applies if no custom bitmap is specified.MFT_RIGHTJUSTIFY: Right-justifies a top level menu item.MFT_SEPARATOR: The entry is a separator line in a pop-up menu.MFT_STRING: The entry contains a string. dwTypeData contains the address of the string, and the cch field contains the length of the string. fState Long,Current menu entry state or action to take. May be any combination of the following:MFS_CHECKED: Entry is checked.MFS_DEFAULT: The entry is a default item (appears in bold).MFS_DISABLED: Entry is disabled.MFS_ENABLED: Entry is enabled.MFS_GRAYED: Entry is grayed and disabled.MFS_HILITE: Entry is highlighted.MFS_UNCHECKED: Entry is unchecked.MFS_UNHILITE: Entry is unhighlighted. wID Long,Menu entry identifier. The high 16 bits are not used. hSubMenu Long,Handle to a pop-up menu if one is associated with the menu entry hbmpChecked Long,Handle to a bitmap to display for a menu entry when checked. Zero to use the default hbmpUnchecked Long,Handle to a bitmap to display for a menu entry when unchecked. Zero to use the default dwItemData Long,User-defined value associated with this entry. dwTypeData Long,Depends on the menu type cch Long,Length of the menu string when MFT_STRING is specified. Zero for other menu types.
http://www.csdn.net/cnshare/shtm/100.shtm再去这看看
先在固定菜單編輯器中加一固定的比如叫New 索引=0 不可見Visible=false if rs.recordcount>0 then rs.movefirst i=1 do until rs.eof Load fmainform.New(i) fmainform.New(i).Caption = rs.fields(0).value 'fmainform為主窗口 fmainform.New(i).Visible = True i=i+1 rs.movenext loop end if
还可以考虑用 load menu方式建立一个二级菜单mnu,加个索引为0 For i = 1 To 3 Load mnu(i) Next然后在MouseDown事件中: popupmenu 一级菜单名
Const MF_APPEND = &H100&
Const TPM_LEFTALIGN = &H0&
Const MF_DISABLED = &H2&
Const MF_GRAYED = &H1&
Const MF_SEPARATOR = &H800&
Const MF_STRING = &H0&
Const TPM_RETURNCMD = &H100&
Const TPM_RIGHTBUTTON = &H2&
Private Type POINTAPI
x As Long
y As Long
End Type
Private Declare Function CreatePopupMenu Lib "user32" () As Long
Private Declare Function TrackPopupMenuEx Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal HWnd As Long, ByVal lptpm As Any) As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Dim hMenu As Long
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim Pt As POINTAPI
Dim ret As Long
hMenu = CreatePopupMenu()
AppendMenu hMenu, MF_STRING, 1, "Hello !"
AppendMenu hMenu, MF_GRAYED Or MF_DISABLED, 2, "Testing ..."
AppendMenu hMenu, MF_SEPARATOR, 3, ByVal 0&
AppendMenu hMenu, MF_CHECKED, 4, "TrackPopupMenu"
GetCursorPos Pt
ret = TrackPopupMenuEx(hMenu, TPM_LEFTALIGN Or TPM_RETURNCMD Or TPM_RIGHTBUTTON, Pt.x, Pt.y, Me.HWnd, ByVal 0&)
DestroyMenu hMenu
Select Case ret
Case 1
MsgBox "Hello !"
Case 4
MsgBox "TrackPopupMenu ..."
End Select
End Sub
Declare Function CreatePopupMenu Lib "user32" Alias "CreatePopupMenu" () As Long
说明
创建一个空的弹出式菜单。可用AppendMenu或InsertMenu函数在窗口中添加条目,或者为一个现成的菜单添加弹出式菜单,并在新建的菜单中添加条目
返回值
Long,如成功,返回一个菜单句柄;零意味着错误
注解
并不推荐用这个函数来创建备用的vb菜单,除非是为TrackPopupMenu函数生成菜单。这个窗口中使用的命令ID必须与现有vb菜单控件的ID相符。或者用一个子类处理控件进行管理
InsertMenuItem VB声明
Declare Function InsertMenuItem Lib "user32" Alias "InsertMenuItemA" (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, ByVal lpcMenuItemInfo As MENUITEMINFO) As Long
说明
用一个MENUITEMINFO结构指定的特征插入一个新菜单条目
返回值
Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError
参数表
参数 类型及说明
hMenu Long,菜单的句柄
un Long,菜单条目的菜单ID。新条目会插入由这个参数指定的项目之前
bool Boolean,如un指定的是条目的位置,就为TRUE,如指定的是菜单ID,就为FALSE
lpcMenuItemInfo MENUITEMINFO,用于设置指定菜单条目的特征 GetMenuItemInfo VB声明
Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Long
说明
用一个MENUITEMINFO结构取得(接收)与一个菜单条目有关的特定信息
返回值
Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError
参数表
参数 类型及说明
hMenu Long,菜单的句柄
un Long,菜单条目的菜单ID或位置
b Boolean,如un指定的是条目位置,就为TRUE;如指定的是一个菜单ID,则为FALSE
lpMenuItemInfo MENUITEMINFO,这个结构用于装载请求的信息 SetMenuItemInfo VB声明
Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, lpcMenuItemInfo As MENUITEMINFO) As Long
说明
为一个菜单条目设置指定的信息,具体信息保存于MENUITEMINFO结构中
返回值
Long,TRUE(非零)表示成功,否则返回零。会设置GetLastError
参数表
参数 类型及说明
hMenu Long,菜单句柄
un Long,菜单条目的菜单ID或位置
bool Boolean,如un指定了条目的位置,就为TRUE;如指定的是菜单ID,就为FALSE
lpcMenuItemInfo MENUITEMINFO,用于设置目标菜单条目的特征
MENUITEMINFO 类型定义
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 Long
cch As Long
End Type
说明
这个结构包含了菜单条目的信息,不支持win nt 3.51(原文:This structure contains information about a menu entry. Not supported on NT 3.51)
字段表
字段 类型及说明
cbSize Long,结构大小,通常为44bytes(Size of this structure, currently at 44 bytes.)
fMask Long,Specifies the information to set or get. Any combination of the following
MIIM_CHECKMARKS Sets or gets the hbmpChecked and hbmpUnchecked fields
MIIM_DATA Sets or gets the dwItemData field
MIIM_ID ets or gets the wID field
MIIM_STATE Sets or gets the fState field
MIIM_SUBMENU Sets or gets the hSubMenu field
MIIM_TYPE Sets or gets the fType and dwTypeData fields
fType Long,Menu item type, any combination of the following with the exception that MFT_BITMAP, MFT_SEPARATOR, and MFT_STRING cannot be combined with one another:MFT_BITMAP: Displays the menu item using a bitmap. dwTypeData contains the bitmap handle.MFT_MENUBARBREAK: The menu item is placed on a new line for top level menus, a new column for popup menus. Places a line between the columns.MFT_MENUBREAK: Same as MFT_MENUBREAK without the vertical line.MFT_OWNERDRAW: The menu item is an owner-draw menu.MFT_RADIOCHECK: Uses a radio button (option button) bullet to indicate the checked state instead of a check . Applies if no custom bitmap is specified.MFT_RIGHTJUSTIFY: Right-justifies a top level menu item.MFT_SEPARATOR: The entry is a separator line in a pop-up menu.MFT_STRING: The entry contains a string. dwTypeData contains the address of the string, and the cch field contains the length of the string.
fState Long,Current menu entry state or action to take. May be any combination of the following:MFS_CHECKED: Entry is checked.MFS_DEFAULT: The entry is a default item (appears in bold).MFS_DISABLED: Entry is disabled.MFS_ENABLED: Entry is enabled.MFS_GRAYED: Entry is grayed and disabled.MFS_HILITE: Entry is highlighted.MFS_UNCHECKED: Entry is unchecked.MFS_UNHILITE: Entry is unhighlighted.
wID Long,Menu entry identifier. The high 16 bits are not used.
hSubMenu Long,Handle to a pop-up menu if one is associated with the menu entry
hbmpChecked Long,Handle to a bitmap to display for a menu entry when checked. Zero to use the default
hbmpUnchecked Long,Handle to a bitmap to display for a menu entry when unchecked. Zero to use the default
dwItemData Long,User-defined value associated with this entry.
dwTypeData Long,Depends on the menu type
cch Long,Length of the menu string when MFT_STRING is specified. Zero for other menu types.
if rs.recordcount>0 then
rs.movefirst
i=1
do until rs.eof
Load fmainform.New(i)
fmainform.New(i).Caption = rs.fields(0).value 'fmainform為主窗口
fmainform.New(i).Visible = True
i=i+1
rs.movenext
loop
end if
load menu方式建立一个二级菜单mnu,加个索引为0
For i = 1 To 3
Load mnu(i)
Next然后在MouseDown事件中:
popupmenu 一级菜单名