新建From1(窗体),新建Command1(按钮CommandButton),Timer1(Timer)代码:
'本代码要测试时请先编译成 .exe 再试
'添加 Command1 Timer1
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 Long
Const WM_CLOSE = &H10
Const MsgTitle As String = "Test Message"
Private Sub Command1_Click()
Timer1.Enabled = False
Timer1.Interval = 3000
Timer1.Enabled = True
Dim ret As Long
ret = MsgBox("若您不回应的话,3 秒后此 MsgBox 会自动关闭", 64, MsgTitle)
Timer1.Enabled = False
End Sub
Private Sub Timer1_Timer()
Dim ret As Long
ret = FindWindow(vbNullString, MsgTitle)
Call SendMessage(ret, WM_CLOSE, 0, ByVal 0&)
End Sub