Option Explicit
' Import
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function SetDlgItemText Lib "user32" _
Alias "SetDlgItemTextW" _
(ByVal hDlg As Long, _
ByVal nIDDlgItem As Long, _
ByVal lpString As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
' Handle to the Hook procedure
Private hHook As Long
' Hook type
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
' Constants
Public Const IDOK = 1
Public Const IDCANCEL = 2
Public Const IDABORT = 3
Public Const IDRETRY = 4
Public Const IDIGNORE = 5
Public Const IDYES = 6
Public Const IDNO = 7
' Modify this code for English
Private StrYes As String
Private StrNo As String
Private StrOK As String
Private StrCancel As String
' Application title
Private Const xApp_Title = "MIS Application"
Function MsgBox(MessageTxt As String, Optional msgStyle As VbMsgBoxStyle) As VbMsgBoxResult
Beep
Dim iVal As VbMsgBoxStyle, msgBoxIcon As MsoAlertIconType, msgButton As MsoAlertButtonType
iVal = msgStyle
Select Case msgStyle
Case 20, 19, 17, 16: ' Critical case
iVal = iVal - 16
msgBoxIcon = msoAlertIconCritical
Case 36, 35, 33, 32: ' Question case
iVal = iVal - 32
msgBoxIcon = msoAlertIconQuery
Case 52, 51, 49, 48: ' Exclamation case
iVal = iVal - 48
msgBoxIcon = msoAlertIconWarning
Case 68, 67, 65, 64: ' Information case
iVal = iVal - 64
msgBoxIcon = msoAlertIconInfo
End Select
Select Case iVal
Case 4:
msgButton = msoAlertButtonYesNo
Case 3:
msgButton = msoAlertButtonYesNoCancel
Case 1:
msgButton = msoAlertButtonOKCancel
Case 0:
msgButton = msoAlertButtonOK
End Select
' Set Hook
hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, 0, GetCurrentThreadId)
' Display the messagebox
MsgBox = Application.Assistant.DoAlert(xApp_Title, MessageTxt, msgButton, msgBoxIcon, msoAlertDefaultFirst, msoAlertCancelDefault, True)
End Function
Private Function MsgBoxHookProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
'Hàm StrConv để chuyển chuỗi tiếng Việt sang dạng Unicode, hàm SetDlgItemText trên kia có Alias là W đối với thông điệp Unicode.
StrYes = "&C" & ChrW(243)
StrNo = "&Kh" & ChrW(244) & "ng"
StrOK = ChrW(272) & ChrW(7891) & "&ng " & ChrW(253)
StrCancel = "&H" & ChrW(7911) & "y"
SetDlgItemText wParam, IDYES, StrConv(StrYes, vbUnicode)
SetDlgItemText wParam, IDNO, StrConv(StrNo, vbUnicode)
SetDlgItemText wParam, IDCANCEL, StrConv(StrCancel, vbUnicode)
SetDlgItemText wParam, IDOK, StrConv(StrOK, vbUnicode)
' Release the Hook
UnhookWindowsHookEx hHook
End If
MsgBoxHookProc = False
End Function