Thủ thuật: Message Box xuất hiện trong thời gian quy định
Tác giả: truongphu, từ The Best Scripting
Mô tả: Thông thường, khi một MessageBox xuất hiện, bạn phải đóng nó trước khi tiếp tục làm việc khác. Thủ thuật này sẽ trình bày cách hiển thị 1 MessageBox trong 1 khỏang thời gian được chỉ định trước. Tức là nó sẽ tự động đóng sau 1 khỏang thời gian nào đó (bạn không cần phải tắt nó).
- Vui lòng đọc nội qui diễn đàn để tránh bị xóa bài viết
- Tìm kiếm trước khi đặt câu hỏi
Message Box xuất hiện trong thời gian quy định
-
- Bài viết: 8
- Ngày tham gia: T.Hai 09/02/2009 2:27 pm
- Đến từ: http://nguoibaclieu.com
- Has thanked: 3 time
- Been thanked: 1 time
- Liên hệ:
Re: Message Box xuất hiện trong thời gian quy định
Code của bác truongphu chỉ cần đặt trong Form_Load() còn của bác NoBi thì phải bổ sung thêm modules nữa. Code này hay quá, thanks 2 bác nha!
-
- Bài viết: 7
- Ngày tham gia: T.Sáu 23/08/2013 10:53 pm
- Been thanked: 1 time
Re: Message Box xuất hiện trong thời gian quy định

