将应用程序置于前台
Put application to foreground
我必须使用它的名字将 window 放在前台,例如 "images"。
有
findWindowW(NULL, stringName)
我得到进程的句柄 (HWND
)。
然后用
SetForegroundWindow(windowHandle);
我想我把它自动放到前台了,但我必须按'Enter'。我做错了什么还是有另一种方法可以做到这一点?我也可以使用进程的 PID。
我的最终目的是在将其放入前台后向该进程发送CTRL + V之类的快捷方式。谢谢。
来自 MSDN
The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
- The process is the foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The process is being debugged.
- The foreground process is not a Modern Application or the Start Screen.
- The foreground is not locked (see LockSetForegroundWindow).
- The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
- No menus are active.
所以,如果你的程序不符合上述情况,则不能自动设置前台。
我认为你可以使用下面的代码来处理你的情况,这个 link 也可以提供帮助。
void SetForegroundWindowForce(HWND hWnd)
{
HWND hWndForeground = ::GetForegroundWindow();
if(hWndForeground == hWnd) return;
DWORD Strange = ::GetWindowThreadProcessId(hWndForeground, NULL);
DWORD My = ::GetWindowThreadProcessId(hWnd, NULL);
if( !::AttachThreadInput(My, Strange, TRUE) )
{
ASSERT(0);
}
::SetForegroundWindow(hWnd);
::BringWindowToTop(hWnd);
if( !::AttachThreadInput(My, Strange, FALSE) )
{
ASSERT(0);
}
}
我必须使用它的名字将 window 放在前台,例如 "images"。 有
findWindowW(NULL, stringName)
我得到进程的句柄 (HWND
)。
然后用
SetForegroundWindow(windowHandle);
我想我把它自动放到前台了,但我必须按'Enter'。我做错了什么还是有另一种方法可以做到这一点?我也可以使用进程的 PID。
我的最终目的是在将其放入前台后向该进程发送CTRL + V之类的快捷方式。谢谢。
来自 MSDN
The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:
- The process is the foreground process.
- The process was started by the foreground process.
- The process received the last input event.
- There is no foreground process.
- The process is being debugged.
- The foreground process is not a Modern Application or the Start Screen.
- The foreground is not locked (see LockSetForegroundWindow).
- The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
- No menus are active.
所以,如果你的程序不符合上述情况,则不能自动设置前台。
我认为你可以使用下面的代码来处理你的情况,这个 link 也可以提供帮助。
void SetForegroundWindowForce(HWND hWnd)
{
HWND hWndForeground = ::GetForegroundWindow();
if(hWndForeground == hWnd) return;
DWORD Strange = ::GetWindowThreadProcessId(hWndForeground, NULL);
DWORD My = ::GetWindowThreadProcessId(hWnd, NULL);
if( !::AttachThreadInput(My, Strange, TRUE) )
{
ASSERT(0);
}
::SetForegroundWindow(hWnd);
::BringWindowToTop(hWnd);
if( !::AttachThreadInput(My, Strange, FALSE) )
{
ASSERT(0);
}
}