***********Form1***************** Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu menu_main End If End SubPrivate Sub showf2_Click() Form2.Show 1 End Sub ***********Form2***************** Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu menu123 End If End Sub他说的是这种情况,menu123显示不出来
加一个Timer1,设置:Interval = 1,Enabled = False。form0的代码如下:Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu menu_main End If End SubPrivate Sub sdas_Click() Timer1.Enabled = True End SubPrivate Sub Timer1_Timer() Form1.Show 1 Timer1.Enabled = False End Sub
in form2 Public form1mode As IntegerPrivate Sub menu_Click() form1mode = 1 Form1.ShowEnd Sub in form1 Private Sub Form_Load() Form1.Show form1mode End Sub
一下是copy来的例子,使用API可以在菜单关闭后得到用户选择的选项,从而避免多次打开菜单的问题。'Example submitted by Leondias Frost ([email protected]) 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 Debug.Print ret End Sub
然后又想在form1上点右键再弹出菜单啊?
这个是不可能的啊,有模式的就锁定窗体了,此时的form1没办法获得焦点了,把form2关掉之后form1的弹出菜单仍是可用的...
楼主说的是什么情况?
你是用什么方式弹出菜单的?PopupMenu吗?
是什么事件引起探出窗口的呢?Command1_Click之类的吗?还请多贴一些细节:)
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
PopupMenu menu_main
End If
End SubPrivate Sub showf2_Click()
Form2.Show 1
End Sub
***********Form2*****************
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
PopupMenu menu123
End If
End Sub他说的是这种情况,menu123显示不出来
两个窗口里都有弹出菜单,在form1中使用弹出菜单弹出Form2,然后,form2里的弹出菜单就不能显示,哈哈,我应该说清楚了吧
楼主是不是跟我的一样啊
学习学习……
If Button = 2 Then
PopupMenu menu_main
End If
End SubPrivate Sub sdas_Click()
Timer1.Enabled = True
End SubPrivate Sub Timer1_Timer()
Form1.Show 1
Timer1.Enabled = False
End Sub
Public form1mode As IntegerPrivate Sub menu_Click()
form1mode = 1
Form1.ShowEnd Sub
in form1
Private Sub Form_Load()
Form1.Show form1mode
End Sub
当点击菜单项之后,如果马上弹出form1会造成form0运行暂停,导致菜单不能关闭。而现在把弹出方式改成激活Timer1,而不是马上弹出form1,这样form0就不会被暂停了。
Timer1的响应时间设置成了1(1毫秒),这样Timer1会马上作出反应。但是由于VB的具体运行机制,在VB中某一个操作没有完成之前,一般不会响应更多的其他事件。所以,只有当菜单关闭之后,VB程序空闲了,Timer1才会响应,从而打开form1。
Timer1的代码部分也很简单,就是弹出窗口,然后将自己禁用(避免无限制的弹出)。另外,使用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
Debug.Print ret
End Sub