Function VietnameseOff() As Boolean
'VietnameseOff = True 'Unikey is clicked
'VietnameseOff = False 'Not Unikey
VietnameseOff = Vietnamese(True)
End Function
Function VietnameseOn() As Boolean
'VietnameseOn = True 'Unikey is clicked
'VietnameseOn = False 'Not Unikey
VietnameseOn = Vietnamese(False)
End Function
Function Vietnamese(Optional ByVal TurnOff As Boolean = True) As Boolean
Dim nCount, k As Long, sTip As String
Dim tb As TBButton, tray As TRAYDATA
Dim tbXp As TBButtonXP, trayXp As TRAYDATAXP 'Windows XP-> < 10
Dim pid As Long
#If VBA7 Then
Dim pMemory As LongPtr, hTB As LongPtr, hProcess As LongPtr, BytesRead As LongPtr
#Else
Dim pMemory As Long, hTB As Long, hProcess As Long, BytesRead As Long
#End If
'Variables Added by Nguyen Duy Tuan
Dim HasUnikeybutton As Boolean, sTemp As String, CountLoop As Long
Dim WinXPVISTA As Boolean, IsUnikeyVN As Boolean
'----------------------------------
WinXPVISTA = IsWinXPOrVista
hTB = TrayToolbarWnd(Not WinXPVISTA)
lbBegenFind:
CountLoop = CountLoop + 1
If hTB = 0 Then Exit Function
GetWindowThreadProcessId hTB, pid
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
If hProcess = 0 Then Exit Function
nCount = SendMessage(hTB, TB_BUTTONCOUNT, 0, 0)
pMemory = VirtualAllocEx(hProcess, ByVal 0, ByVal 1024, MEM_COMMIT, PAGE_READWRITE)
For k = 0 To nCount - 1
SendMessage hTB, TB_GETBUTTON, k, pMemory
sTip = String(256, Chr(0))
If WinXPVISTA Then
ReadProcessMemory hProcess, ByVal pMemory, tbXp, LenB(tbXp), BytesRead
ReadProcessMemory hProcess, ByVal tbXp.dwData, trayXp, LenB(trayXp), BytesRead
ReadProcessMemory hProcess, ByVal tbXp.iString, ByVal StrPtr(sTip), 256, BytesRead
Else
ReadProcessMemory hProcess, ByVal pMemory, tb, LenB(tb), BytesRead
ReadProcessMemory hProcess, ByVal tb.dwData, tray, LenB(tray), BytesRead
ReadProcessMemory hProcess, ByVal tb.iString, ByVal StrPtr(sTip), 256, BytesRead
End If
sTip = Left(sTip, InStr(1, sTip, Chr(0)) - 1)
'Check window has Unikey button
sTemp = Replace(sTip, "turn off", "")
sTemp = Replace(sTemp, "turn on", "")
HasUnikeybutton = sTemp = "Click to Vietnamese mode" 'DO NOT CHANGE IT
'------------------------------
If HasUnikeybutton Then
IsUnikeyVN = InStr(sTip, " turn off ") > 0
If (IsUnikeyVN And TurnOff) Or (Not IsUnikeyVN And Not TurnOff) Then
If WinXPVISTA Then
AutoClickUnikey trayXp.hwnd, trayXp.uCallbackMessage, trayXp.uID
Else
AutoClickUnikey tray.hwnd, tray.uCallbackMessage, tray.uID
End If
Vietnamese = True
End If
Exit For
End If
Next k
If Not WinXPVISTA And (Not HasUnikeybutton And CountLoop = 1) Then 'Unikey may be in "TrayNotifyWnd" area
hTB = TrayToolbarWnd(False)
GoTo lbBegenFind
End If
VirtualFreeEx hProcess, pMemory, 0, MEM_RELEASE
CloseHandle hProcess
End Function
Private Function TrayToolbarWnd(ByVal CheckFloatWindow As Boolean)
Dim hTB
If CheckFloatWindow Then 'run it if OS is not Windows XP
hTB = FindWindow("NotifyIconOverflowWindow", vbNullString)
End If
If hTB <> 0 Then
hTB = FindWindowEx(hTB, 0, "ToolbarWindow32", vbNullString)
Else
hTB = FindWindow("Shell_TrayWnd", vbNullString)
If hTB <> 0 Then
hTB = FindWindowEx(hTB, 0, "TrayNotifyWnd", vbNullString)
If hTB <> 0 Then
hTB = FindWindowEx(hTB, 0, "SysPager", vbNullString)
If hTB <> 0 Then hTB = FindWindowEx(hTB, 0, "ToolbarWindow32", vbNullString)
End If
End If
End If
TrayToolbarWnd = hTB
End Function
#If VBA7 Then
Private Sub AutoClickUnikey(ByVal TrayHwnd As LongPtr, _
ByVal uCallbackMessage As Long, _
ByVal TrayUID As LongPtr)
#Else
Private Sub AutoClickUnikey(ByVal TrayHwnd As Long, _
ByVal uCallbackMessage As Long, _
ByVal TrayUID As Long)
#End If
PostMessage TrayHwnd, uCallbackMessage, TrayUID, WM_LBUTTONDOWN
PostMessage TrayHwnd, uCallbackMessage, TrayUID, WM_LBUTTONUP
End Sub