将创建指定窗口的线程引入前台并激活窗口。 键盘输入将定向到窗口,用户会更改各种视觉提示。 系统为创建前台窗口的线程分配的优先级略高于其他线程。
Syntax
BOOL SetForegroundWindow(
[in] HWND hWnd
);
参数
[in] hWnd
类型:HWND
应激活并带到前台的窗口的句柄。
返回值
类型: BOOL
如果窗口被带到前台,则返回值为非零。
如果未将窗口带到前台,则返回值为零。
注解
系统限制哪些进程可以设置前台窗口。 仅当以下值时,进程才能通过调用 SetForegroundWindow 来设置前台窗口:
- 以下所有条件均为 true:
- 调用过程属于桌面应用程序,而不是针对 Windows 8 或 8.1 设计的 UWP 应用或 Windows 应用商店应用。
- 前台进程尚未通过对 LockSetForegroundWindow 函数的上一次调用来禁用对 SetForegroundWindow 的调用。
- 没有处于活动状态的菜单。
- 此外,至少满足以下条件之一:
- 前台锁定超时已过期(请参阅 SystemParametersInfo 中的SPI_GETFOREGROUNDLOCKTIMEOUT)。
- 调用进程是前台进程。
- 调用进程由前台进程启动。
- 当前没有前台窗口,因此没有前台进程。
- 调用进程收到最后一个输入事件。
- 正在调试前台进程或调用进程。
即使它满足这些条件,进程也有可能被拒绝设置前台窗口的权利。
当用户使用另一个窗口时,应用程序无法强制将窗口强制到前台。 相反,Windows 会闪烁窗口的任务栏按钮以通知用户。
可以设置前台窗口的进程可以通过调用 AllowSetForegroundWindow 函数来启用另一个进程来设置前台窗口。 由 dwProcessId 参数为 AllowSetForegroundWindow 指定的进程将失去在用户下次生成输入时设置前台窗口的能力,除非输入针对该进程,或者下一次进程调用 AllowSetForegroundWindow,除非在上一次对 AllowSetForegroundWindow 的调用中指定了相同的进程。
前台进程可以通过调用 LockSetForegroundWindow 函数来禁用对 SetForegroundWindow 的调用。
Example
下面的代码示例演示如何使用 SetForegroundWindow
// If the window is invisible we will show it and make it topmost without the
// foreground focus. If the window is visible it will also be made the
// topmost window without the foreground focus. If wParam is TRUE then
// for both cases the window will be forced into the foreground focus
if (uMsg == m_ShowStageMessage) {
BOOL bVisible = IsWindowVisible(hwnd);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW |
(bVisible ? SWP_NOACTIVATE : 0));
// Should we bring the window to the foreground
if (wParam == TRUE) {
SetForegroundWindow(hwnd);
}
return (LRESULT) 1;
}
要求
| Requirement | 价值 |
|---|---|
| 最低支持的客户端 | Windows 2000 Professional [仅限桌面应用] |
| 支持的最低服务器 | Windows 2000 Server [仅限桌面应用] |
| 目标平台 | Windows操作系统 |
| Header | winuser.h (包括 Windows.h) |
| Library | User32.lib |
| DLL | User32.dll |
| API 集 | ext-ms-win-ntuser-window-l1-1-0(在 Windows 8 中引入) |
另请参阅
概念
引用