Auto-close message box
Imports System.Runtime.InteropServices
Public Class Form1
_
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
_
Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
Public Const WM_CLOSE As Integer = &H10
Private Sub StartKiller()
Dim timer As New Timer()
timer.Interval = 3000 '3秒啓動
AddHandler timer.Tick, AddressOf Timer_Tick
timer.Start()
End Sub
Private Sub KillMessageBox()
'依MessageBox的標題,找出MessageBox的視窗
Dim ptr As IntPtr = FindWindow(Nothing, "TEST")
If ptr <> IntPtr.Zero Then '找到則關閉MessageBox視窗
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
StartKiller()
MessageBox.Show("3秒後自動關閉訊息視窗", "TEST")
End Sub
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) KillMessageBox()
CType(sender, Timer).Stop()
End Sub
End Class
Public Class Form1
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
Public Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
Public Const WM_CLOSE As Integer = &H10
Private Sub StartKiller()
Dim timer As New Timer()
timer.Interval = 3000 '3秒啓動
AddHandler timer.Tick, AddressOf Timer_Tick
timer.Start()
End Sub
Private Sub KillMessageBox()
'依MessageBox的標題,找出MessageBox的視窗
Dim ptr As IntPtr = FindWindow(Nothing, "TEST")
If ptr <> IntPtr.Zero Then '找到則關閉MessageBox視窗
PostMessage(ptr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
StartKiller()
MessageBox.Show("3秒後自動關閉訊息視窗", "TEST")
End Sub
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) KillMessageBox()
CType(sender, Timer).Stop()
End Sub
End Class
留言