写一个小程序,用VB6随意做下界面,离奇的发现弹出菜单居然没有效果,过程如下
Option Explicit Private Sub Form_MouseUp(Button As Integer,Shift As Integer,X As Single,Y As Single) PopupMenu mnuPopup1 End Sub Private Sub mPopup1_Click() Dim f As New Form2 f.Show vbModal End Sub
创建对应的Form2,同样有弹出菜单mnuPopup2和子菜单项mPopup2,事件代码为:
Option Explicit Private Sub Form_MouseUp(Button As Integer,Y As Single) PopupMenu mnuPopup2 End Sub
这时候会发现,无论在哪个事件调用PopupMenu都无法弹出mnuPopup2菜单,我怀疑过RichEdit,怀疑过ComDlg,N多怀疑
查了下MSDN说明有一句:
在显示弹出式菜单时,调用 PopupMenu 方法后面的代码直到用户或者从菜单中选择了命令(这时,该命令的 Click 事件的代码比 PopupMenu 语句后面的代码先执行)或者取消该菜单时才能执行。此外,每次只能显示一个弹出式菜单,因此,如果已经显示了一个弹出式菜单或打开了一个下拉式菜单时,该方法的其它调用将被忽略。
实践证明,PopupMenu后面的代码是继续执行的,但是多次弹出菜单无效,即弹出一个菜单后的对话框不能再继续弹出对话框
调整后Form1的代码为:
Option Explicit Private Sub Form_MouseUp(Button As Integer,Y As Single) PopupMenu mnuPopup1 End Sub Private Sub mPopup1_Click() Dim f As New Form2 'f.Show vbModal f.Show End Sub
这样mPopup1_Click()响应过程能够返回,再调用PopupMenu也就能弹出菜单了,当然了还有其他的解决办法,比如:
Option Explicit Private Sub Form_MouseUp(Button As Integer,Y As Single) PopupMenu mnuPopup1 End Sub Private Sub mPopup1_Click() 'Dim f As New Form2 ''f.Show vbModal 'f.Show Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Dim f As New Form2 f.Show vbModal End Sub
不过这样做要注意不要Show多个模态窗体,道理也是类似的.
应该就这么多了