PopupMeny方法可以指定弹出菜单的位置语法object.PopupMenu menuname, flags, x, y, boldcommand
PopupMenu 方法的语法包含下列部分:部分 描述
object 可选的。一个对象表达式,其值为“应用于”列表中的一个对象。如果省略 object,则带有焦点的 Form 对象缺省为 object。
Menuname 必需的。要显示的弹出式菜单名。指定的菜单必须含有至少一个子菜单。
Flags 可选的。一个数值或常数,按照下列设置中的描述,用以指定弹出式菜单的位置和行为。
X 可选的。指定显示弹出式菜单的 x 坐标。如果该参数省略,则使用鼠标的坐标。
Y 可选的。指定显示弹出式菜单的 y 坐标。如果该参数省略,则使用鼠标的坐标。
boldcommand 可选的。指定弹出式菜单中的菜单控件的名字,用以显示其黑体正文标题。如果该参数省略,则弹出式菜单中没有以黑体字出现的控件。 设置值用于 flag 的设置值有:常数位置 值 描述
vbPopupMenuLeftAlign 0 (缺省值)。弹出式菜单的左边定位于 x。
vbPopupMenuCenterAlign 4 弹出式菜单的于 x 居中位。
vbPopupMenuRightAlign 8 弹出式菜单的右边定位于 x。 常数行为 值 描述
vbPopupMenuLeftButton 0 (缺省值)。 仅当使用鼠标左按钮时, 弹出式菜单中的项目才响应鼠标单击。
vbPopupMenuRightButton 2 不论使用鼠标右按钮还是左按钮, 弹出式菜单中的项目都响应鼠标单击。
PopupMenu 方法的语法包含下列部分:部分 描述
object 可选的。一个对象表达式,其值为“应用于”列表中的一个对象。如果省略 object,则带有焦点的 Form 对象缺省为 object。
Menuname 必需的。要显示的弹出式菜单名。指定的菜单必须含有至少一个子菜单。
Flags 可选的。一个数值或常数,按照下列设置中的描述,用以指定弹出式菜单的位置和行为。
X 可选的。指定显示弹出式菜单的 x 坐标。如果该参数省略,则使用鼠标的坐标。
Y 可选的。指定显示弹出式菜单的 y 坐标。如果该参数省略,则使用鼠标的坐标。
boldcommand 可选的。指定弹出式菜单中的菜单控件的名字,用以显示其黑体正文标题。如果该参数省略,则弹出式菜单中没有以黑体字出现的控件。 设置值用于 flag 的设置值有:常数位置 值 描述
vbPopupMenuLeftAlign 0 (缺省值)。弹出式菜单的左边定位于 x。
vbPopupMenuCenterAlign 4 弹出式菜单的于 x 居中位。
vbPopupMenuRightAlign 8 弹出式菜单的右边定位于 x。 常数行为 值 描述
vbPopupMenuLeftButton 0 (缺省值)。 仅当使用鼠标左按钮时, 弹出式菜单中的项目才响应鼠标单击。
vbPopupMenuRightButton 2 不论使用鼠标右按钮还是左按钮, 弹出式菜单中的项目都响应鼠标单击。
VERSION 5.00
Begin VB.Form frmMain
Caption = "定位弹出式菜单"
ClientHeight = 3300
ClientLeft = 165
ClientTop = 735
ClientWidth = 6300
LinkTopic = "Form1"
ScaleHeight = 3300
ScaleWidth = 6300
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton btnAlign
Caption = "控件下面"
Height = 375
Left = 4800
TabIndex = 3
Top = 240
Width = 1335
End
Begin VB.CommandButton btnRight
Caption = "右边"
Height = 375
Left = 3240
TabIndex = 2
Top = 240
Width = 1335
End
Begin VB.CommandButton btnMiddle
Caption = "中间"
Height = 375
Left = 1680
TabIndex = 1
Top = 240
Width = 1335
End
Begin VB.CommandButton btnLeft
Caption = "左边"
Height = 375
Left = 120
TabIndex = 0
Top = 240
Width = 1335
End
Begin VB.Menu mnuFile
Caption = "File"
Begin VB.Menu mnuAbout
Caption = "About"
End
Begin VB.Menu mnuContactScam
Caption = "Contact Scam"
End
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' Popupmenu's use x/y coords to determine where to place
' the menu. In order to align the menu correctly we must
' determine what those x/y coords are.' to align to the left side of our form we use
' frmMain.ScaleLeft, to align with the right side we use
' frmMain.ScaleWidth, to align center we divide the
' ScaleWidth and ScaleHeight by 2.' Notice that the alignment doesn't change when resizing
' your form.' To align to a button you simply state that the menu's
' x location is equal to btnAlign.Left. Once we have the
' x coord we need to get the y coordinate. We then combine
' btnAlign's Height and Top to get the menu to align along
' the bottom of the button.Private Sub btnAlign_Click()
PopupMenu frmMain.mnuFile, 0, btnAlign.Left, btnAlign.Top + btnAlign.Height
End SubPrivate Sub btnLeft_Click()
PopupMenu frmMain.mnuFile, 0, frmMain.ScaleLeft, frmMain.ScaleHeight / 2
End SubPrivate Sub btnMiddle_Click()
PopupMenu frmMain.mnuFile, 0, frmMain.ScaleWidth / 2, frmMain.ScaleHeight / 2
End SubPrivate Sub btnRight_Click()
PopupMenu frmMain.mnuFile, 0, frmMain.ScaleWidth, frmMain.ScaleHeight / 2
End Sub
PopupMenu frmMain.mnuFile, 0, btnAlign.Left, btnAlign.Top + btnAlign.Height
End Sub
' X 变量和 Y 变量的尺寸。
Dim xloc, yloc '设置 X 变量和 Y 变量到窗体中心。
xloc = ScaleWidth / 2
yloc = ScaleHeight / 2 '显示弹出式菜单。
PopupMenu mnuEdit, vbPopupMenuCenterAlign Or _
vbPopupMenuRightButton, xloc, yloc
End Sub
PopupMenu aa, , Command1.Left + Command1.Width, Command1.Top + Command1.Height
End Sub
在你的窗体中建一菜单,其主菜单为不可见壮态
步聚二
在对象的 右击事件中(当然也可以在其它事件)响应如下代码
Me.PopupMenu MyMenu, , 2400, 585
其中要注意的是 MyMenu 是一级菜单的名字,并且该菜单在设计时为不可见壮态的
2400为其弹出菜单的 x 轴 585是其 y 轴(该代码为vb6中使用)