Private Sub Command1_Click()MsgBox "ok"
Delay (1)SendKeys "{enter}"End Sub这段程序为什么不能自动消掉MsgBox?

解决方案 »

  1.   

    因为MsgBox 弹出的是模式窗口,类似Form2.Show vbModal这种,他算是按管了程序的消息循环,需要关闭它后,代码才会继续执行.所以你需要换个非模式的,或者自己做个会延时关闭的对话框
      

  2.   

    通过EnumWindows获取Msgbox的Hwnd,然后postmessage消息
      

  3.   

    ' ◆◆◆定时消失的对话框◆◆◆
    ' 新建标准 EXE工程,添加控件 Command1 、Timer1
    ' 编译后运行 .exe 文件!
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                                (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                                (ByVal hWnd As Long, ByVal wMsg As Long, _
                                 ByVal wParam As Long, lParam As Any) As LongPrivate Const MsgTitle$ = "定时消失的对话框"Private Sub Command1_Click()
        Dim msg As String
        If Compiled Then
            With Timer1
                .Interval = 3000
                .Enabled = True
            End With
            msg = "将会消失在 3 秒后。"
        Else
          msg = "将会消失在 3 秒后," & vbCrLf & "必须编译后才能看见效果。"
        End If
        MsgBox msg, 64, MsgTitle
        End SubPrivate Sub Form_Load()
       Timer1.Enabled = False
       Me.Caption = MsgTitle
    End SubPrivate Sub Timer1_Timer()
       Dim hWnd As Long
       Timer1.Enabled = False
       hWnd = FindWindow(vbNullString, MsgTitle)
       If (hWnd) Then Call SendMessage(hWnd, &H10, 0, ByVal 0&)
    End SubPrivate Function Compiled() As Boolean
       On Error GoTo NotCompiled
       Debug.Print 1 / 0
       Compiled = True
    NotCompiled:
    End Function
      

  4.   

    把msgbox做成窗体,方便自由控制
      

  5.   

    使用form代码msgbox,或者启动一个线程,在线程里使用MessageBox函数显示对话框,不过由于线程里没有消息循环,因此你需要使用sleep让对话框停留指定的时间,否则会一闪而过。
      

  6.   

    LZ我教你一招,MsgBox弹出后程序处于挂起等待返回状态,其实你可以使用一个Timer,设置其值为要使MsgBox消失的时间,比如5000,然后在MsgBox弹出的前一句开启这个Timer,这个Timer查找MsgBox的窗口然后关闭,关闭完成后把Timer禁用。
      

  7.   

    如果你不想msgbox中断你的程序,就自己写一个对话框弹出,弹出以后在对话框里调用代码关闭自己.
      

  8.   

    timer控件的事件不受模式对话框的影响,楼主“编译以下程序后测试exe文件,VB IDE中无效。)
    Private Sub Command1_Click()
       Timer1.Enabled = True
       MsgBox "OK"
       Timer1.Enabled = False
    End SubPrivate Sub Form_Load()
       Timer1.Enabled = False
       Timer1.Interval = 1000
    End SubPrivate Sub Timer1_Timer()
       SendKeys "{ENTER}"
    End Sub
      

  9.   

    可以用wscript.shell对象的Popup方法代替Msgbox
      

  10.   

    没有办法。 一种做法就是自己写个窗体frm1模仿这个 msgbox ,然后弹出frm1.
      

  11.   

    一般来说,最简单的方法还是自己模拟一个MsgBox,这样可以避免模式窗体阻塞程序执行的情况。