비베닷넷에서 메세지 박스를 띄울 때 타이머를 붙여서 확인 메세지를 클릭하지 않고 메세지 박스를 자동으로 닫는 클래스입니다.
전체 코드. 탭이 먹히지 않아서 보기가 그런데 비쥬얼 스튜디오에서 복사하고 그냥 그대로 붙여넣기 하면 됩니다.
Public Class AutoClosingMessageBox
Private _timeoutTimer As System.Threading.Timer
Private _caption As String
Private Sub New(ByVal text As String, ByVal caption As String, ByVal timeout As Integer)
_caption = caption
_timeoutTimer = New System.Threading.Timer(AddressOf OnTimerElapsed, Nothing, timeout, System.Threading.Timeout.Infinite)
Using _timeoutTimer
MessageBox.Show(text, caption)
End Using
End Sub
Public Shared Sub Show(ByVal text As String, ByVal caption As String, ByVal timeout As Integer)
Dim TempAutoClosingMessageBox As AutoClosingMessageBox = New AutoClosingMessageBox(text, caption, timeout)
End Sub
Private Sub OnTimerElapsed(ByVal state As Object)
Dim mbWnd As IntPtr = FindWindow("#32770", _caption) ' lpClassName is #32770 for MessageBox
If mbWnd <> IntPtr.Zero Then
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
End If
_timeoutTimer.Dispose()
End Sub
Private Const WM_CLOSE As Integer = &H10
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True)> _
Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll", CharSet:=System.Runtime.InteropServices.CharSet.Auto)> _
Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInt32, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function
End Class
사용법
원하는 이벤트에 아래 코드를 추가합니다.
AutoClosingMessageBox.Show("메세지의 내용" & vbNewLine & vbNewLine & "5초후 메세지 박스는 닫힙니다.", "메세지 박스 제목입니다.", 5000)
타이머의 단위는 밀리초입니다.
메세지박스의 확인을 클릭하지 않아도 5초후에 자동으로 없어집니다.
코드 출처
https://stackoverflow.com/questions/14522540/close-a-messagebox-after-several-seconds
위 링크에 가면 C# 으로 되어 있습니다. 본문의 코드는 C# 코드를 vb.net 으로 변환한 것입니다.
'프로그래밍' 카테고리의 다른 글
예약시간에 프로그램 시작, 종료, 모니터끄기 WindowexeTaskOnOff (0) | 2021.03.03 |
---|---|
공인아이피 및 로컬아이피 확인하는 프로그램 (0) | 2021.02.24 |
vb.net 콘솔 FreeConsole AllocConsole 재사용하기 (0) | 2020.06.01 |
vb.net SystemInformation 클래스 사용하기 (0) | 2020.05.30 |
C# 알트탭 눌렀을 때 프로그램 목록에 나오지 않게 하기 (8) | 2020.05.27 |