-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,155 @@ | ||
#include "pch.h" | ||
#include <magnification.h> | ||
#include "../Shared/CommonSharedConstants.h" | ||
|
||
static UINT WM_MAGPIE_SCALINGCHANGED; | ||
static HWND curHwndScaling = NULL; | ||
|
||
static void UpdateInputTransform(HWND hwndScaling) noexcept { | ||
if (curHwndScaling == hwndScaling) { | ||
return; | ||
} | ||
curHwndScaling = hwndScaling; | ||
|
||
if (hwndScaling == NULL) { | ||
RECT ununsed{}; | ||
MagSetInputTransform(FALSE, &ununsed, &ununsed); | ||
return; | ||
} | ||
|
||
RECT srcRect{ | ||
.left = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.SrcLeft"), | ||
.top = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.SrcTop"), | ||
.right = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.SrcRight"), | ||
.bottom = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.SrcBottom") | ||
}; | ||
|
||
RECT destRect{ | ||
.left = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.DestLeft"), | ||
.top = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.DestTop"), | ||
.right = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.DestRight"), | ||
.bottom = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.DestBottom") | ||
}; | ||
|
||
MagSetInputTransform(TRUE, &srcRect, &destRect); | ||
} | ||
|
||
static LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { | ||
if (msg == WM_MAGPIE_SCALINGCHANGED) { | ||
if (wParam == 0) { | ||
// 缩放结束 | ||
UpdateInputTransform(NULL); | ||
} else if (wParam == 1) { | ||
// 缩放开始 | ||
UpdateInputTransform((HWND)lParam); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
return DefWindowProc(hWnd, msg, wParam, lParam); | ||
} | ||
|
||
static wil::unique_mutex_nothrow CheckSingleInstance() noexcept { | ||
wil::unique_mutex_nothrow hSingleInstanceMutex; | ||
|
||
bool alreadyExists = false; | ||
if (!hSingleInstanceMutex.try_create( | ||
CommonSharedConstants::TOUCH_HELPER_SINGLE_INSTANCE_MUTEX_NAME, | ||
CREATE_MUTEX_INITIAL_OWNER, | ||
MUTEX_ALL_ACCESS, | ||
nullptr, | ||
&alreadyExists | ||
) || alreadyExists) { | ||
hSingleInstanceMutex.reset(); | ||
} | ||
|
||
return hSingleInstanceMutex; | ||
} | ||
|
||
int APIENTRY wWinMain( | ||
_In_ HINSTANCE /*hInstance*/, | ||
_In_ HINSTANCE hInstance, | ||
_In_opt_ HINSTANCE /*hPrevInstance*/, | ||
_In_ LPWSTR /*lpCmdLine*/, | ||
_In_ int /*nCmdShow*/ | ||
) { | ||
// 确保单例 | ||
wil::unique_mutex_nothrow hSingleInstanceMutex = CheckSingleInstance(); | ||
if (!hSingleInstanceMutex) { | ||
MessageBox(NULL, L"已有实例", L"消息", MB_OK); | ||
return 0; | ||
} | ||
auto se = hSingleInstanceMutex.ReleaseMutex_scope_exit(); | ||
|
||
wil::unique_mutex_nothrow hMagpieMutex; | ||
if (!hMagpieMutex.try_create(CommonSharedConstants::SINGLE_INSTANCE_MUTEX_NAME)) { | ||
return 1; | ||
} | ||
|
||
if (wil::handle_wait(hMagpieMutex.get(), 0)) { | ||
// Magpie 未启动 | ||
hMagpieMutex.ReleaseMutex(); | ||
return 0; | ||
} | ||
|
||
if (!MagInitialize()) { | ||
MessageBox(NULL, L"失败", L"消息", MB_OK); | ||
return 1; | ||
} | ||
|
||
RECT srcRect{ 0,0,100,100 }; | ||
RECT destRect{ 10,10,210,210 }; | ||
if (!MagSetInputTransform(TRUE, &srcRect, &destRect)) { | ||
MessageBox(NULL, L"失败", L"消息", MB_OK); | ||
WM_MAGPIE_SCALINGCHANGED = | ||
RegisterWindowMessage(CommonSharedConstants::WM_MAGPIE_SCALINGCHANGED); | ||
// 防止消息被 UIPI 过滤 | ||
ChangeWindowMessageFilter(WM_MAGPIE_SCALINGCHANGED, MSGFLT_ADD); | ||
|
||
// 创建一个隐藏窗口用于接收广播消息 | ||
{ | ||
WNDCLASSEXW wcex{ | ||
.cbSize = sizeof(wcex), | ||
.lpfnWndProc = WndProc, | ||
.hInstance = hInstance, | ||
.lpszClassName = CommonSharedConstants::TOUCH_HELPER_WINDOW_CLASS_NAME | ||
}; | ||
RegisterClassEx(&wcex); | ||
} | ||
|
||
wil::unique_hwnd hWnd(CreateWindow( | ||
CommonSharedConstants::TOUCH_HELPER_WINDOW_CLASS_NAME, | ||
nullptr, | ||
WS_OVERLAPPEDWINDOW | WS_POPUP, | ||
CW_USEDEFAULT, | ||
CW_USEDEFAULT, | ||
CW_USEDEFAULT, | ||
CW_USEDEFAULT, | ||
NULL, | ||
NULL, | ||
hInstance, | ||
0 | ||
)); | ||
if (!hWnd) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
|
||
{ | ||
// 检查 Magpie 是否正在缩放,注意如果缩放窗口尚未显示视为没有缩放, | ||
// 此时缩放窗口正在初始化,会在完成后广播 WM_MAGPIE_SCALINGCHANGED 消息 | ||
HWND hwndScaling = FindWindow(CommonSharedConstants::SCALING_WINDOW_CLASS_NAME, nullptr); | ||
if (hwndScaling && IsWindowVisible(hwndScaling)) { | ||
UpdateInputTransform(hwndScaling); | ||
} | ||
} | ||
|
||
MSG msg; | ||
while (true) { | ||
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { | ||
DispatchMessage(&msg); | ||
} | ||
|
||
// 等待新消息或 Magpie 退出 | ||
if (MsgWaitForMultipleObjectsEx(1, hMagpieMutex.addressof(), | ||
INFINITE, QS_ALLINPUT, MWMO_INPUTAVAILABLE) == WAIT_OBJECT_0) { | ||
// Magpie 已退出 | ||
hMagpieMutex.ReleaseMutex(); | ||
return 0; | ||
} | ||
} | ||
} |