Skip to content

Commit

Permalink
refactor: 使用 ChangeWindowMessageFilterEx 替换 ChangeWindowMessageFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinue committed May 7, 2024
1 parent a7c91e9 commit f223f4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
14 changes: 8 additions & 6 deletions docs/Interact with Magpie programally.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Magpie provides mechanisms for interaction with other programs. Through these me

[MagpieWatcher](https://github.com/Blinue/MagpieWatcher) demonstrates how to use these mechanisms.

## How to Receive Notifications When Scaling State Changes
## How to Receive Notifications When Scaling State Changes

You should listen for the MagpieScalingChanged message.

Expand All @@ -13,15 +13,16 @@ UINT WM_MAGPIE_SCALINGCHANGED = RegisterWindowMessage(L"MagpieScalingChanged");
### Parameters

`wParam` is the event ID. For different events, `lParam` has different meanings. Currently, two events are supported:
* 0: Scaling has ended. `lParam` is not used.
* 1: Scaling has started. `lParam` is the handle of the scaling window.

* 0: Scaling has ended. `lParam` is not used.
* 1: Scaling has started. `lParam` is the handle of the scaling window.

### Notes

If your process has a higher integrity level than Magpie, you won't receive messages broadcasted by Magpie due to User Interface Privilege Isolation (UIPI). In such cases, call ChangeWindowMessageFilter to allow receiving the MagpieScalingChanged message.
If your process has a higher integrity level than Magpie, you won't receive messages broadcasted by Magpie due to User Interface Privilege Isolation (UIPI). In such cases, call [ChangeWindowMessageFilterEx](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-changewindowmessagefilterex) to allow receiving the MagpieScalingChanged message.

```c++
ChangeWindowMessageFilter(WM_MAGPIE_SCALINGCHANGED, MSGFLT_ADD);
ChangeWindowMessageFilterEx(hYourWindow, WM_MAGPIE_SCALINGCHANGED, MSGFLT_ADD, nullptr);
```
## How to Get the Handle of the Scaling Window
Expand Down Expand Up @@ -58,6 +59,7 @@ if (message == WM_MAGPIE_SCALINGCHANGED) {
## How to Obtain Scaling Information

Scaling information is stored in the [window properties](https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-properties) of the scaling window. Currently available properties include:

* `Magpie.SrcHWND`: Handle of the source window
* `Magpie.SrcLeft``Magpie.SrcTop``Magpie.SrcRight``Magpie.SrcBottom`: Source region of scaling
* `Magpie.DestLeft``Magpie.DestTop``Magpie.DestRight``Magpie.DestBottom`: Destination region of scaling
Expand All @@ -83,5 +85,5 @@ destRect.bottom = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.DestBottom");
Magpie stops scaling when the foreground window changes, with some system windows being exceptions. By setting the `Magpie.ToolWindow` property, you can include your window and all its owned windows in the exceptions list.

```c++
SetProp(hWnd, L"Magpie.ToolWindow", (HANDLE)TRUE);
SetProp(hYourWindow, L"Magpie.ToolWindow", (HANDLE)TRUE);
```
16 changes: 9 additions & 7 deletions docs/以编程方式与 Magpie 交互.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Magpie 提供了和其他程序交互的机制。通过它们,你的应用可以和 Magpie 配合使用。

[MagpieWatcher](https://github.com/Blinue/MagpieWatcher) 演示了如何使用这些机制。
[MagpieWatcher](https://github.com/Blinue/MagpieWatcher) 演示了如何使用这些机制。

## 如何在缩放状态改变时得到通知
## 如何在缩放状态改变时得到通知

你应该监听 MagpieScalingChanged 消息。

Expand All @@ -13,15 +13,16 @@ UINT WM_MAGPIE_SCALINGCHANGED = RegisterWindowMessage(L"MagpieScalingChanged");
### 参数

`wParam` 为事件 ID,对于不同的事件 `lParam` 有不同的含义。目前支持两个事件:
* 0: 缩放已结束。不使用 `lParam`
* 1: 缩放已开始。`lParam` 为缩放窗口句柄。

* 0: 缩放已结束。不使用 `lParam`
* 1: 缩放已开始。`lParam` 为缩放窗口句柄。

### 注意事项

如果你的进程完整性级别 (Integration level) 比 Magpie 更高,由于用户界面特权隔离 (UIPI),你将无法收到 Magpie 广播的消息。这种情况下请调用 ChangeWindowMessageFilter 允许接收 MagpieScalingChanged 消息。
如果你的进程完整性级别 (Integration level) 比 Magpie 更高,由于用户界面特权隔离 (UIPI),你将无法收到 Magpie 广播的消息。这种情况下请调用 [ChangeWindowMessageFilterEx](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-changewindowmessagefilterex) 以允许接收 MagpieScalingChanged 消息。

```c++
ChangeWindowMessageFilter(WM_MAGPIE_SCALINGCHANGED, MSGFLT_ADD);
ChangeWindowMessageFilterEx(hYourWindow, WM_MAGPIE_SCALINGCHANGED, MSGFLT_ADD, nullptr);
```
## 如何获取缩放窗口句柄
Expand Down Expand Up @@ -58,6 +59,7 @@ if (message == WM_MAGPIE_SCALINGCHANGED) {
## 如何获取缩放信息

缩放窗口的[窗口属性](https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-properties)中存储着缩放信息。目前支持以下属性:

* `Magpie.SrcHWND`: 源窗口句柄
* `Magpie.SrcLeft``Magpie.SrcTop``Magpie.SrcRight``Magpie.SrcBottom`: 被缩放区域的边界
* `Magpie.DestLeft``Magpie.DestTop``Magpie.DestRight``Magpie.DestBottom`: 缩放后区域矩形边界
Expand All @@ -83,5 +85,5 @@ destRect.bottom = (LONG)(INT_PTR)GetProp(hwndScaling, L"Magpie.DestBottom");
前台窗口改变时 Magpie 会停止缩放,只对某些系统窗口例外。你可以通过设置属性 `Magpie.ToolWindow` 将自己的窗口添加入例外,这对由该窗口拥有 (owned) 的窗口也有效。

```c++
SetProp(hWnd, L"Magpie.ToolWindow", (HANDLE)TRUE);
SetProp(hYourWindow, L"Magpie.ToolWindow", (HANDLE)TRUE);
```
28 changes: 14 additions & 14 deletions src/TouchHelper/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ static UINT WM_MAGPIE_SCALINGCHANGED;
static UINT WM_MAGPIE_TOUCHHELPER;
static HWND curHwndScaling = NULL;

static void InitMessages() noexcept {
WM_MAGPIE_SCALINGCHANGED =
RegisterWindowMessage(CommonSharedConstants::WM_MAGPIE_SCALINGCHANGED);
WM_MAGPIE_TOUCHHELPER =
RegisterWindowMessage(CommonSharedConstants::WM_MAGPIE_TOUCHHELPER);

// 防止消息被 UIPI 过滤
ChangeWindowMessageFilter(WM_MAGPIE_SCALINGCHANGED, MSGFLT_ADD);
ChangeWindowMessageFilter(WM_MAGPIE_TOUCHHELPER, MSGFLT_ADD);
}

static void UpdateInputTransform(HWND hwndScaling) noexcept {
if (curHwndScaling == hwndScaling) {
return;
Expand Down Expand Up @@ -70,11 +59,24 @@ static LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (wParam == 0) {
// 退出
DestroyWindow(hWnd);
return 0;
}

return 0;
}

switch (msg) {
case WM_CREATE:
{
WM_MAGPIE_SCALINGCHANGED =
RegisterWindowMessage(CommonSharedConstants::WM_MAGPIE_SCALINGCHANGED);
WM_MAGPIE_TOUCHHELPER =
RegisterWindowMessage(CommonSharedConstants::WM_MAGPIE_TOUCHHELPER);

// 防止消息被 UIPI 过滤
ChangeWindowMessageFilterEx(hWnd, WM_MAGPIE_SCALINGCHANGED, MSGFLT_ADD, nullptr);
ChangeWindowMessageFilterEx(hWnd, WM_MAGPIE_TOUCHHELPER, MSGFLT_ADD, nullptr);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
Expand Down Expand Up @@ -139,8 +141,6 @@ int APIENTRY wWinMain(
return 1;
}

InitMessages();

// 创建一个隐藏窗口用于接收广播消息
{
WNDCLASSEXW wcex{
Expand Down

0 comments on commit f223f4f

Please sign in to comment.