VB to close other process input methods

Source: Internet
Author: User

Create a few buttons and list above, open Notepad or Win32pad, click to send a message to Notepad, the program will press CTRL + SPACEBAR to close the Chinese input method, and send AAA to Notepad. Applies only to foreground processes.

Private Declare Function FindWindow Lib "user32" Alias "Findwindowa" (ByVal lpclassname as String, ByVal Lpwindowname as S Tring) 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 WPar Am 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 WPar Am as long, lParam as any) as long
Private Const wm_mouseactivate = &h21
The following APIs are used to close the foreground process Input method:
‘-------------------------------------------------------------------------------------------------------
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 St Ring, ByVal Ubuflen as long) as long
‘-------------------------------------------------------------------------------------------------------
Private Sub Command1_Click ()
' Find Notepad handle
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 ' Delay 2 seconds ensure the window is brought to the foreground
The following code closes the input method of the foreground process:
‘-------------------------------------------------------------------------------------------------------
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 = "中文版" (American)
End If
List1.AddItem "Current Window title:" & Scaption
List1.AddItem "Current Input Method:" & Sbuffer
If sbuffer <> 中文版 (American) then SendKeys "^"
Sleep 1000 ' delay 1 seconds make sure CTRL + SPACEBAR is in effect
‘-------------------------------------------------------------------------------------------------------
SendKeys "AAA"
' MsgBox pwszklid
End Sub

Private Sub Command2_Click ()
' Find Win32pad handle
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 ' Bring the parent form to the foreground
SendMessage Thwnd, 33, 0, 0 ' take the form to the front
Sleep 2000 ' Delay 2 seconds ensure the window is brought to the foreground
The following code closes the input method of the foreground process:
‘-------------------------------------------------------------------------------------------------------
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 = "中文版" (American)
End If
List1.AddItem "Current Window title:" & Scaption
List1.AddItem "Current Input Method:" & Sbuffer
If sbuffer <> 中文版 (American) then SendKeys "^"
Sleep 1000 ' delay 1 seconds make sure CTRL + SPACEBAR is in effect
‘-------------------------------------------------------------------------------------------------------
SendKeys "AAA"
End Sub

Private Sub Command3_Click ()
End
End Sub

VB to close other process input methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.