VBでAttachThreadInputを使って別のプロセスのIME状態を変更する方法は?

Visual BasicでAttachThreadInput 関数を使用して他のプロセスの入力方式のステータスを変更するには、最初にAttachThreadInput 関数と関連パラメータの宣言を宣言します。次に、GetWindowThreadProcessId 関数を使用してターゲット プロセスのスレッド ID を取得し、AttachThreadInput 関数を呼び出してターゲット プロセスの入力方式のステータスを変更します。

AttachThreadInput 関数を使用して別のプロセスの入力メソッド状態を変更するコードの例を以下に示します:

Imports System.Runtime.InteropServices

Public Class Form1
    ' 声明AttachThreadInput函数
    <DllImport("user32.dll")>
    Private Shared Function AttachThreadInput(ByVal idAttach As Integer, ByVal idAttachTo As Integer, ByVal fAttach As Boolean) As Boolean
    End Function

    ' 声明GetWindowThreadProcessId函数
    <DllImport("user32.dll")>
    Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
    End Function

    ' 改变其他进程的输入法状态
    Private Sub ChangeInputMethod(ByVal hWnd As IntPtr, ByVal enable As Boolean)
        Dim processId As Integer = 0
        Dim threadId As Integer = GetWindowThreadProcessId(hWnd, processId)
        If threadId <> 0 Then
            AttachThreadInput(threadId, 0, enable)
        End If
    End Sub

    ' 测试改变输入法状态按钮的点击事件
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim hWnd As IntPtr = Process.GetProcessesByName("目标进程名称")(0).MainWindowHandle
        If hWnd <> IntPtr.Zero Then
            ' 改变输入法状态为启用
            ChangeInputMethod(hWnd, True)
        End If
    End Sub
End Class

実際の対象プロセス名に「対象プロセス名」を必ず置き換えてください。また、この方法を使用するには、管理者権限で実行する必要があります。

bannerAds