CoAllowSetForegroundWindow 函数 (objbase.h)

此函数传递前台特权 (特权,以将前台窗口) 从一个进程设置为另一个进程。 具有前台特权的进程可以调用此函数,将该特权传递给本地 COM 服务器进程。 请注意,调用 CoAllowSetForegroundWindow 仅授予权限;它不会设置前景窗口本身。 仅当目标 COM 服务器调用 SetForegroundWindow 或间接调用的另一个 API 时,才会从客户端应用程序中移走前台和焦点。

语法

HRESULT CoAllowSetForegroundWindow(
  [in] IUnknown *pUnk,
  [in] LPVOID   lpvReserved
);

参数

[in] pUnk

指向目标 COM 服务器的代理上的 IUnknown 接口的指针。

[in] lpvReserved

此参数是保留的,必须为 NULL

返回值

此函数可以返回以下值。

返回代码 说明
S_OK
方法成功。
E_INVALIDARG
lpvReserved 参数不为 NULL
E_NOINTERFACE
pUnk 参数不支持前台窗口控件。
E_ACCESSDENIED
调用进程当前不具有前台权限。

注解

系统限制哪些进程可以调用 SetForegroundWindowAllowSetForegroundWindow 函数来设置前台窗口。 因此,即使用户与应用程序交互,应用程序也会被阻止从另一个应用程序窃取焦点。 使用 CoAllowSetForegroundWindow 将前台权限从具有前台权限的进程传递给尚未拥有它的进程。 这可以通过传递方式完成:将权限从一个进程传递到另一个进程,然后传递到另一个进程,依此类举。

CoAllowSetForegroundWindow 使具有自定义界面的用户能够获取与 OLE 接口相同的行为,其中窗口的更改预期 (主要与链接和嵌入) 相关联。

在后台, IForegroundTransfer 接口用于生成进程之间的前台窗口。 标准 COM 提供的代理已实现 IForegroundTransfer,因此,如果使用标准代理,则无需执行任何额外工作。 只需调用 CoAllowSetForegroundWindow ,即可将前台权限转移到任何进程外 COM 对象。

示例

以下示例演示客户端进程如何创建本地 COM 服务器,调用 CoAllowSetForegroundWindow 以传输前台权限,然后在 COM 服务器上调用函数,而该函数又直接或间接调用 SetForegroundWindow

Microsoft::WRL::ComPtr<IExampleInterface> exampleLocalServer;

ThrowIfFailed(::CoCreateInstance(CLSID_ExampleLocalServer,
	nullptr, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&exampleLocalServer)));

// You can adapt to success or failure, but don't automatically throw. Don’t make the
// opening of a window dependent on successfully passing privilege (and taking foreground),
// because the window will signal to the user that it is ready to take focus.
HRESULT hr = ::CoAllowSetForegroundWindow(exampleLocalServer.Get(), nullptr);

// Call an example method that itself calls ::SetForegroundWindow(HWND).
hr = exampleLocalServer->FunctionThatSetsForegroundWindow();

要求

要求
最低受支持的客户端 Windows 2000 Professional [仅限桌面应用]
最低受支持的服务器 Windows 2000 Server [仅限桌面应用]
目标平台 Windows
标头 objbase.h
Library Ole32.lib
DLL Ole32.dll
API 集 在 Windows 8.1) 中引入的 ext-ms-win-com-ole32-l1-1-1 (

另请参阅

IForegroundTransfer