IDisplayInformationStaticsInterop::GetForWindow 方法 (windows.graphics.display.interop.h)
擷取指定視窗的 DisplayInformation 物件。 GetForWindow 一律會配置並傳回新的 DisplayInformation。
語法
HRESULT GetForWindow(
HWND window,
REFIID riid,
void **displayInfo
);
參數
window
類型: [in] HWND
視窗的控制碼。
riid
類型: [in] REFIID
DisplayInformation類別的GUID。
displayInfo
類型: [iid_is][retval][out] void**
記憶體區塊的指標,接收傳回 之 DisplayInformation 物件的指標。
傳回值
類型: HRESULT
如果函式成功,它會傳回 S_OK。 否則,它會傳回 HRESULT錯誤碼。
備註
因此 ,DisplayInformation 可以處理視窗移動和 DPI 變更訊息,它會攔截 HWND的訊息迴圈。 為了確保順利發生, GetForWindow 具有下列需求:
- 視窗的引數必須是目前線程所擁有的最上層視窗HWND。
- 目前的執行緒必須執行 Windows.System.DispatcherQueue ,才能接收事件。
- 目前的執行緒可以是 MTA 或 STA。
您必須負責:只要視窗的引數相關,快取已建立的DisplayInformation;取消註冊事件處理常式;並卸載最後一個參考,以終結DisplayInformation實例。
範例
對於轉譯寬色遊戲和高動態範圍內容的應用程式而言,請務必動態調整以變更監視器的條件;或在監視器之間移動時。 在膝上型電腦上,使用者可以調整螢幕的亮度,而且可以調整提供給應用程式的音調對應參數。
// It's safe, and recommended, to cache the DisplayInformation created from an HWND,
// since it safely provides the latest information and event handlers for when
// changes take place.
#include <Windows.Graphics.Display.Interop.h>
#include <winrt/Windows.Graphics.Display.h>
using namespace winrt::Windows::Graphics::Display;
...
void ReadHdrParametersFromDisplayInformation(HWND myWindow)
{
auto factory{ winrt::get_activation_factory<DisplayInformation,
IDisplayInformationStaticsInterop>() };
DisplayInformation displayInfo{ nullptr };
winrt::check_hresult(
factory->GetForWindow(
myWindow,
winrt::guid_of<DisplayInformation>(),
winrt::put_abi(displayInfo)
)
);
auto colorInfo{ displayInfo.GetAdvancedColorInfo() };
// Here you can read colorInfo properties such as:
// * CurrentAdvancedColorKind
// * RedPrimary, BluePrimary, GreenPrimary, WhitePoint
// * MinLuminanceInNits, MaxLuminanceInNits
// * MaxAverageFullFrameLuminanceInNits, SdrWhiteLevelInNits
// ... and adapt your rendering.
// You can also subscribe event handlers to listen for changes:
displayInfo.AdvancedColorInfoChanged(
[&](auto sender, auto args)
{
// Handle the event.
}
);
// Cache the DisplayInformation object for as long as your window
// is alive: it always provides fresh data for your window.
}
規格需求
最低支援的用戶端 | Windows 11組建 22621 |
標頭 | windows.graphics.display.interop.h |
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應