Permalink
Browse files

New RadialController sample + bug fix for DPIAwarenessPerWindow sample

* RadialController sample for WinForms and Win32
* Fix bug in DPIAwarenessPerWindow that prevented caption bars
  from being drawn properly.
1 parent 77e53af commit 96f883e4c900948e39660ec14a200a5164a3c7b7 Windows classic samples committed Oct 27, 2016
Showing with 1,457 additions and 1 deletion.
  1. +2 −1 Samples/DPIAwarenessPerWindow/cpp/DpiAwarenessContext.cpp
  2. +278 −0 Samples/RadialController/cpp/DeviceListener.cpp
  3. +51 −0 Samples/RadialController/cpp/DeviceListener.h
  4. +106 −0 Samples/RadialController/cpp/RadialController.cpp
  5. +28 −0 Samples/RadialController/cpp/RadialController.sln
  6. +167 −0 Samples/RadialController/cpp/RadialController.vcxproj
  7. +42 −0 Samples/RadialController/cpp/RadialController.vcxproj.filters
  8. +8 −0 Samples/RadialController/cpp/stdafx.cpp
  9. +15 −0 Samples/RadialController/cpp/stdafx.h
  10. +13 −0 Samples/RadialController/cpp/targetver.h
  11. +6 −0 Samples/RadialController/cs/winforms/App.config
  12. +47 −0 Samples/RadialController/cs/winforms/Form1.Designer.cs
  13. +141 −0 Samples/RadialController/cs/winforms/Form1.cs
  14. +120 −0 Samples/RadialController/cs/winforms/Form1.resx
  15. BIN Samples/RadialController/cs/winforms/Item0.png
  16. +22 −0 Samples/RadialController/cs/winforms/Program.cs
  17. +36 −0 Samples/RadialController/cs/winforms/Properties/AssemblyInfo.cs
  18. +63 −0 Samples/RadialController/cs/winforms/Properties/Resources.Designer.cs
  19. +117 −0 Samples/RadialController/cs/winforms/Properties/Resources.resx
  20. +26 −0 Samples/RadialController/cs/winforms/Properties/Settings.Designer.cs
  21. +7 −0 Samples/RadialController/cs/winforms/Properties/Settings.settings
  22. +27 −0 Samples/RadialController/cs/winforms/RadialControllerInterop.cs
  23. +107 −0 Samples/RadialController/cs/winforms/RadialControllerWinForms.csproj
  24. +28 −0 Samples/RadialController/cs/winforms/RadialControllerWinForms.sln
