Metro, ユニバーサルアプリ, HWND


ユニバーサルアプリでHWNDを必要とする例は滅多にないだろうけど、こんな方法で取得できるらしい。


extern "C" int __stdcall GetWindowTextW(HWND hwnd, PWSTR text, int count);
#pragma comment (lib, "user32.lib")

struct __declspec(uuid("45D64A29-A63E-4CB6-B498-5781D298CB4F")) __declspec(novtable)
ICoreWindowInterop : IUnknown
{
virtual HRESULT __stdcall get_WindowHandle(HWND* hwnd) = 0;
virtual HRESULT __stdcall put_MessageHandled(unsigned char) = 0;
};

// AppはWindows::ApplicationModel::Core::IFrameworkViewを継承したクラス
void App::SetWindow(CoreWindow^ window)
{
...

// Metroアプリのウインドウハンドル
HWND hWnd;
Microsoft::WRL::ComPtr interop;

HRESULT hr = ( (IUnknown*)window)->QueryInterface(interop.GetAddressOf());

interop->get_WindowHandle(&hWnd);

  // 試しにTextを取得してみる、user32.libをリンク
WCHAR text[100];
GetWindowTextW(hWnd, text, 100 ); // text is equal to app name.

}