只有在 VB 调试界面中(Design Time)执行才会有这种 MsgBox 影响 Timer 的情况,编译成 .exe 就不会了。下面是一个简单例子,你编译成 .exe 运行试试看: Option ExplicitPrivate Sub Command1_Click() MsgBox "ok" End SubPrivate Sub Form_Load() Timer1.Interval = 100 Timer1.Enabled = True End SubPrivate Sub Timer1_Timer() Label1 = Format(Now, "yyyy-dd-dd HH:nn:ss") End Sub 甚至可以用 Timer 结束一个未响应的对话框: Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate 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 WM_CLOSE = &H10 Private Const MsgTitle As String = "Test Message"Private Sub Command1_Click() Dim nRet As Long Timer1.Interval = 3000 Timer1.Enabled = True nRet = MsgBox("若您不回应的话,3 秒后此 MsgBox 会自动关闭", 64, MsgTitle) Timer1.Enabled = False End SubPrivate Sub Timer1_Timer() Dim hWnd As Long hWnd = FindWindow(vbNullString, MsgTitle) Call SendMessage(hWnd, WM_CLOSE, 0, ByVal 0&) End Sub 同样的,这个例子也要在编译成 .exe 之后才生效。
Option ExplicitPrivate Sub Command1_Click()
MsgBox "ok"
End SubPrivate Sub Form_Load()
Timer1.Interval = 100
Timer1.Enabled = True
End SubPrivate Sub Timer1_Timer()
Label1 = Format(Now, "yyyy-dd-dd HH:nn:ss")
End Sub
甚至可以用 Timer 结束一个未响应的对话框:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate 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 WM_CLOSE = &H10
Private Const MsgTitle As String = "Test Message"Private Sub Command1_Click()
Dim nRet As Long
Timer1.Interval = 3000
Timer1.Enabled = True
nRet = MsgBox("若您不回应的话,3 秒后此 MsgBox 会自动关闭", 64, MsgTitle)
Timer1.Enabled = False
End SubPrivate Sub Timer1_Timer()
Dim hWnd As Long
hWnd = FindWindow(vbNullString, MsgTitle)
Call SendMessage(hWnd, WM_CLOSE, 0, ByVal 0&)
End Sub
同样的,这个例子也要在编译成 .exe 之后才生效。