MicrosoftのWinRTリファレンス
動作確認環境
- Microsoft Visual Studio Community 2019 Preview、Version 16.9.0 Preview 3.0
- Windows デスクトップ ウィザード(空のコンソール アプリケーション)
- C++17
- Microsoft.Windows.CppWinRT 2.0.210122.3
ソースコード
名前空間の省略、元の型の優先
#include "winrt/Windows.Devices.Power.h" using namespace winrt; using namespace Windows::Devices::Power; #include <iostream> int main() { // WinRTをマルチスレッド用に初期化 init_apartment(); // 接続中のすべてのバッテリーコントローラーを表すバッテリーオブジェクトの取得 Battery battery{ Battery::AggregateBattery() }; // デバイスセレクタの文字列表現とバッテリーレポートの取得 hstring device_selector{ battery.GetDeviceSelector() }; BatteryReport report{ battery.GetReport() }; // 出力 const std::array<const wchar_t*, 4> str_BatteryStatus{ L"NoPresent", L"Discharging", L"Idle", L"Charging" }; std::wcout << (std::wstring_view)device_selector << std::endl; std::wcout << str_BatteryStatus[static_cast<int>(report.Status())] << std::endl; return 0; }
名前空間の省略、auto
型の優先
#include "winrt/Windows.Devices.Power.h" using namespace winrt; using namespace Windows::Devices::Power; #include <iostream> int main() { // WinRTをマルチスレッド用に初期化 init_apartment(); // 接続中のすべてのバッテリーコントローラーを表すバッテリーオブジェクトの取得 auto battery{ Battery::AggregateBattery() }; // デバイスセレクタの文字列表現とバッテリーレポートの取得 auto device_selector{ battery.GetDeviceSelector() }; auto report{ battery.GetReport() }; // 出力 const std::array<const wchar_t*, 4> str_BatteryStatus{ L"NoPresent", L"Discharging", L"Idle", L"Charging" }; std::wcout << (std::wstring_view)device_selector << std::endl; std::wcout << str_BatteryStatus[static_cast<int>(report.Status())] << std::endl; return 0; }
名前空間の明記、元の型の優先
#include "winrt/Windows.Devices.Power.h" #include <iostream> int main() { // WinRTをマルチスレッド用に初期化 winrt::init_apartment(); // 接続中のすべてのバッテリーコントローラーを表すバッテリーオブジェクトの取得 winrt::Windows::Devices::Power::Battery battery{ winrt::Windows::Devices::Power::Battery::AggregateBattery() }; // デバイスセレクタの文字列表現とバッテリーレポートの取得 winrt::hstring device_selector{ battery.GetDeviceSelector() }; winrt::Windows::Devices::Power::BatteryReport report{ battery.GetReport() }; // 出力 const std::array<const wchar_t*, 4> str_BatteryStatus{ L"NoPresent", L"Discharging", L"Idle", L"Charging" }; std::wcout << (std::wstring_view)device_selector << std::endl; std::wcout << str_BatteryStatus[static_cast<int>(report.Status())] << std::endl; return 0; }
説明
WinRTの使用に関する事項
#include <winrt/Windows.Devices.Power.h>
ではなく#include "winrt/Windows.Devices.Power.h"
を使用します。C++/WinRTのファイルはMicrosoft.Windows.CppWinRT NuGetパッケージによりプロジェクト以下に最新版が展開されます。<...>
を使用するとSDKの古いファイルが参照されてしまいます。#include "winrt/Windows.Devices.Power.h"
は内部で公開するクラスが必要とするヘッダーファイルを一通りインクルードします。- C++/WinRTのメンバーは
winrt
名前空間に定義されます。公式ドキュメントのサンプルコードにならい、ここでもusing namespace ...;
を使用しています。 using namespace Windows::Devices::Power;
はusing namespace winrt::Windows::Devices::Power;
を先のusing namespace winrt;
により省略したものです。競合がなければ後者を使用しても同じです。
WinRTの一般事項
winrt::init_apartment
はWinRTを初期化します。引数にスレッドモデルを指定でき、既定値はマルチスレッドです。winrt::hstring
はWinRTのカスタム文字列型です。構造体として定義されており、STL準拠のメンバー関数を持ちます。概要はC++/WinRT での文字列の処理 - Microsoft Docsを参照ください。
コード特有の事項
const std::array<const wchar_t*, 4> str_BatteryStatus
は[]
演算子の範囲チェックのために使用しています。