当我知道其进程ID时,如何在Windows 7和C ++中切换到另一个正在运行的应用程序?
我只有进程ID。可能有多个具有相同名称的应用程序,因此使用窗口标题不起作用。我也没有可靠的HWND使用。
我希望应用程序处于活动状态,可见并准备输入,就像我使用alt-tab切换到它一样。
答案 0 :(得分:0)
尝试
HWND hNewWindow = FindWindow(...); // get window handle by title etc.
if (hNewWindow)
{
DWORD hCurrentWindowThread = GetWindowThreadProcessId( hWndCurrentWindow, NULL );
DWORD hNewWindowThread = GetWindowThreadProcessId( hNewWindow, NULL);
AttachThreadInput( hCurrentWindowThread, hNewWindowThread, TRUE );
SetForegroundWindow(hNewWindow);
AttachThreadInput( hCurrentWindowThread, hNewWindowThread, FALSE );
}
SetForegroundWindow正在做你想要的魔术。它具有此处列出的限制SetforegroundWindow() API
来自here.
的解决方案