VB编程控制Windows中文输入法
2008-04-09 04:44:47来源:互联网 阅读 ()
它的原理是:使用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即可。
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash