AttachThreadInput

在一些情况下(比如屏幕软键盘或者输入法程序),自己的窗口没有输入焦点但是想要当前焦点窗口的键盘输入消息,可以使用Win32 API函数AttachThreadInput()来解决这个问题。AttachThreadInput把一个线程(idAttach)的输入消息连接到另外线程(idAttachTo)。
函数原型:

BOOL WINAPI AttachThreadInput(
  __in  DWORD idAttach,
  __in  DWORD idAttachTo,
  __in  BOOL fAttach
);

用法示例:

AttachThreadInput(
         ::GetWindowThreadProcessId(::GetForegroundWindow(),NULL),  //当前焦点窗口的线程ID
         ::GetCurrentThreadId(),  //自己的线程ID
         TRUE);

其他例子:如何在程序A中模拟Tab按键消息发送给前台窗口(foreground window)

// 获取创建前台窗口的线程
DWORD dwThread = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
// 将前台窗口线程贴附到当前线程(也就是程序A中的调用线程)
AttachThreadInput(dwThread, GetCurrentThreadId(), TRUE);
// 获取焦点窗口句柄
HWND hFocus = GetFocus(); 
// 解除贴附
AttachThreadInput(dwThread, GetCurrentThreadId(), FALSE);
// 发送消息
PostMessage(hFocus, WM_KEYDOWN, VK_TAB, 0);
......
https://blog.csdn.net/hellokandy/article/details/50589088

其他参考示例:

// 下面的代码将当前窗口带到带到最顶层,并设置为活动窗口

HWND hForegdWnd = ::GetForegroundWindow();
DWORD dwCurID = ::GetCurrentThreadId();
DWORD dwForeID = ::GetWindowThreadProcessId(hForegdWnd, NULL);
::AttachThreadInput(dwCurID, dwForeID, TRUE);
::SetForegroundWindow(GetSafeHwnd());
::AttachThreadInput(dwCurID, dwForeID, FALSE);
原文地址:https://www.cnblogs.com/rosesmall/p/14794289.html