# Re: 输入法

Posted by cww on February 19, 1998 at 02:08:11:

In Reply to: 输入法 posted by 吕军贤 on February 16, 1998 at 01:06:02:

以下程式可以解决您第一个问题，至于第二个问题设定form1.keypreView = true
而后拦截您key in的 键盘，再与取得的keyboard handle做结合，
用ActivateKeyboardLayout来设定便可

Private Declare Function GetKeyboardLayoutList Lib "user32" (ByVal nBuff As Long, lpList As Long) As Long
Private Declare Function ImmGetDescription Lib "imm32.dll" Alias "ImmGetDescriptionA" (ByVal HKL As Long, ByVal lpsz As String, ByVal uBufLen As Long) As Long
Private Declare Function ImmIsIME Lib "imm32.dll" (ByVal HKL As Long) As Long
Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" (ByVal pwszKLID As String) As Long
Private Declare Function ActivateKeyboardLayout Lib "user32" (ByVal HKL As Long, ByVal flags As Long) As Long
Private Declare Function GetKeyboardLayout Lib "user32" (ByVal dwLayout As Long) As Long

Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Const REG_EXPAND_SZ = 2
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_LOCAL_MACHINE = &H80000002

Const READ_CONTROL = &H20000
Const STANDARD_RIGHTS_READ = (READ_CONTROL)
Const KEY_QUERY_VALUE = &H1
Const KEY_ENUMERATE_SUB_KEYS = &H8
Const KEY_NOTIFY = &H10
Const SYNCHRONIZE = &H100000
Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
Private hCurKBDLayout As Long

Private Sub Form_Load()
Dim NoOfKBDLayout As Long, i As Long, j As Long
Dim hKB(24) As Long, BuffLen As Long
Dim Buff As String
Dim RetStr As String
Dim RetCount As Long

Buff = String(255, 0)
hCurKBDLayout = GetKeyboardLayout(0) '取得目前keyboard layout
NoOfKBDLayout = GetKeyboardLayoutList(25, hKB(0))
For i = 1 To NoOfKBDLayout
If ImmIsIME(hKB(i - 1)) = 1 Then '中文输入法
BuffLen = 255
RetCount = ImmGetDescription(hKB(i - 1), Buff, BuffLen)
RetStr = Left(Buff, RetCount)
Combo1.AddItem RetStr
Else
ActivateKeyboardLayout hKB(i - 1), 0
GetKeyboardLayoutName Buff '取得keyboard layout的subkey
j = InStr(1, Buff, Chr(0), vbBinaryCompare)
RetStr = Left(Buff, j - 1)
RetStr = GetRegValue(RetStr)
Combo1.AddItem RetStr
End If
If hKB(i - 1) = hCurKBDLayout Then
Combo1.Text = RetStr
End If
Next
ActivateKeyboardLayout hCurKBDLayout, 0 '回复原来输入法
End Sub

Private Function GetRegValue(Qrystr As String) As String
Dim key5 As String, KeyValue As String
Dim resul As Long
Dim subkey As String
Dim pr As Long, leng1 As Long

key5 = "System\CurrentControlset\control\Keyboard Layouts\" + Qrystr
resul = RegOpenKeyEx(HKEY_LOCAL_MACHINE, key5, 0, KEY_READ, pr)
subkey = "layout text"
leng1 = 255
KeyValue = String(255, 0)
resul = RegQueryValueEx(pr, subkey, 0, 1, KeyValue, leng1)
RegCloseKey pr
GetRegValue = Left(KeyValue, leng1 - 1)

End Function

Follow Ups: