 |
|
สอบถามหน่อยครับมีวิธีปิด Auto Close MessageBox ที่แสดง ให้ปิดแบบอัตโนมัติไหมครับ |
|
 |
|
|
 |
 |
|
Code (C#)
private void btnOK_Click(object sender, RoutedEventArgs e)
{
AutoClosingMessageBox.Show("Wrong Input.", "LMS", 5000);
}
public class AutoClosingMessageBox
{
System.Threading.Timer _timeoutTimer;
string _caption;
AutoClosingMessageBox(string text, string caption, int timeout)
{
_caption = caption;
_timeoutTimer = new System.Threading.Timer(OnTimerElapsed,
null, timeout, System.Threading.Timeout.Infinite);
MessageBox.Show(text, caption);
}
public static void Show(string text, string caption, int timeout)
{
new AutoClosingMessageBox(text, caption, timeout);
}
void OnTimerElapsed(object state)
{
IntPtr mbWnd = FindWindow(null, _caption);
if (mbWnd != IntPtr.Zero)
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
_timeoutTimer.Dispose();
}
const int WM_CLOSE = 0x0010;
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
}
|
 |
 |
 |
 |
Date :
2017-08-01 16:33:28 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Private Sub btnOK_Click(sender As Object, e As RoutedEventArgs)
AutoClosingMessageBox.Show("Wrong Input.", "LMS", 5000)
End Sub
Public Class AutoClosingMessageBox
Private _timeoutTimer As System.Threading.Timer
Private _caption As String
Private Sub New(text As String, caption As String, timeout As Integer)
_caption = caption
_timeoutTimer = New System.Threading.Timer(AddressOf OnTimerElapsed, Nothing, timeout, System.Threading.Timeout.Infinite)
MessageBox.Show(text, caption)
End Sub
Public Shared Sub Show(text As String, caption As String, timeout As Integer)
New AutoClosingMessageBox(text, caption, timeout)
End Sub
Private Sub OnTimerElapsed(state As Object)
Dim mbWnd As IntPtr = FindWindow(Nothing, _caption)
If mbWnd <> IntPtr.Zero Then
SendMessage(mbWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero)
End If
_timeoutTimer.Dispose()
End Sub
Const WM_CLOSE As Integer = &H10
<System.Runtime.InteropServices.DllImport("user32.dll", SetLastError := True)> _
Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
<System.Runtime.InteropServices.DllImport("user32.dll", CharSet := System.Runtime.InteropServices.CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As IntPtr, Msg As UInt32, wParam As IntPtr, lParam As IntPtr) As IntPtr
End Function
End Class
|
 |
 |
 |
 |
Date :
2017-08-01 16:34:11 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
New เป็น คำเฉพาะ
ไม่ควรนำมาเป็น ชื่อ function
private sub New(xxx )
คำสั่ง New จะใช้สำหรับ create object
ดังนั้นปกติจะต้องมีตัวแปรมารับค่า
เช่่น
_timeoutTimer = New System.Threading.Timer(AddressOf OnTimerElapsed, Nothing, timeout, System.Threading.Timeout.Infinite)
ตรวจสอบไวยกรณ์ และเปลี่ยน ชื่อ function ซะ
|
 |
 |
 |
 |
Date :
2023-02-10 19:36:30 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|