CoAllowSetForegroundWindow 函式 (objbase.h)

此函式會將前景許可權 (許可權,將前景視窗) 從一個進程設定到另一個進程。 具有前景許可權的進程可以呼叫此函式,將該許可權傳遞給本機 COM 伺服器進程。 請注意,呼叫 CoAllowSetForegroundWindow 只會授與許可權;它不會設定前景視窗本身。 只有在目標 COM 伺服器呼叫 SetForegroundWindow 或其他間接執行的 API 時,才會從用戶端應用程式中取回前景和焦點。

語法

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

參數

[in] pUnk

目標 COM 伺服器 Proxy 上 IUnknown 介面的指標。

[in] lpvReserved

此參數是保留的,而且必須是 NULL

傳回值

此函式可以傳回下列值。

傳回碼 描述
S_OK
此方法成功。
E_INVALIDARG
lpvReserved 參數不是 NULL
E_NOINTERFACE
pUnk 參數不支援前景視窗控制件。
E_ACCESSDENIED
呼叫進程目前沒有前景許可權。

備註

系統會限制哪些進程可以呼叫 SetForegroundWindowAllowSetForegroundWindow 函式來設定前景視窗。 因此,即使使用者與其互動,應用程式也不會竊取另一個應用程式的焦點。 使用 CoAllowSetForegroundWindow ,從具有前景許可權的進程傳遞至尚未擁有它的進程。 這可轉移完成:將許可權從一個進程傳遞至另一個進程,然後再傳遞至另一個進程等等。

CoAllowSetForegroundWindow 可讓使用者取得與連結和內嵌) 有關聯之窗口變更 (的 OLE 介面所發生的相同行為。

在幕後, IForegroundTransfer 介面是用來在進程之間產生前景視窗。 標準 COM 提供的 Proxy 已經實作 IForegroundTransfer,因此如果您使用標準 Proxy,就不需要執行任何額外的工作。 只要呼叫 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
程式庫 Ole32.lib
Dll Ole32.dll
API 集合 ext-ms-win-com-ole32-l1-1-1 (在 Windows 8.1) 中引進

另請參閱

IForegroundTransfer