2020-03-24 01:44:02 +08:00
|
|
|
#include "pch.h"
|
2020-03-06 07:52:59 +08:00
|
|
|
#include "EditKeyboardWindow.h"
|
|
|
|
|
|
|
|
LRESULT CALLBACK EditKeyboardWindowProc(HWND, UINT, WPARAM, LPARAM);
|
|
|
|
|
2020-03-24 01:44:02 +08:00
|
|
|
// This Hwnd will be the window handler for the Xaml Island: A child window that contains Xaml.
|
2020-03-06 07:52:59 +08:00
|
|
|
HWND hWndXamlIslandEditKeyboardWindow = nullptr;
|
2020-03-24 01:44:02 +08:00
|
|
|
// This variable is used to check if window registration has been done to avoid repeated registration leading to an error.
|
|
|
|
bool isEditKeyboardWindowRegistrationCompleted = false;
|
2020-03-06 07:52:59 +08:00
|
|
|
|
2020-03-24 01:44:02 +08:00
|
|
|
// Function to create the Edit Keyboard Window
|
|
|
|
void createEditKeyboardWindow(HINSTANCE hInst, KeyboardManagerState& keyboardManagerState)
|
2020-03-06 07:52:59 +08:00
|
|
|
{
|
2020-03-24 01:44:02 +08:00
|
|
|
// Window Registration
|
2020-03-06 07:52:59 +08:00
|
|
|
const wchar_t szWindowClass[] = L"EditKeyboardWindow";
|
2020-03-24 01:44:02 +08:00
|
|
|
if (!isEditKeyboardWindowRegistrationCompleted)
|
2020-03-06 07:52:59 +08:00
|
|
|
{
|
2020-03-24 01:44:02 +08:00
|
|
|
WNDCLASSEX windowClass = {};
|
|
|
|
windowClass.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
windowClass.lpfnWndProc = EditKeyboardWindowProc;
|
|
|
|
windowClass.hInstance = hInst;
|
|
|
|
windowClass.lpszClassName = szWindowClass;
|
|
|
|
windowClass.hbrBackground = (HBRUSH)(COLOR_WINDOW);
|
|
|
|
windowClass.hIconSm = LoadIcon(windowClass.hInstance, IDI_APPLICATION);
|
|
|
|
if (RegisterClassEx(&windowClass) == NULL)
|
|
|
|
{
|
|
|
|
MessageBox(NULL, L"Windows registration failed!", L"Error", NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isEditKeyboardWindowRegistrationCompleted = true;
|
2020-03-06 07:52:59 +08:00
|
|
|
}
|
|
|
|
|
2020-03-24 01:44:02 +08:00
|
|
|
// Window Creation
|
2020-03-06 07:52:59 +08:00
|
|
|
HWND _hWndEditKeyboardWindow = CreateWindow(
|
|
|
|
szWindowClass,
|
|
|
|
L"PowerKeys Remap Keyboard",
|
|
|
|
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT,
|
|
|
|
400,
|
|
|
|
400,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
hInst,
|
|
|
|
NULL);
|
|
|
|
if (_hWndEditKeyboardWindow == NULL)
|
|
|
|
{
|
|
|
|
MessageBox(NULL, L"Call to CreateWindow failed!", L"Error", NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This DesktopWindowXamlSource is the object that enables a non-UWP desktop application
|
|
|
|
// to host UWP controls in any UI element that is associated with a window handle (HWND).
|
|
|
|
DesktopWindowXamlSource desktopSource;
|
|
|
|
// Get handle to corewindow
|
|
|
|
auto interop = desktopSource.as<IDesktopWindowXamlSourceNative>();
|
|
|
|
// Parent the DesktopWindowXamlSource object to current window
|
|
|
|
check_hresult(interop->AttachToWindow(_hWndEditKeyboardWindow));
|
|
|
|
|
|
|
|
// Get the new child window's hwnd
|
|
|
|
interop->get_WindowHandle(&hWndXamlIslandEditKeyboardWindow);
|
|
|
|
// Update the xaml island window size becuase initially is 0,0
|
|
|
|
SetWindowPos(hWndXamlIslandEditKeyboardWindow, 0, 0, 0, 400, 400, SWP_SHOWWINDOW);
|
|
|
|
|
|
|
|
//Creating the Xaml content
|
|
|
|
Windows::UI::Xaml::Controls::StackPanel xamlContainer;
|
|
|
|
xamlContainer.Background(Windows::UI::Xaml::Media::SolidColorBrush{ Windows::UI::Colors::LightGray() });
|
|
|
|
Windows::UI::Xaml::Controls::Button bt;
|
|
|
|
bt.Content(winrt::box_value(winrt::to_hstring("Don't Type key")));
|
|
|
|
xamlContainer.Children().Append(bt);
|
|
|
|
xamlContainer.UpdateLayout();
|
|
|
|
desktopSource.Content(xamlContainer);
|
|
|
|
////End XAML Island section
|
|
|
|
if (_hWndEditKeyboardWindow)
|
|
|
|
{
|
|
|
|
ShowWindow(_hWndEditKeyboardWindow, SW_SHOW);
|
|
|
|
UpdateWindow(_hWndEditKeyboardWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message loop:
|
|
|
|
MSG msg = {};
|
|
|
|
while (GetMessage(&msg, NULL, 0, 0))
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
desktopSource.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK EditKeyboardWindowProc(HWND hWnd, UINT messageCode, WPARAM wParam, LPARAM lParam)
|
|
|
|
{
|
|
|
|
RECT rcClient;
|
|
|
|
switch (messageCode)
|
|
|
|
{
|
|
|
|
case WM_PAINT:
|
|
|
|
GetClientRect(hWnd, &rcClient);
|
|
|
|
SetWindowPos(hWndXamlIslandEditKeyboardWindow, 0, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom, SWP_SHOWWINDOW);
|
|
|
|
break;
|
|
|
|
case WM_DESTROY:
|
|
|
|
PostQuitMessage(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return DefWindowProc(hWnd, messageCode, wParam, lParam);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|