Đây là code làm Msgbox hiển thị tiếng việt có dấu Unicode và Msgbox có thể tự đóng.
Mã: Chọn hết
Type MB
Hook As Long ' Holds the hook
Ok As String ' Custom okbutton holder
Retry As String
Cancel As String
Ignore As String
No As String
Yes As String
End Type
Private Declare Function GetCurrentThreadId& Lib "kernel32" ()
Private Declare Function UnhookWindowsHookEx& Lib "user32" (ByVal hHook&)
'ANSI
Private Declare Function SetWindowsHookExA Lib "user32.dll" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetDlgItemTextA Lib "user32.dll" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long
Private Declare Function MessageBoxA Lib "user32.dll" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
Private Declare Function GetWindowLongA Lib "user32" (ByVal hwnd&, ByVal nIndex&) As Long
'UNICODE
Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetDlgItemTextW Lib "user32.dll" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As Long) As Long
Private Declare Function MessageBoxW Lib "user32.dll" (ByVal hwnd As Long, ByVal lpText As Long, ByVal lpCaption As Long, ByVal wType As Long) As Long
Private Declare Function GetWindowLongW Lib "user32" (ByVal hwnd&, ByVal nIndex&) As Long
'Constants
Private Const MB_ABORTRETRYIGNORE As Long = &H2&
Private Const MB_OKCANCEL As Long = &H1&
Private Const MB_RETRYCANCEL As Long = &H5&
Private Const MB_YESNO As Long = &H4&
Private Const MB_YESNOCANCEL As Long = &H3&
Private Const MB_ICONASTERISK As Long = &H40&
Private Const MB_ICONEXCLAMATION As Long = &H30&
Private Const MB_ICONHAND As Long = &H10&
Private Const MB_ICONINFORMATION As Long = MB_ICONASTERISK
Private Const MB_ICONMASK As Long = &HF0&
Private Const MB_ICONQUESTION As Long = &H20&
Private Const MB_ICONSTOP As Long = MB_ICONHAND
Private Const MB_OK As Long = &H0&
Private Const MB_TASKMODAL As Long = &H2000&
'All the Messagebox buttons
Private Const IDOK As Long = 1
Private Const IDABORT As Long = 3
Private Const IDRETRY As Long = 4
Private Const IDYES As Long = 6
Private Const IDCANCEL As Long = 2
Private Const IDIGNORE As Long = 5
Private Const IDNO As Long = 7
Private Const WH_CBT As Long = 5
Private MT As MB
'------------------------
Public Const NV_CLOSEMSGBOX As Long = &H5000&
' API declares
Public Declare Function LockWindowUpdate& Lib "user32" (ByVal hwndLock&)
Public Declare Function GetActiveWindow& Lib "user32" ()
Public Declare Function GetDesktopWindow& Lib "user32" ()
'Public Declare Function FindWindow& Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, _
ByVal lpWindowName$)
Public Declare Function FindWindow Lib "user32" Alias "FindWindowW" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Public Declare Function SetForegroundWindow& Lib "user32" (ByVal hwnd&)
Public Declare Function GetClassName& Lib "user32" Alias "GetClassNameA" (ByVal hwnd&, _
ByVal lpClassName$, ByVal nMaxCount&)
'Public Declare Function GetWindowRect& Lib "user32" (ByVal hwnd&, lpRect As RECT)
Public Declare Function SetWindowPos& Lib "user32" (ByVal hwnd&, ByVal hWndInsertAfter&, _
ByVal x&, ByVal y&, ByVal cx&, ByVal cy&, ByVal wFlags&)
Public Declare Function MessageBox& Lib "user32" Alias "MessageBoxA" (ByVal hwnd&, _
ByVal lpText$, ByVal lpCaption$, ByVal wType&)
Public Declare Function SetTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&, ByVal uElapse&, _
ByVal lpTimerFunc&)
Public Declare Function KillTimer& Lib "user32" (ByVal hwnd&, ByVal nIDEvent&)
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
Public Declare Function GetTickCount Lib "kernel32" () As Long
Private Const WM_CLOSE = &H10
Public tieudeMSG As String, EndTime As Long, StartTime As Long
Public Function ConvertToUnicode(sText As String)
Dim i As Integer, j As Integer
Dim sCurChar As String, sPreChar As String, sPreTxt As String
For j = 1 To 2
For i = 2 To Len(sText)
sCurChar = Mid(sText, i, 1)
sPreTxt = Left(sText, i - 2)
sPreChar = Mid(sText, i - 1, 1)
Select Case sCurChar
Case "1"
Select Case sPreChar
'a
Case "a": sText = sPreTxt & ChrW$(&HE1) & Right(sText, Len(sText) - i)
Case "A": sText = sPreTxt & ChrW$(&HC1) & Right(sText, Len(sText) - i)
Case ChrW$(&HE2): sText = sPreTxt & ChrW$(&H1EA5) & Right(sText, Len(sText) - i)
Case ChrW$(&HC2): sText = sPreTxt & ChrW$(&H1EA4) & Right(sText, Len(sText) - i)
Case ChrW$(&H103): sText = sPreTxt & ChrW$(&H1EAF) & Right(sText, Len(sText) - i)
Case ChrW$(&H102): sText = sPreTxt & ChrW$(&H1EAE) & Right(sText, Len(sText) - i)
'e
Case "e": sText = sPreTxt & ChrW$(&HE9) & Right(sText, Len(sText) - i)
Case "E": sText = sPreTxt & ChrW$(&HC9) & Right(sText, Len(sText) - i)
Case ChrW$(&HEA): sText = sPreTxt & ChrW$(&H1EBF) & Right(sText, Len(sText) - i)
Case ChrW$(&HCA): sText = sPreTxt & ChrW$(&H1EBE) & Right(sText, Len(sText) - i)
'i
Case "i": sText = sPreTxt & ChrW$(&HED) & Right(sText, Len(sText) - i)
Case "I": sText = sPreTxt & ChrW$(&HCD) & Right(sText, Len(sText) - i)
'o
Case "o": sText = sPreTxt & ChrW$(&HF3) & Right(sText, Len(sText) - i)
Case "O": sText = sPreTxt & ChrW$(&HD3) & Right(sText, Len(sText) - i)
Case ChrW$(&HF4): sText = sPreTxt & ChrW$(&H1ED1) & Right(sText, Len(sText) - i)
Case ChrW$(&HD4): sText = sPreTxt & ChrW$(&H1ED0) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A1): sText = sPreTxt & ChrW$(&H1EDB) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A0): sText = sPreTxt & ChrW$(&H1EDA) & Right(sText, Len(sText) - i)
'u
Case "u": sText = sPreTxt & ChrW$(&HFA) & Right(sText, Len(sText) - i)
Case "U": sText = sPreTxt & ChrW$(&HDA) & Right(sText, Len(sText) - i)
Case ChrW$(&H1B0): sText = sPreTxt & ChrW$(&H1EE9) & Right(sText, Len(sText) - i)
Case ChrW$(&H1AF): sText = sPreTxt & ChrW$(&H1EE8) & Right(sText, Len(sText) - i)
'y
Case "y": sText = sPreTxt & ChrW$(&HFD) & Right(sText, Len(sText) - i)
Case "Y": sText = sPreTxt & ChrW$(&HDD) & Right(sText, Len(sText) - i)
End Select
Case "2"
Select Case sPreChar
'a
Case "a": sText = sPreTxt & ChrW$(&HE0) & Right(sText, Len(sText) - i)
Case "A": sText = sPreTxt & ChrW$(&HC0) & Right(sText, Len(sText) - i)
Case ChrW$(&HE2): sText = sPreTxt & ChrW$(&H1EA7) & Right(sText, Len(sText) - i)
Case ChrW$(&HC2): sText = sPreTxt & ChrW$(&H1EA6) & Right(sText, Len(sText) - i)
Case ChrW$(&H103): sText = sPreTxt & ChrW$(&H1EB1) & Right(sText, Len(sText) - i)
Case ChrW$(&H102): sText = sPreTxt & ChrW$(&H1EB0) & Right(sText, Len(sText) - i)
'e
Case "e": sText = sPreTxt & ChrW$(&HE8) & Right(sText, Len(sText) - i)
Case "E": sText = sPreTxt & ChrW$(&HC8) & Right(sText, Len(sText) - i)
Case ChrW$(&HEA): sText = sPreTxt & ChrW$(&H1EC1) & Right(sText, Len(sText) - i)
Case ChrW$(&HCA): sText = sPreTxt & ChrW$(&H1EC0) & Right(sText, Len(sText) - i)
'i
Case "i": sText = sPreTxt & ChrW$(&HEC) & Right(sText, Len(sText) - i)
Case "I": sText = sPreTxt & ChrW$(&HCC) & Right(sText, Len(sText) - i)
'o
Case "o": sText = sPreTxt & ChrW$(&HF2) & Right(sText, Len(sText) - i)
Case "O": sText = sPreTxt & ChrW$(&HD2) & Right(sText, Len(sText) - i)
Case ChrW$(&HF4): sText = sPreTxt & ChrW$(&H1ED3) & Right(sText, Len(sText) - i)
Case ChrW$(&HD4): sText = sPreTxt & ChrW$(&H1ED2) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A1): sText = sPreTxt & ChrW$(&H1EDD) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A0): sText = sPreTxt & ChrW$(&H1EDC) & Right(sText, Len(sText) - i)
'u
Case "u": sText = sPreTxt & ChrW$(&HF9) & Right(sText, Len(sText) - i)
Case "U": sText = sPreTxt & ChrW$(&HD9) & Right(sText, Len(sText) - i)
Case ChrW$(&H1B0): sText = sPreTxt & ChrW$(&H1EEB) & Right(sText, Len(sText) - i)
Case ChrW$(&H1AF): sText = sPreTxt & ChrW$(&H1EEA) & Right(sText, Len(sText) - i)
'y
Case "y": sText = sPreTxt & ChrW$(&H1EF3) & Right(sText, Len(sText) - i)
Case "Y": sText = sPreTxt & ChrW$(&H1EF2) & Right(sText, Len(sText) - i)
End Select
Case "3"
Select Case sPreChar
'a
Case "a": sText = sPreTxt & ChrW$(&H1EA3) & Right(sText, Len(sText) - i)
Case "A": sText = sPreTxt & ChrW$(&H1EA2) & Right(sText, Len(sText) - i)
Case ChrW$(&HE2): sText = sPreTxt & ChrW$(&H1EA9) & Right(sText, Len(sText) - i)
Case ChrW$(&HC2): sText = sPreTxt & ChrW$(&H1EA8) & Right(sText, Len(sText) - i)
Case ChrW$(&H103): sText = sPreTxt & ChrW$(&H1EB3) & Right(sText, Len(sText) - i)
Case ChrW$(&H102): sText = sPreTxt & ChrW$(&H1EB2) & Right(sText, Len(sText) - i)
'e
Case "e": sText = sPreTxt & ChrW$(&H1EBB) & Right(sText, Len(sText) - i)
Case "E": sText = sPreTxt & ChrW$(&H1EBA) & Right(sText, Len(sText) - i)
Case ChrW$(&HEA): sText = sPreTxt & ChrW$(&H1EC3) & Right(sText, Len(sText) - i)
Case ChrW$(&HCA): sText = sPreTxt & ChrW$(&H1EC2) & Right(sText, Len(sText) - i)
'i
Case "i": sText = sPreTxt & ChrW$(&H1EC9) & Right(sText, Len(sText) - i)
Case "I": sText = sPreTxt & ChrW$(&H1EC8) & Right(sText, Len(sText) - i)
'o
Case "o": sText = sPreTxt & ChrW$(&H1ECF) & Right(sText, Len(sText) - i)
Case "O": sText = sPreTxt & ChrW$(&H1ECE) & Right(sText, Len(sText) - i)
Case ChrW$(&HF4): sText = sPreTxt & ChrW$(&H1ED5) & Right(sText, Len(sText) - i)
Case ChrW$(&HD4): sText = sPreTxt & ChrW$(&H1ED4) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A1): sText = sPreTxt & ChrW$(&H1EDF) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A0): sText = sPreTxt & ChrW$(&H1EDE) & Right(sText, Len(sText) - i)
'u
Case "u": sText = sPreTxt & ChrW$(&H1EE7) & Right(sText, Len(sText) - i)
Case "U": sText = sPreTxt & ChrW$(&H1EE6) & Right(sText, Len(sText) - i)
Case ChrW$(&H1B0): sText = sPreTxt & ChrW$(&H1EED) & Right(sText, Len(sText) - i)
Case ChrW$(&H1AF): sText = sPreTxt & ChrW$(&H1EEC) & Right(sText, Len(sText) - i)
'y
Case "y": sText = sPreTxt & ChrW$(&H1EF7) & Right(sText, Len(sText) - i)
Case "Y": sText = sPreTxt & ChrW$(&H1EF6) & Right(sText, Len(sText) - i)
End Select
Case "4"
Select Case sPreChar
'a
Case "a": sText = sPreTxt & ChrW$(&HE3) & Right(sText, Len(sText) - i)
Case "A": sText = sPreTxt & ChrW$(&HC3) & Right(sText, Len(sText) - i)
Case ChrW$(&HE2): sText = sPreTxt & ChrW$(&H1EAB) & Right(sText, Len(sText) - i)
Case ChrW$(&HC2): sText = sPreTxt & ChrW$(&H1EAA) & Right(sText, Len(sText) - i)
Case ChrW$(&H103): sText = sPreTxt & ChrW$(&H1EB5) & Right(sText, Len(sText) - i)
Case ChrW$(&H102): sText = sPreTxt & ChrW$(&H1EB4) & Right(sText, Len(sText) - i)
'e
Case "e": sText = sPreTxt & ChrW$(&H1EBD) & Right(sText, Len(sText) - i)
Case "E": sText = sPreTxt & ChrW$(&H1EBC) & Right(sText, Len(sText) - i)
Case ChrW$(&HEA): sText = sPreTxt & ChrW$(&H1EC5) & Right(sText, Len(sText) - i)
Case ChrW$(&HCA): sText = sPreTxt & ChrW$(&H1EC4) & Right(sText, Len(sText) - i)
'i
Case "i": sText = sPreTxt & ChrW$(&H129) & Right(sText, Len(sText) - i)
Case "I": sText = sPreTxt & ChrW$(&H128) & Right(sText, Len(sText) - i)
'o
Case "o": sText = sPreTxt & ChrW$(&HF5) & Right(sText, Len(sText) - i)
Case "O": sText = sPreTxt & ChrW$(&HD5) & Right(sText, Len(sText) - i)
Case ChrW$(&HF4): sText = sPreTxt & ChrW$(&H1ED7) & Right(sText, Len(sText) - i)
Case ChrW$(&HD4): sText = sPreTxt & ChrW$(&H1ED6) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A1): sText = sPreTxt & ChrW$(&H1EE1) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A0): sText = sPreTxt & ChrW$(&H1EE0) & Right(sText, Len(sText) - i)
'u
Case "u": sText = sPreTxt & ChrW$(&H169) & Right(sText, Len(sText) - i)
Case "U": sText = sPreTxt & ChrW$(&H168) & Right(sText, Len(sText) - i)
Case ChrW$(&H1B0): sText = sPreTxt & ChrW$(&H1EEF) & Right(sText, Len(sText) - i)
Case ChrW$(&H1AF): sText = sPreTxt & ChrW$(&H1EEE) & Right(sText, Len(sText) - i)
'y
Case "y": sText = sPreTxt & ChrW$(&H1EF9) & Right(sText, Len(sText) - i)
Case "Y": sText = sPreTxt & ChrW$(&H1EF8) & Right(sText, Len(sText) - i)
End Select
Case "5"
Select Case sPreChar
'a
Case "a": sText = sPreTxt & ChrW$(&H1EA1) & Right(sText, Len(sText) - i)
Case "A": sText = sPreTxt & ChrW$(&H1EA0) & Right(sText, Len(sText) - i)
Case ChrW$(&HE2): sText = sPreTxt & ChrW$(&H1EAD) & Right(sText, Len(sText) - i)
Case ChrW$(&HC2): sText = sPreTxt & ChrW$(&H1EAC) & Right(sText, Len(sText) - i)
Case ChrW$(&H103): sText = sPreTxt & ChrW$(&H1EB7) & Right(sText, Len(sText) - i)
Case ChrW$(&H102): sText = sPreTxt & ChrW$(&H1EB6) & Right(sText, Len(sText) - i)
'e
Case "e": sText = sPreTxt & ChrW$(&H1EB9) & Right(sText, Len(sText) - i)
Case "E": sText = sPreTxt & ChrW$(&H1EB8) & Right(sText, Len(sText) - i)
Case ChrW$(&HEA): sText = sPreTxt & ChrW$(&H1EC7) & Right(sText, Len(sText) - i)
Case ChrW$(&HCA): sText = sPreTxt & ChrW$(&H1EC6) & Right(sText, Len(sText) - i)
'i
Case "i": sText = sPreTxt & ChrW$(&H1ECB) & Right(sText, Len(sText) - i)
Case "I": sText = sPreTxt & ChrW$(&H1ECA) & Right(sText, Len(sText) - i)
'o
Case "o": sText = sPreTxt & ChrW$(&H1ECD) & Right(sText, Len(sText) - i)
Case "O": sText = sPreTxt & ChrW$(&H1ECC) & Right(sText, Len(sText) - i)
Case ChrW$(&HF4): sText = sPreTxt & ChrW$(&H1ED9) & Right(sText, Len(sText) - i)
Case ChrW$(&HD4): sText = sPreTxt & ChrW$(&H1ED8) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A1): sText = sPreTxt & ChrW$(&H1EE3) & Right(sText, Len(sText) - i)
Case ChrW$(&H1A0): sText = sPreTxt & ChrW$(&H1EE2) & Right(sText, Len(sText) - i)
'u
Case "u": sText = sPreTxt & ChrW$(&H1EE5) & Right(sText, Len(sText) - i)
Case "U": sText = sPreTxt & ChrW$(&H1EE4) & Right(sText, Len(sText) - i)
Case ChrW$(&H1B0): sText = sPreTxt & ChrW$(&H1EF1) & Right(sText, Len(sText) - i)
Case ChrW$(&H1AF): sText = sPreTxt & ChrW$(&H1EF0) & Right(sText, Len(sText) - i)
'y
Case "y": sText = sPreTxt & ChrW$(&H1EF5) & Right(sText, Len(sText) - i)
Case "Y": sText = sPreTxt & ChrW$(&H1EF4) & Right(sText, Len(sText) - i)
End Select
Case "6"
Select Case sPreChar
'a
Case "a": sText = sPreTxt & ChrW$(&HE2) & Right(sText, Len(sText) - i)
Case "A": sText = sPreTxt & ChrW$(&HC2) & Right(sText, Len(sText) - i)
'e
Case "e": sText = sPreTxt & ChrW$(&HEA) & Right(sText, Len(sText) - i)
Case "E": sText = sPreTxt & ChrW$(&HCA) & Right(sText, Len(sText) - i)
'o
Case "o": sText = sPreTxt & ChrW$(&HF4) & Right(sText, Len(sText) - i)
Case "O": sText = sPreTxt & ChrW$(&HD4) & Right(sText, Len(sText) - i)
End Select
Case "7"
Select Case sPreChar
'o
Case "o": sText = sPreTxt & ChrW$(&H1A1) & Right(sText, Len(sText) - i)
Case "O": sText = sPreTxt & ChrW$(&H1A0) & Right(sText, Len(sText) - i)
'u
Case "u": sText = sPreTxt & ChrW$(&H1B0) & Right(sText, Len(sText) - i)
Case "U": sText = sPreTxt & ChrW$(&H1AF) & Right(sText, Len(sText) - i)
End Select
Case "8"
Select Case sPreChar
'a
Case "a": sText = sPreTxt & ChrW$(&H103) & Right(sText, Len(sText) - i)
Case "A": sText = sPreTxt & ChrW$(&H102) & Right(sText, Len(sText) - i)
End Select
Case "9"
Select Case sPreChar
'd
Case "d": sText = sPreTxt & ChrW$(&H111) & Right(sText, Len(sText) - i)
Case "D": sText = sPreTxt & ChrW$(&H110) & Right(sText, Len(sText) - i)
End Select
End Select
Next i
Next j
ConvertToUnicode = sText
End Function
'-------------------------
'Functions
Function MsgBoxA(Prompt As String, Flags As VbMsgBoxStyle, _
Title As String, sOK As String, _
Optional sRetry As String, Optional sCancel As String, _
Optional sYes As String, Optional sNo As String, _
Optional sIgnore As String) As Long
On Error Resume Next ' error protection
MT.Hook = SetWindowsHookExA(5, AddressOf MsgBoxCallBackA, GetWindowLongA(0, (-6)), GetCurrentThreadId)
MT.Ok = sOK
MT.Cancel = sCancel
MT.Ignore = sIgnore
MT.No = sNo
MT.Retry = sRetry
MT.Yes = sYes
MsgBoxA = MessageBoxA(0, Prompt$, Title$, Flags Or MB_TASKMODAL)
End Function
Function MsgBoxCallBackA(ByVal uMsg As Long, ByVal wParam As Long, lParam As Long) As Long
If uMsg = WH_CBT Then
SetDlgItemTextA wParam, IDOK, MT.Ok
SetDlgItemTextA wParam, IDCANCEL, MT.Cancel
SetDlgItemTextA wParam, IDIGNORE, MT.Ignore
SetDlgItemTextA wParam, IDNO, MT.No
SetDlgItemTextA wParam, IDYES, MT.Yes
SetDlgItemTextA wParam, IDRETRY, MT.Retry
UnhookWindowsHookEx MT.Hook
End If
End Function
Function MsgBoxW(Prompt As String, Flags As VbMsgBoxStyle, _
Title As String, wHwnd As Long, Optional sTime, Optional sOK As String, _
Optional sRetry As String, Optional sCancel As String, _
Optional sYes As String, Optional sNo As String, _
Optional sIgnore As String) As Long
On Error Resume Next ' error protection
tieudeMSG = Title: EndTime = CLng(sTime): StartTime = GetTickCount() / 1000
If Not IsMissing(sTime) Then SetTimer wHwnd, NV_CLOSEMSGBOX, 1000, AddressOf TimerProc
MT.Hook = SetWindowsHookExW(5, AddressOf MsgBoxCallBackW, GetWindowLongW(0, (-6)), GetCurrentThreadId)
MT.Ok = IIf(Len(sOK) = 0, ChrW(&H110) & ChrW(&H1ED3) & "ng " & ChrW(&HFD), sOK)
MT.Cancel = IIf(Len(sCancel) = 0, "H" & ChrW(&H1EE7) & "y b" & ChrW(&H1ECF), sCancel)
MT.Ignore = IIf(Len(sIgnore) = 0, "B" & ChrW(&H1ECF) & " qua", sIgnore)
MT.No = IIf(Len(sNo) = 0, "Kh" & ChrW(&HF4) & "ng", sNo)
MT.Retry = IIf(Len(sRetry) = 0, "Th" & ChrW(&H1EED) & "l" & ChrW(&H1EA1) & "i", sRetry)
MT.Yes = IIf(Len(sYes) = 0, "C" & ChrW(&HF3), sYes)
MsgBoxW = MessageBoxW(wHwnd, StrPtr((Prompt)), StrPtr((Title)), Flags Or MB_TASKMODAL)
End Function
Function MsgBoxCallBackW(ByVal uMsg As Long, ByVal wParam As Long, lParam As Long) As Long
If uMsg = WH_CBT Then
SetDlgItemTextW wParam, IDOK, StrPtr(MT.Ok)
SetDlgItemTextW wParam, IDCANCEL, StrPtr(MT.Cancel)
SetDlgItemTextW wParam, IDIGNORE, StrPtr(MT.Ignore)
SetDlgItemTextW wParam, IDNO, StrPtr(MT.No)
SetDlgItemTextW wParam, IDYES, StrPtr(MT.Yes)
SetDlgItemTextW wParam, IDRETRY, StrPtr(MT.Retry)
UnhookWindowsHookEx MT.Hook
End If
End Function
Public Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)
Dim TempTime As Long
Dim hMessageBox&
' find the messagebox window
hMessageBox = FindWindow(StrPtr("#32770"), StrPtr(tieudeMSG))
TempTime = GetTickCount() / 1000
'Debug.Print StartTime + EndTime & " " & TempTime
Debug.Print hMessageBox
If (StartTime + EndTime <= TempTime) Or (hMessageBox = 0) Then
If hMessageBox Then
Call SetForegroundWindow(hMessageBox)
SendMessage hMessageBox, &H10, 0, 0
End If
KillTimer hwnd, idEvent
StartTime = 0: TempTime = 0
Debug.Print "da giet timer"
End If
End Sub
Cách sử dụng: ví dụ MSGBOX đóng sau 10 giây
MsgBoxW sThongbao, vbInformation, sTieude, Me.hWnd, 10
Quay về “[VB] Ứng dụng - Form và Control”
Đang trực tuyến
Đang xem chuyên mục này: Không có thành viên nào trực tuyến. và 1 khách