用VB讀取和控制Windows的中文IME

來源:互聯網
上載者:User

在Windows中我們可以用“Ctrl+Shift”鍵來調入或切換中文IME,但是這樣做每次都是使位於IME列表頂端的那個IME首先被調用。通常我們都要連續按好幾次“Ctrl+Shift”才能將習慣的IME調出。我編製了一段小程式,通過它可以把任意一個IME放在IME列表的頂端。

它的原理是:使用LoadKeyboardLayout函數可以改變IME的順序,只要在第一個參數中傳遞目標IME的KeyboardlayoutName,第二個參數用KLF_REORDER就可以了。

例如,aa = LoadKeyboardLayout(″00000409″, KLF_REORDER) 使英文變成第一。那怎樣獲得KeyboardlayoutName呢?因為使用GetKeyboardLayoutname可以返回當前IME的KeyboardlayoutName,所以我們可以先用GetKeyboardLayoutList 函數來取得所有IME,再用activateKeyboardlayout()函數設定當前IME,最後就可以得到它的KeyboardlayoutName了。具體步驟如下:

開啟VB後選擇標準的EXE文檔,在Form1上添加一個Combobox和一個command控制項,輸入以下程式。

′以下的API函數用於IME操作

  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′如果使用者尚未選擇IME,顯示出錯資訊
   MsgBox ″請先選擇一個IME″
   Exit Sub
   End If
   ′改變IME順序
   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) ′取得目前的IME
   NoOfKBDLayout = GetKeyboardLayoutList(25, hKB(0)) ′取得所有IME
   ′ReDim layoutlist(NoOfKBDLayout) As String
   For i = 1 To NoOfKBDLayout
   If ImmIsIME(hKB(i - 1)) = 1 Then ′中文IME
   BuffLen = 255
   RetCount = ImmGetDescription(hKB(i - 1), Buff, BuffLen)
   RetStr = Left(Buff, RetCount)
   Combo1.AddItem RetStr
   Else
   RetStr = ″English (American)″ ′英文IME
   Combo1.AddItem RetStr
   End If
   Next
   ActivateKeyboardLayout hCurKBDLayout, 0 ′恢複原來的IME
   End Sub

運行後,在combobox中選擇目標IME,按下command即可。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.