发信人: hyhour (time), 信区: VB
标  题: 切换输入法
发信站: 饮水思源站 (Wed Jan 12 16:51:52 2000), 转信需要一个ComboBOX,和一个Text.Option Explicit
Private Declare Function GetKeyboardLayoutList Lib "user32" (ByVal nBuff As Long, _
        lpList As Long) As Long
Private Declare Function GetKeyboardLayoutName Lib "user32" Alias "GetKeyboardLayoutNameA" _
        (ByVal pwszKLID As String) As Long
Private Declare Function GetKeyboardLayout Lib "user32" (ByVal dwLayout 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 ActivateKeyboardLayout Lib "user32" (ByVal hkl As Long, ByVal _
        flags As Long) As LongConst IME_CONFIG_GENERAL = 1
Const KLF_REORDER = &H8
Const KLF_ACTIVATE = &H1Dim la(1 To 16) As Long
Dim ActIme As LongPrivate Sub Combo1_Click()
    ActIme = la(Combo1.ListIndex + 1)
    Debug.Print ActIme
    Text1.SetFocus
End SubPrivate Sub Form_Load()
    Dim astr As String * 256
    Dim bstr As String
    Dim x, hMem, i As Long
    
    x = GetKeyboardLayoutList(32, la(1))
    Combo1.Clear
    If x Then
        For i = 1 To x
            ImmGetDescription la(i), astr, 256
            If InStr(astr, Chr(0)) = 1 Then
                bstr = ""
            Else
                bstr = Left$(astr, InStr(astr, Chr(0)))
            End If
        
            If Trim(bstr) = "" Then
                Combo1.AddItem "英语(美国)"
            Else
                Combo1.AddItem bstr
            End If
        Next i
    End If
End SubPrivate Sub Text1_GotFocus()
    If Combo1.ListCount > 0 Then
        ActivateKeyboardLayout ActIme, 1
    End If
End Sub
--
※ 修改:·hyhour 於 Jan 12 16:52:49 修改本文·[FROM: 202.120.18.28]
※ 来源:·饮水思源站 bbs.sjtu.edu.cn·[FROM: 202.120.18.28]

解决方案 »

  1.   

    CSDN - 技术频道 - 文档中心 - Visual Basic      
      
    标题     用VB读取和控制Windows的中文输入法    vbfdy(收藏)  
      
    关键字     输入法 
      
     在Windows中我们可以用“Ctrl+Shift”键来调入或切换中文输入法,但是这样做每次都是使位于输入法列表顶端的那个输入法首先被调用。通常我们都要连续按好几次“Ctrl+Shift”才能将习惯的输入法调出。我编制了一段小程序,通过它可以把任意一个输入法放在输入法列表的顶端。
      它的原理是:使用LoadKeyboardLayout函数可以改变输入法的顺序,只要在第一个参数中传递目标输入法的KeyboardlayoutName,第二个参数用KLF_REORDER就可以了。 
      例如,aa = LoadKeyboardLayout(″00000409″, KLF_REORDER) 使英文变成第一。那怎样获得KeyboardlayoutName呢?因为使用GetKeyboardLayoutname可以返回当前输入法的KeyboardlayoutName,所以我们可以先用GetKeyboardLayoutList 函数来取得所有输入法,再用activateKeyboardlayout()函数设置当前输入法,最后就可以得到它的KeyboardlayoutName了。具体步骤如下: 
      打开VB后选择标准的EXE文档,在Form1上添加一个Combobox和一个command控件,输入以下程序。 
      ′以下的API函数用于输入法操作 
      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 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 GetKeyboardLayoutName Lib ″user32″ Alias _ 
      ″GetKeyboardLayoutNameA″ (ByVal pwszKLID As String) As Long 
      Private Declare Function LoadKeyboardLayout Lib ″user32″ Alias ″LoadKeyboardLayoutA″ _ 
      (ByVal pwszKLID As String, ByVal flags As Long) As Long 
      Const KLF_REORDER = &H8 
      Private NoOfKBDLayout As Long, i As Long, j As Long 
      Private hKB(24) As Long, BuffLen As Long 
      Private Buff As String 
      Private RetStr As String 
      Private RetCount As Long 
      Private kln As String 
      Private Sub Command1_Click() 
      If Combo1.ListIndex = -1 Then′如果用户尚未选择输入法,显示出错信息 
      MsgBox ″请先选择一个输入法″ 
      Exit Sub 
      End If 
      ′改变输入法顺序 
      kln = String(8, 0) 
      ActivateKeyboardLayout hKB(Combo1.ListIndex), 0 
      res = GetKeyboardLayoutName(kln) 
      res = LoadKeyboardLayout(kln, KLF_REORDER) 
      ActivateKeyboardLayout hCurKBDLayout, 0 
      End Sub 
      Private Sub Form_Load() 
      Buff = String(255, 0) 
      hCurKBDLayout = GetKeyboardLayout(0) ′取得目前的输入法 
      NoOfKBDLayout = GetKeyboardLayoutList(25, hKB(0)) ′取得所有输入法 
      ′ReDim layoutlist(NoOfKBDLayout) As String 
      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 
      RetStr = ″English (American)″ ′英文输入法 
      Combo1.AddItem RetStr 
      End If 
      Next 
      ActivateKeyboardLayout hCurKBDLayout, 0 ′恢复原来的输入法 
      End Sub 
      运行后,在combobox中选择目标输入法,按下command即可。。
     
      

  2.   

    希望对你有用,我不记得我下载的URL。
    只好贴出来CODE。
      

  3.   

    太麻烦了吧
    使用控件的Imemode属性就可以了
      

  4.   

    object.IMEMode [= value]
    Setting Description 
    0 None (Default). This value indicates "No control to IME". When the IMEMode property is set to 0, you can use the IMEStatus function to determine the current IME status. 
    1 IME on. This value indicates that the IME is on and characters specific to Chinese or Japanese can be entered. This setting is valid for Japanese, Simplified Chinese, and Traditional Chinese IME only. 
    2 IME off. This mode indicates that the IME is off, meaning that the object behaves the same as English entry mode. This setting is valid for Japanese, Simplified Chinese, and Traditional Chinese IME only. 
    3 IME disabled. This mode is similar to IMEMode = 2, except the value 3 disables IME. With this setting, the users cannot turn the IME on from the keyboard, and the IME floating window is hidden. This setting is valid for Japanese IME only. 
    4 Hiragana double-byte characters (DBC). This setting is valid for Japanese IME only. 
    5 Katakana DBC. This setting is valid for Japanese IME only. 
    6 Katakana single-byte characters (SBC). This setting is valid for Japanese IME only. 
    7 Alphanumeric DBC. This setting is valid for Japanese IME only. 
    8 Alphanumeric SBC. This setting is valid for Japanese IME only. 
    9 Hangeul DBC. This setting is valid for Korean IME only. 
    10 Hangeul SBC. This setting is valid for Korean IME only.