VB关闭其他进程的输入法

新建上面的几个按钮和list,打开记事本或者win32pad,单击向记事本发送消息,程序会按ctrl+空格关闭中文输入法,并发送aaa到记事本。只适用于前台进程。

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_MOUSEACTIVATE = &H21
‘以下几个API用于关闭前台进程输入法:
‘-------------------------------------------------------------------------------------------------------
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function GetCurrentThread Lib "kernel32" () As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetKeyboardLayout Lib "user32" (ByVal dwLayout As Long) As Long
Private Declare Function ImmIsIME Lib "imm32.dll" (ByVal hkl 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 Sub Command1_Click()
‘查找记事本句柄
Dim dHwnd As Long
Dim tHwnd As Long
dHwnd = FindWindow("Notepad", vbNullString)
If dHwnd > 0 Then
tHwnd = FindWindowEx(dHwnd, ByVal 0&, "Edit", vbNullString)
End If
SetForegroundWindow tHwnd
SendMessage tHwnd, 33, 0, 0
Sleep 2000 ‘延迟2秒确保窗口带到前台
‘以下代码把前台进程的输入法关闭:
‘-------------------------------------------------------------------------------------------------------
Dim hwnd As Long
Dim hCurThread As Long
Dim sCaption As String
Dim sBuffer As String
Dim hCurKBDLayout As Long
hwnd = GetForegroundWindow
sCaption = Space(255)
GetWindowText hwnd, sCaption, 255
If InStr(sCaption, Chr(0)) Then
sCaption = Left(sCaption, InStr(sCaption, Chr(0)) - 1)
End If
hCurThread = GetWindowThreadProcessId(hwnd, ByVal 0&)
hCurKBDLayout = GetKeyboardLayout(hCurThread)
If ImmIsIME(hCurKBDLayout) = 1 Then
sBuffer = Space(255)
RetCount = ImmGetDescription(ByVal hCurKBDLayout, sBuffer, 255)
sBuffer = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
Else
sBuffer = "English(American)"
End If
List1.AddItem "当前窗口标题:" & sCaption
List1.AddItem " 当前输入法: " & sBuffer
If sBuffer <> "English(American)" Then SendKeys "^ "
Sleep 1000 ‘延迟1秒确保ctrl+空格生效
‘-------------------------------------------------------------------------------------------------------
SendKeys "aaa"
‘ MsgBox pwszKLID
End Sub

Private Sub Command2_Click()
‘查找win32pad句柄
Dim dHwnd As Long
Dim tHwnd As Long
dHwnd = FindWindow("win32padClass", vbNullString)
If dHwnd > 0 Then
tHwnd = FindWindowEx(dHwnd, ByVal 0&, "RichEdit20A", vbNullString)
End If
SetForegroundWindow tHwnd ‘把父窗体带到前台
SendMessage tHwnd, 33, 0, 0 ‘把子窗体带到前台
Sleep 2000 ‘延迟2秒确保窗口带到前台
‘以下代码把前台进程的输入法关闭:
‘-------------------------------------------------------------------------------------------------------
Dim hwnd As Long
Dim hCurThread As Long
Dim sCaption As String
Dim sBuffer As String
Dim hCurKBDLayout As Long
hwnd = GetForegroundWindow
sCaption = Space(255)
GetWindowText hwnd, sCaption, 255
If InStr(sCaption, Chr(0)) Then
sCaption = Left(sCaption, InStr(sCaption, Chr(0)) - 1)
End If
hCurThread = GetWindowThreadProcessId(hwnd, ByVal 0&)
hCurKBDLayout = GetKeyboardLayout(hCurThread)
If ImmIsIME(hCurKBDLayout) = 1 Then
sBuffer = Space(255)
RetCount = ImmGetDescription(ByVal hCurKBDLayout, sBuffer, 255)
sBuffer = Left(sBuffer, InStr(sBuffer, Chr(0)) - 1)
Else
sBuffer = "English(American)"
End If
List1.AddItem "当前窗口标题:" & sCaption
List1.AddItem " 当前输入法: " & sBuffer
If sBuffer <> "English(American)" Then SendKeys "^ "
Sleep 1000 ‘延迟1秒确保ctrl+空格生效
‘-------------------------------------------------------------------------------------------------------
SendKeys "aaa"
End Sub

Private Sub Command3_Click()
End
End Sub

时间: 2024-08-07 00:13:25

VB关闭其他进程的输入法的相关文章

java linux 项目经常无故被关闭 进程无故消息

布了几个项目.居然天天会自动的挂掉.急了.花时间解决了一下.总结方案如下: 1.磁盘满了.这大家都懂,清一下 2.tomcat在关闭的或是重启的时候,常常后台进程没有被关闭.需要用ps aux|grep java 这个命令查一下,把多余的进程关掉,再启动startup.sh 3.这种情况比较少见,就是在系统资源缺少的情况下,被系统自动DOWN掉,或是被其它软件干掉了. 其实在我这里,这样还是没有解决,进程还是莫名其妙会自己挂掉,日志也没有任何报错.后来打开tomcat主目录下的子目录conf里面

Win7怎么关闭多余进程,怎么关闭无法关闭进程

我们使用电脑的时候,有时候会发现电脑的速度突然变慢了,这时候我们可以查看下自己电脑的进程,看看有没有非常占用CPU的无关进程在使用,这时候我们可以关闭一些占用CPU系统资源比较多的无用进程,但是提醒大家关闭进程大家一定要谨慎操作,否则会导致电脑上一些程序或应用就无法使用哦,可能还有些时候大家发现为啥有些进程无法关闭呢?总是提示无法中止进程,怎么办呢?其实我们不需要使用第三方工具,我们使用电脑的一些DOS命令或者电脑操作就可以进行关闭,这里小编给大家介绍下关闭系统进程的操作方法.希望大家需要时可以

VB.net结束进程

Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click On Error GoTo Errmessages '在做系统操作时加排错标签是个好习惯 Dim TargetName As String = "WX" '存储进程名为文本型,注:进程名不加扩展名 Dim TargetKill() As Process = Process.GetProc

java 强制关闭win7进程

package com.cmd.core; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CmdCore { public static boolean isRunning(String exeName) { Process proc = null; try { proc = Runtime.getRuntime().exec("t

java linux 项目常常无故被关闭 进程无故消息

布了几个项目.竟然天天会自己主动的挂掉.急了.花时间攻克了一下.总结方案例如以下: 1.磁盘满了.这大家都懂,清一下 2.tomcat在关闭的或是重新启动的时候,经常后台进程没有被关闭.须要用ps aux|grep java 这个命令查一下,把多余的进程关掉,再启动startup.sh 3.这样的情况比較少见,就是在系统资源缺少的情况下,被系统自己主动DOWN掉,或是被其他软件干掉了. 事实上在我这里,这样还是没有解决,进程还是莫名其妙会自己挂掉,日志也没有不论什么报错.后来打开tomcat主文

[转]关闭word进程

命名空间 :System.Diagnostics 以前在word的时候,经常碰到word进程产生一大堆,怕关错了,把用户自己打开的word也关闭,一直搞忽悠,今天上网花了10块钱,下了个文件,给我了一点启发,总于可以实现只关闭程序中打开的,,,我自己测试了,是可以实现的,各位也可以copy去试试,不行的话,给我留言... //以下的定义为打开或保存word文件时,需要用到得参数 object confirmConversions = Type.Missing;                ob

c# 关闭软件 进程 杀死进程

c# 关闭软件 进程  杀死进程 foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcessesByName("Server")) { p.Kill(); }

关闭IE进程出错,提示拒绝访问

解决办法:  对关闭的进程设置一些属性,然后再执行process.kill() 代码 1 System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses(); 2 3 foreach (System.Diagnostics.Process myProcess in myProcesses) 4 { 5 if (myProcess.ProcessName.ToUpper() == "IEXPLOR

C#如何关闭指定进程

public static void KillProcess(string strProcessesByName)//关闭线程 { foreach (Process p in Process.GetProcesses())//GetProcessesByName(strProcessesByName)) { if (p.ProcessName.ToUpper().Contains("你要关闭的进程名字")) { try { p.Kill(); p.WaitForExit(); // p