怎样在vb中使用popupmenu,将多个菜单显示到同一个右键菜单,比如我想将菜单栏的多个菜单:file,edit等
3个回答
展开全部
'我这里有一个以前在网上找的代码,没有加以研究,你看看是否对你有用。
'把下面代码保存为Form1.frm,然后打开文件,运行后在窗体空白处按鼠标右键,即可看到你想看到的效果。
'====Form1.frm====
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 165
ClientTop = 855
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.Label Label1
Caption = "请在窗体内按鼠标右键,会弹出重组后的菜单。"
Height = 375
Left = 120
TabIndex = 0
Top = 120
Width = 4455
End
Begin VB.Menu m1
Caption = "AAA"
Begin VB.Menu m11
Caption = "aaa"
End
Begin VB.Menu m12
Caption = "bbb"
End
Begin VB.Menu m13
Caption = "ccc"
End
End
Begin VB.Menu m2
Caption = "BBB"
Begin VB.Menu m21
Caption = "ddd"
End
Begin VB.Menu m22
Caption = "eee"
End
Begin VB.Menu m23
Caption = "fff"
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
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)
If Button = vbRightButton Then
Dim Pt As POINTAPI
Dim ret As Long
hMenu = CreatePopupMenu()
AppendMenu hMenu, MF_STRING, 1, m13.Caption
AppendMenu hMenu, MF_STRING, 2, m22.Caption
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 m13.Caption
Case 2
MsgBox m22.Caption
End Select
End If
End Sub
'把下面代码保存为Form1.frm,然后打开文件,运行后在窗体空白处按鼠标右键,即可看到你想看到的效果。
'====Form1.frm====
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 165
ClientTop = 855
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.Label Label1
Caption = "请在窗体内按鼠标右键,会弹出重组后的菜单。"
Height = 375
Left = 120
TabIndex = 0
Top = 120
Width = 4455
End
Begin VB.Menu m1
Caption = "AAA"
Begin VB.Menu m11
Caption = "aaa"
End
Begin VB.Menu m12
Caption = "bbb"
End
Begin VB.Menu m13
Caption = "ccc"
End
End
Begin VB.Menu m2
Caption = "BBB"
Begin VB.Menu m21
Caption = "ddd"
End
Begin VB.Menu m22
Caption = "eee"
End
Begin VB.Menu m23
Caption = "fff"
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
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)
If Button = vbRightButton Then
Dim Pt As POINTAPI
Dim ret As Long
hMenu = CreatePopupMenu()
AppendMenu hMenu, MF_STRING, 1, m13.Caption
AppendMenu hMenu, MF_STRING, 2, m22.Caption
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 m13.Caption
Case 2
MsgBox m22.Caption
End Select
End If
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询