@@ -299,7 +299,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
// not per-monitor-DPI-aware). This API should be called while
// processing WM_NCCREATE.
EnableNonClientDpiScaling(hWnd);
- return TRUE;
+
+ return DefWindowProc(hWnd, message, wParam, lParam);
}
case WM_CREATE:
@@ -0,0 +1,278 @@
+#include "stdafx.h"
+
+#include "DeviceListener.h"
+#include <windows.h>
+#include <Windowsx.h>
+#include <tchar.h>
+#include <StrSafe.h>
+#include <wrl\implements.h>
+#include <wrl\module.h>
+#include <wrl\event.h>
+#include <roapi.h>
+#include <wrl.h>
+#include <map>
+#include <vector>
+#include <windows.foundation.h>
+#include <windows.foundation.numerics.h>
+#include <windows.foundation.collections.h>
+#include <windows.ui.input.h>
+#include <wrl\client.h>
+#include <winstring.h>
+
+#define RETURN_IF_FAILED(hr) { if(FAILED(hr)) return hr; }
+
+using namespace ABI::Windows::UI::Input;
+using namespace ABI::Windows::Foundation;
+using namespace Microsoft::WRL;
+using namespace Microsoft::WRL::Wrappers;
+
+DeviceListener::DeviceListener(HANDLE console)
+{
+ _console = console;
+}
+
+HRESULT DeviceListener::Init(HWND hwnd)
+{
+ bool isRoInit = false;
+ HRESULT hr = Windows::Foundation::Initialize(RO_INIT_MULTITHREADED);
+
+ if (SUCCEEDED(hr))
+ {
+ PrintMsg(L"RO_INIT_MULTITHREADED SUCCEEDED\n", FOREGROUND_GREEN);
+ isRoInit = true;
+ }
+ else
+ {
+ PrintMsg(L"RO_INIT_MULTITHREADED FAILED\n", FOREGROUND_RED);
+ }
+
+ if (isRoInit)
+ {
+ hr = RegisterForEvents(hwnd);
+
+ if (SUCCEEDED(hr))
+ {
+ hr = PopulateMenuItems();
+ }
+
+ if (SUCCEEDED(hr))
+ {
+ PrintMsg(L"Successfully initialized \n", FOREGROUND_GREEN);
+ }
+ else
+ {
+ PrintMsg(L"Failed to initialize\n", FOREGROUND_RED);
+ }
+ }
+
+ return hr;
+}
+
+HRESULT DeviceListener::SetRotationResolution(double res)
+{
+ return _controller->put_RotationResolutionInDegrees(res);
+}
+
+HRESULT DeviceListener::RegisterForEvents(HWND hwnd)
+{
+ RETURN_IF_FAILED(Windows::Foundation::GetActivationFactory(
+ HStringReference(RuntimeClass_Windows_UI_Input_RadialController).Get(),
+ &_controllerInterop));
+
+ RETURN_IF_FAILED(_controllerInterop->CreateForWindow(hwnd, IID_PPV_ARGS(&_controller)));
+
+ // Wire events
+ RETURN_IF_FAILED(_controller->add_ScreenContactContinued(
+ Callback<ITypedEventHandler<RadialController*, RadialControllerScreenContactContinuedEventArgs*>>(this, &DeviceListener::OnScreenContactContinued).Get(),
+ &_screenContactContinuedToken));
+
+ RETURN_IF_FAILED(_controller->add_ScreenContactStarted(
+ Callback<ITypedEventHandler<RadialController*, RadialControllerScreenContactStartedEventArgs*>>(this, &DeviceListener::OnScreenContactStarted).Get(),
+ &_screenContactStartedToken));
+
+ RETURN_IF_FAILED(_controller->add_ScreenContactEnded(
+ Callback<ITypedEventHandler<RadialController*, IInspectable*>>(this, &DeviceListener::OnScreenContactEnded).Get(),
+ &_screenContactEndedToken));
+
+ RETURN_IF_FAILED(_controller->add_ControlLost(
+ Callback<ITypedEventHandler<RadialController*, IInspectable*>>(this, &DeviceListener::OnControlLost).Get(),
+ &_controlLostToken));
+
+ RETURN_IF_FAILED(_controller->add_ControlAcquired(
+ Callback<ITypedEventHandler<RadialController*, RadialControllerControlAcquiredEventArgs*>>(this, &DeviceListener::OnControlAcquired).Get(),
+ &_controlAcquiredToken));
+
+ RETURN_IF_FAILED(_controller->add_RotationChanged(
+ Callback<ITypedEventHandler<RadialController*, RadialControllerRotationChangedEventArgs*>>(this, &DeviceListener::OnRotationChanged).Get(),
+ &_rotatedToken));
+
+ // Lambda callback
+ RETURN_IF_FAILED(_controller->add_ButtonClicked(
+ Callback<ITypedEventHandler<RadialController*, RadialControllerButtonClickedEventArgs*>>([this]
+ (IRadialController*, IRadialControllerButtonClickedEventArgs* args)
+ {
+ PrintMsg(L"ButtonClicked\n", FOREGROUND_BLUE | FOREGROUND_GREEN);
+ RETURN_IF_FAILED(LogContact(args));
+
+ return S_OK;
+ }).Get(),
+ &_buttonClickedToken));
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::PopulateMenuItems()
+{
+ RETURN_IF_FAILED(_controller->get_Menu(&_menu));
+
+ PrintMsg(L"Got Menu \n", FOREGROUND_BLUE);
+
+ RETURN_IF_FAILED(Windows::Foundation::GetActivationFactory(
+ HStringReference(RuntimeClass_Windows_UI_Input_RadialControllerMenuItem).Get(),
+ &_menuItemStatics));
+
+
+ RETURN_IF_FAILED(AddMenuItemFromKnownIcon(HStringReference(L"My Ink").Get(),
+ RadialControllerMenuKnownIcon::RadialControllerMenuKnownIcon_InkColor));
+
+ RETURN_IF_FAILED(AddMenuItemFromKnownIcon(HStringReference(L"Ruler").Get(),
+ RadialControllerMenuKnownIcon::RadialControllerMenuKnownIcon_Ruler));
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::AddMenuItemFromKnownIcon(_In_ HSTRING itemName, _In_ RadialControllerMenuKnownIcon icon)
+{
+ // Get menu items
+ ComPtr<Collections::IVector<RadialControllerMenuItem*>> menuItems;
+ RETURN_IF_FAILED(_menu->get_Items(&menuItems));
+
+ // Create item
+ ComPtr<IRadialControllerMenuItem> menuItem;
+ RETURN_IF_FAILED(_menuItemStatics->CreateFromKnownIcon(itemName, icon, &menuItem));
+
+
+ // Set Callback
+ RETURN_IF_FAILED(menuItem->add_Invoked(
+ Callback<ITypedEventHandler<RadialControllerMenuItem*, IInspectable*>>(this, &DeviceListener::OnItemInvoked).Get(),
+ &_menuItem2Token));
+
+ // Add item to menu
+ RETURN_IF_FAILED(menuItems->Append(menuItem.Get()));
+
+ // Log new item
+ wchar_t message[2000];
+ swprintf_s(message, 2000, L"Added %s to menu\n", WindowsGetStringRawBuffer(itemName, nullptr));
+ PrintMsg(message, FOREGROUND_BLUE | FOREGROUND_GREEN);
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::OnItemInvoked(_In_ IRadialControllerMenuItem* item, _In_ IInspectable* args)
+{
+ if (item != nullptr)
+ {
+ wchar_t message[2000];
+ HSTRING str;
+
+ item->get_DisplayText(&str);
+ swprintf_s(message, 2000, L"ItemInvoked %s\n", WindowsGetStringRawBuffer(str, nullptr));
+ PrintMsg(message, FOREGROUND_RED | FOREGROUND_GREEN);
+ }
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::OnScreenContactContinued(_In_ IRadialController* /*sender*/, _In_ IRadialControllerScreenContactContinuedEventArgs* args)
+{
+ RETURN_IF_FAILED(LogContact(args));
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::OnScreenContactStarted(_In_ IRadialController* /*sender*/, _In_ IRadialControllerScreenContactStartedEventArgs* args)
+{
+ RETURN_IF_FAILED(LogContact(args));
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::OnScreenContactEnded(_In_ IRadialController* /*sender*/, _In_ IInspectable* args)
+{
+ PrintMsg(L"ScreenContactEnded\n", FOREGROUND_BLUE | FOREGROUND_GREEN);
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::OnControlLost(_In_ IRadialController* /*sender*/, _In_ IInspectable* args)
+{
+ PrintMsg(L"ControlLost\n", FOREGROUND_RED);
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::OnControlAcquired(_In_ IRadialController* /*sender*/, _In_ IRadialControllerControlAcquiredEventArgs* args)
+{
+ PrintMsg(L"ControlAcquired\n", FOREGROUND_GREEN);
+ RETURN_IF_FAILED(LogContact(args));
+
+ return S_OK;
+}
+
+HRESULT DeviceListener::OnRotationChanged(_In_ IRadialController* /*sender*/, _In_ IRadialControllerRotationChangedEventArgs* args)
+{
+ wchar_t message[2000];
+ double delta = 0;
+
+ args->get_RotationDeltaInDegrees(&delta);
+ swprintf_s(message, 2000, L"RotationChanged delta=%lf\n", delta);
+ PrintMsg(message, FOREGROUND_GREEN | FOREGROUND_RED);
+
+ RETURN_IF_FAILED(LogContact(args));
+
+ return S_OK;
+}
+
+template<typename TContactArgs>
+HRESULT DeviceListener::LogContact(_In_ TContactArgs* args)
+{
+ // Get contact
+ ComPtr<IRadialControllerScreenContact> contact;
+ RETURN_IF_FAILED(args->get_Contact(&contact));
+
+ if (contact != nullptr)
+ {
+ // Get contact info
+ Point position;
+ Rect bounds;
+ RETURN_IF_FAILED(contact->get_Position(&position));
+ RETURN_IF_FAILED(contact->get_Bounds(&bounds));
+
+ // Log contact info
+ wchar_t message[2000];
+ swprintf_s(message, 2000, L"\t Postion X=%lf, Y=%lf & \n \t Bounds Height=%lf, Width=%lf, X=%lf, Y=%lf\n",
+ position.X, position.Y,
+ bounds.Height, bounds.Width, bounds.X, bounds.Y);
+
+ PrintMsg(message, FOREGROUND_GREEN | FOREGROUND_BLUE);
+ }
+
+ return S_OK;
+}
+
+void DeviceListener::PrintMsg(WCHAR *message, WORD messageColor)
+{
+ UINT bufferSize = 2000;
+ size_t textLength;
+ DWORD charsWritten;
+ StringCchLength(message, bufferSize, &textLength);
+
+ // Set font to message color
+ SetConsoleTextAttribute(_console, messageColor);
+
+ // Send message
+ WriteConsole(_console, message, (DWORD)textLength, &charsWritten, NULL);
+
+ // Return font to original color
+ SetConsoleTextAttribute(_console, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
+}
@@ -0,0 +1,51 @@
+#pragma once
+
+#include <wrl\client.h>
+#include <windows.ui.input.h>
+#include <RadialControllerInterop.h>
+
+class DeviceListener
+{
+public:
+ DeviceListener(HANDLE console);
+ ~DeviceListener() {}
+
+ HRESULT Init(HWND hwnd);
+ HRESULT SetRotationResolution(double res);
+
+private:
+ HRESULT RegisterForEvents(HWND hwnd);
+ HRESULT PopulateMenuItems();
+ HRESULT AddMenuItemFromKnownIcon(_In_ HSTRING itemName, _In_ ABI::Windows::UI::Input::RadialControllerMenuKnownIcon icon);
+ HRESULT OnItemInvoked(_In_ ABI::Windows::UI::Input::IRadialControllerMenuItem* item, _In_ IInspectable* args);
+
+ HRESULT OnScreenContactContinued(_In_ ABI::Windows::UI::Input::IRadialController*, _In_ ABI::Windows::UI::Input::IRadialControllerScreenContactContinuedEventArgs* args);
+ HRESULT OnScreenContactStarted(_In_ ABI::Windows::UI::Input::IRadialController*, _In_ ABI::Windows::UI::Input::IRadialControllerScreenContactStartedEventArgs* args);
+ HRESULT OnScreenContactEnded(_In_ ABI::Windows::UI::Input::IRadialController* /*sender*/, _In_ IInspectable* args);
+ HRESULT OnControlLost(_In_ ABI::Windows::UI::Input::IRadialController* /*sender*/, _In_ IInspectable* args);
+ HRESULT OnControlAcquired(_In_ ABI::Windows::UI::Input::IRadialController* /*sender*/, _In_ ABI::Windows::UI::Input::IRadialControllerControlAcquiredEventArgs* args);
+ HRESULT OnRotationChanged(_In_ ABI::Windows::UI::Input::IRadialController* sender, _In_ ABI::Windows::UI::Input::IRadialControllerRotationChangedEventArgs* args);
+
+ template<typename TContactArgs>
+ HRESULT LogContact(_In_ TContactArgs* args);
+ void PrintMsg(WCHAR *message, WORD messageColor);
+
+ DWORD _threadId = 0;
+ HANDLE _console = nullptr;
+
+ Microsoft::WRL::ComPtr<IRadialControllerInterop> _controllerInterop;
+ Microsoft::WRL::ComPtr<ABI::Windows::UI::Input::IRadialController> _controller;
+ Microsoft::WRL::ComPtr<ABI::Windows::UI::Input::IRadialControllerMenu> _menu;
+ Microsoft::WRL::ComPtr<ABI::Windows::UI::Input::IRadialControllerMenuItemStatics> _menuItemStatics;
+
+ EventRegistrationToken _rotatedToken;
+ EventRegistrationToken _screenContactStartedToken;
+ EventRegistrationToken _screenContactEndedToken;
+ EventRegistrationToken _screenContactContinuedToken;
+ EventRegistrationToken _controlLostToken;
+ EventRegistrationToken _controlAcquiredToken;
+ EventRegistrationToken _buttonClickedToken;
+ EventRegistrationToken _menuItem1Token;
+ EventRegistrationToken _menuItem2Token;
+};
+
Oops, something went wrong.

0 comments on commit 96f883e

Please sign in to comment.