VB.NET : Realizzare messaggi temporizzati.
Utilizzando l'oggetto MessageBox di System.Windows.Forms si ottengono dei messaggi che rimangono aperti fino a che l'utente non li chiude premendo un tasto.
La classe che riporto di seguito è un esempio di come ottenere dei messaggi che si chiudono automaticamente dopo un certo intervallo di tempo (o quando l'utente li chiude).
Imports System
Imports System.Threading
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Public Class TimingMessageBox
<dllimport("user32.dll")>_
Private Shared Function SendMessage(ByVal hWnd As System.IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
End Function
<dllimport("user32.dll")>_
Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As System.IntPtr
End Function
<dllimport("user32.dll")>_
Private Shared Function FlashWindow(ByVal hWnd As System.IntPtr, ByVal bInvert As Integer) As Long
End Function
Private Const WM_CLOSE As Integer = 16
#Region "Private Members"
Private _ShowThread As System.Threading.Thread = Nothing
Private _HideThread As System.Threading.Thread = Nothing
Private _Caption As String = ""
Private _Message As String = ""
Private _Buttons As MessageBoxButtons
Private _Icons As MessageBoxIcon
Private _MessageOpen As Boolean
Private _Timeout As Integer
Private _BlinkTimeout As Integer = 0
Private _BlinkCount As Integer = 0
Private _Parent As IWin32Window
#End Region
#Region "Public Methods"
Public Sub Show(ByVal Caption As String, ByVal Message As String, ByVal Buttons As MessageBoxButtons, _
ByVal Icons As MessageBoxIcon, ByVal Timeout As Integer, ByVal Parent As IWin32Window)
Me._Caption = Caption
Me._Message = Message
Me._Buttons = Buttons
Me._Icons = Icons
Me._Parent = Parent
Me._Timeout = Timeout
Me._ShowThread = New Thread(New System.Threading.ThreadStart(AddressOf ShowMessage))
Me._ShowThread.IsBackground = False
Me._ShowThread.Start()
Me._HideThread = New Thread(New System.Threading.ThreadStart(AddressOf HideMessage))
Me._HideThread.IsBackground = False
Me._HideThread.Start()
End Sub
Public Sub Show(ByVal Caption As String, ByVal Message As String, ByVal Buttons As MessageBoxButtons, _
ByVal Icons As MessageBoxIcon, ByVal Timeout As Integer, ByVal Parent As IWin32Window, ByVal BlinkInterval As Integer, _
ByVal BlinkCount As Integer)
Me._BlinkCount = BlinkCount
Me._BlinkTimeout = BlinkInterval
Show(Caption, Message, Buttons, Icons, Timeout, Parent)
End Sub
#End Region
#Region "Private Methods"
Private Sub ShowMessage()
Me._MessageOpen = True
MessageBox.Show(_Parent, _Message, _Caption, _Buttons, _Icons)
Me._MessageOpen = False
End Sub
Private Sub HideMessage()
System.Threading.Thread.Sleep(Me._Timeout)
If Me._MessageOpen Then
Dim hwnd As System.IntPtr = FindWindow(Nothing, _Caption)
If hwnd.ToInt64 <> IntPtr.Zero.ToInt64 Then
If Me._BlinkTimeout > 0 Then
For i As Integer = 0 To _BlinkCount
FlashWindow(hwnd, 1)
Thread.Sleep(_BlinkTimeout)
Next
End If
SendMessage(hwnd, WM_CLOSE, 0, 0)
End If
End If
Me._HideThread = Nothing
Me._ShowThread = Nothing
End Sub
#End Region
End Class
Per visualizzare un messaggio temporizzato è sufficiente creare un oggetto di classe TimingMessageBox ed invocare uno dei metodi Show().
Nessun commento:
Posta un commento