This source file includes following definitions.
- WndProc
- CreateMetroTopLevelWindow
- get_HasThreadAccess
- ProcessEvents
- RunAsync
- RunIdleAsync
- add_AcceleratorKeyActivated
- remove_AcceleratorKeyActivated
- get_AutomationHostProvider
- get_Bounds
- get_CustomProperties
- get_Dispatcher
- get_FlowDirection
- put_FlowDirection
- get_IsInputEnabled
- put_IsInputEnabled
- get_PointerCursor
- put_PointerCursor
- get_PointerPosition
- get_Visible
- Activate
- Close
- GetAsyncKeyState
- GetKeyState
- ReleasePointerCapture
- SetPointerCapture
- add_Activated
- remove_Activated
- add_AutomationProviderRequested
- remove_AutomationProviderRequested
- add_CharacterReceived
- remove_CharacterReceived
- add_Closed
- remove_Closed
- add_InputEnabled
- remove_InputEnabled
- add_KeyDown
- remove_KeyDown
- add_KeyUp
- remove_KeyUp
- add_PointerCaptureLost
- remove_PointerCaptureLost
- add_PointerEntered
- remove_PointerEntered
- add_PointerExited
- remove_PointerExited
- add_PointerMoved
- remove_PointerMoved
- add_PointerPressed
- remove_PointerPressed
- add_PointerReleased
- remove_PointerReleased
- add_TouchHitTesting
- remove_TouchHitTesting
- add_PointerWheelChanged
- remove_PointerWheelChanged
- add_SizeChanged
- remove_SizeChanged
- add_VisibilityChanged
- remove_VisibilityChanged
- get_WindowHandle
- put_MessageHandled
- get_Kind
- get_PreviousExecutionState
- get_SplashScreen
- Activate
- get_CoreWindow
- add_Activated
- remove_Activated
- get_IsMain
- get_IsHosted
- get_Id
- add_Suspending
- remove_Suspending
- add_Resuming
- remove_Resuming
- get_Properties
- GetCurrentView
- Run
- RunWithActivationFactories
- Exit
- add_Exiting
- remove_Exiting
- InitWindows7
#include "stdafx.h"
#include <corewindow.h>
#include "base/logging.h"
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
WPARAM wparam, LPARAM lparam) {
PAINTSTRUCT ps;
HDC hdc;
switch (message) {
case WM_PAINT:
hdc = ::BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
break;
case WM_LBUTTONUP:
::InvalidateRect(hwnd, NULL, TRUE);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return ::DefWindowProc(hwnd, message, wparam, lparam);
}
return 0;
}
HWND CreateMetroTopLevelWindow() {
HINSTANCE hInst = reinterpret_cast<HINSTANCE>(&__ImageBase);
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInst;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_INACTIVECAPTION+1);
wcex.lpszMenuName = 0;
wcex.lpszClassName = L"Windows.UI.Core.CoreWindow";
wcex.hIconSm = 0;
HWND hwnd = ::CreateWindowExW(0,
MAKEINTATOM(::RegisterClassExW(&wcex)),
L"metro_win7",
WS_POPUP | WS_VISIBLE,
0, 0, 1024, 1024,
NULL, NULL, hInst, NULL);
return hwnd;
}
typedef winfoundtn::ITypedEventHandler<
winapp::Core::CoreApplicationView*,
winapp::Activation::IActivatedEventArgs*> ActivatedHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::WindowActivatedEventArgs*> WindowActivatedHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::AutomationProviderRequestedEventArgs*>
AutomationProviderHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::CharacterReceivedEventArgs*> CharEventHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::CoreWindowEventArgs*> CoreWindowEventHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::InputEnabledEventArgs*> InputEnabledEventHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::KeyEventArgs*> KeyEventHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::PointerEventArgs*> PointerEventHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::WindowSizeChangedEventArgs*> SizeChangedHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::TouchHitTestingEventArgs*> TouchHitTestHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreWindow*,
winui::Core::VisibilityChangedEventArgs*> VisibilityChangedHandler;
typedef winfoundtn::ITypedEventHandler<
winui::Core::CoreDispatcher*,
winui::Core::AcceleratorKeyEventArgs*> AcceleratorKeyEventHandler;
class CoreDispacherEmulation :
public mswr::RuntimeClass<
winui::Core::ICoreDispatcher,
winui::Core::ICoreAcceleratorKeys> {
public:
virtual HRESULT STDMETHODCALLTYPE get_HasThreadAccess(boolean* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE ProcessEvents(
winui::Core::CoreProcessEventsOption options) {
if (options != winui::Core::CoreProcessEventsOption_ProcessUntilQuit)
return E_FAIL;
MSG msg = {0};
while(::GetMessage(&msg, NULL, 0, 0) != 0) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE RunAsync(
winui::Core::CoreDispatcherPriority priority,
winui::Core::IDispatchedHandler *agileCallback,
ABI::Windows::Foundation::IAsyncAction** asyncAction) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE RunIdleAsync(
winui::Core::IIdleDispatchedHandler *agileCallback,
winfoundtn::IAsyncAction** asyncAction) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_AcceleratorKeyActivated(
AcceleratorKeyEventHandler* handler,
EventRegistrationToken *pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_AcceleratorKeyActivated(
EventRegistrationToken cookie) {
return S_OK;
}
};
class CoreWindowEmulation
: public mswr::RuntimeClass<
mswr::RuntimeClassFlags<mswr::WinRtClassicComMix>,
winui::Core::ICoreWindow, ICoreWindowInterop> {
public:
CoreWindowEmulation() : core_hwnd_(NULL) {
dispatcher_ = mswr::Make<CoreDispacherEmulation>();
core_hwnd_ = CreateMetroTopLevelWindow();
}
~CoreWindowEmulation() {
::DestroyWindow(core_hwnd_);
}
virtual HRESULT STDMETHODCALLTYPE get_AutomationHostProvider(
IInspectable** value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_Bounds(
winfoundtn::Rect* value) {
RECT rect;
if (!::GetClientRect(core_hwnd_, &rect))
return E_FAIL;
value->Width = rect.right;
value->Height = rect.bottom;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_CustomProperties(
winfoundtn::Collections::IPropertySet** value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_Dispatcher(
winui::Core::ICoreDispatcher** value) {
return dispatcher_.CopyTo(value);
}
virtual HRESULT STDMETHODCALLTYPE get_FlowDirection(
winui::Core::CoreWindowFlowDirection* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE put_FlowDirection(
winui::Core::CoreWindowFlowDirection value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_IsInputEnabled(
boolean* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE put_IsInputEnabled(
boolean value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_PointerCursor(
winui::Core::ICoreCursor** value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE put_PointerCursor(
winui::Core::ICoreCursor* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_PointerPosition(
winfoundtn::Point* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_Visible(
boolean* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE Activate(void) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE Close(void) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE GetAsyncKeyState(
ABI::Windows::System::VirtualKey virtualKey,
winui::Core::CoreVirtualKeyStates* KeyState) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE GetKeyState(
ABI::Windows::System::VirtualKey virtualKey,
winui::Core::CoreVirtualKeyStates* KeyState) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE ReleasePointerCapture(void) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE SetPointerCapture(void) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_Activated(
WindowActivatedHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_Activated(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_AutomationProviderRequested(
AutomationProviderHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_AutomationProviderRequested(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_CharacterReceived(
CharEventHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_CharacterReceived(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_Closed(
CoreWindowEventHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_Closed(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_InputEnabled(
InputEnabledEventHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_InputEnabled(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_KeyDown(
KeyEventHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_KeyDown(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_KeyUp(
KeyEventHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_KeyUp(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_PointerCaptureLost(
PointerEventHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_PointerCaptureLost(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_PointerEntered(
PointerEventHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_PointerEntered(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_PointerExited(
PointerEventHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_PointerExited(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_PointerMoved(
PointerEventHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_PointerMoved(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_PointerPressed(
PointerEventHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_PointerPressed(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_PointerReleased(
PointerEventHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_PointerReleased(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_TouchHitTesting(
TouchHitTestHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_TouchHitTesting(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_PointerWheelChanged(
PointerEventHandler* handler,
EventRegistrationToken* cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_PointerWheelChanged(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_SizeChanged(
SizeChangedHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_SizeChanged(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_VisibilityChanged(
VisibilityChangedHandler* handler,
EventRegistrationToken* pCookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_VisibilityChanged(
EventRegistrationToken cookie) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_WindowHandle(HWND* hwnd) {
if (!core_hwnd_)
return E_FAIL;
*hwnd = core_hwnd_;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE put_MessageHandled(
boolean value) {
return S_OK;
}
private:
HWND core_hwnd_;
mswr::ComPtr<winui::Core::ICoreDispatcher> dispatcher_;
};
class ActivatedEvent
: public mswr::RuntimeClass<winapp::Activation::IActivatedEventArgs> {
public:
ActivatedEvent(winapp::Activation::ActivationKind activation_kind)
: activation_kind_(activation_kind) {
}
virtual HRESULT STDMETHODCALLTYPE get_Kind(
winapp::Activation::ActivationKind *value) {
*value = activation_kind_;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_PreviousExecutionState(
winapp::Activation::ApplicationExecutionState *value) {
*value = winapp::Activation::ApplicationExecutionState_ClosedByUser;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_SplashScreen(
winapp::Activation::ISplashScreen **value) {
return E_FAIL;
}
private:
winapp::Activation::ActivationKind activation_kind_;
};
class CoreApplicationViewEmulation
: public mswr::RuntimeClass<winapp::Core::ICoreApplicationView> {
public:
CoreApplicationViewEmulation() {
core_window_ = mswr::Make<CoreWindowEmulation>();
}
HRESULT Activate() {
if (activated_handler_) {
auto ae = mswr::Make<ActivatedEvent>(
winapp::Activation::ActivationKind_File);
return activated_handler_->Invoke(this, ae.Get());
} else {
return S_OK;
}
}
virtual HRESULT STDMETHODCALLTYPE get_CoreWindow(
winui::Core::ICoreWindow** value) {
if (!core_window_)
return E_FAIL;
return core_window_.CopyTo(value);
}
virtual HRESULT STDMETHODCALLTYPE add_Activated(
ActivatedHandler* handler,
EventRegistrationToken* token) {
if (activated_handler_)
return E_FAIL;
activated_handler_ = handler;
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_Activated(
EventRegistrationToken token) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_IsMain(
boolean* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_IsHosted(
boolean* value) {
return S_OK;
}
private:
mswr::ComPtr<winui::Core::ICoreWindow> core_window_;
mswr::ComPtr<ActivatedHandler> activated_handler_;
};
class CoreApplicationWin7Emulation
: public mswr::RuntimeClass<winapp::Core::ICoreApplication,
winapp::Core::ICoreApplicationExit> {
public:
virtual HRESULT STDMETHODCALLTYPE get_Id(
HSTRING* value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_Suspending(
winfoundtn::IEventHandler<winapp::SuspendingEventArgs*>* handler,
EventRegistrationToken* token) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_Suspending(
EventRegistrationToken token) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_Resuming(
winfoundtn::IEventHandler<IInspectable*>* handler,
EventRegistrationToken* token) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_Resuming(
EventRegistrationToken token) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE get_Properties(
winfoundtn::Collections::IPropertySet** value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE GetCurrentView(
winapp::Core::ICoreApplicationView** value) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE Run(
winapp::Core::IFrameworkViewSource* viewSource) {
HRESULT hr = viewSource->CreateView(app_view_.GetAddressOf());
if (FAILED(hr))
return hr;
view_emulation_ = mswr::Make<CoreApplicationViewEmulation>();
hr = app_view_->Initialize(view_emulation_.Get());
if (FAILED(hr))
return hr;
mswr::ComPtr<winui::Core::ICoreWindow> core_window;
hr = view_emulation_->get_CoreWindow(core_window.GetAddressOf());
if (FAILED(hr))
return hr;
hr = app_view_->SetWindow(core_window.Get());
if (FAILED(hr))
return hr;
hr = app_view_->Load(NULL);
if (FAILED(hr))
return hr;
hr = view_emulation_->Activate();
if (FAILED(hr))
return hr;
return app_view_->Run();
}
virtual HRESULT STDMETHODCALLTYPE RunWithActivationFactories(
winfoundtn::IGetActivationFactory* activationFactoryCallback) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE Exit(void) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE add_Exiting(
winfoundtn::IEventHandler<IInspectable*>* handler,
EventRegistrationToken* token) {
return S_OK;
}
virtual HRESULT STDMETHODCALLTYPE remove_Exiting(
EventRegistrationToken token) {
return S_OK;
}
private:
mswr::ComPtr<winapp::Core::IFrameworkView> app_view_;
mswr::ComPtr<CoreApplicationViewEmulation> view_emulation_;
};
mswr::ComPtr<winapp::Core::ICoreApplication> InitWindows7() {
HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr))
CHECK(false);
return mswr::Make<CoreApplicationWin7Emulation>();
}