[Windows]: After the menu pops up, click somewhere else on the screen and the menu will not hide
As described above
我改进了这个问题,你可以试试反馈下。
dependencies:
tray_manager:
git:
url: https://github.com/w0fv1/tray_manager.git
ref: main
path: packages/tray_manager
下面的是AI写的修改思路。
在使用 Flutter Windows 托盘插件 (tray_manager) 时,我们可能会遇到一个 Bug:打开托盘菜单后,点击空白区域或其他窗口,菜单不会自动隐藏,只有点击菜单项时才会消失。这是由于 TrackPopupMenu 只在菜单项被点击时关闭,而不会响应空白区域点击事件。
💡 解决方案 只需要在 PopUpContextMenu 方法中 调用 SetForegroundWindow 之后,紧接着在 TrackPopupMenu 之后添加 PostMessage(hWnd, WM_NULL, 0, 0);,这样可以确保菜单在点击空白区域时关闭。
📌 修改代码如下:
void TrayManagerPlugin::PopUpContextMenu(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
const flutter::EncodableMap& args =
std::get<flutter::EncodableMap>(*method_call.arguments());
bool bringAppToFront =
std::get<bool>(args.at(flutter::EncodableValue("bringAppToFront")));
HWND hWnd = GetMainWindow();
POINT cursorPos;
GetCursorPos(&cursorPos);
double x = cursorPos.x;
double y = cursorPos.y;
if (bringAppToFront) {
SetForegroundWindow(hWnd); // 确保窗口在前台
}
// 使菜单能正确响应空白区域的点击
SetForegroundWindow(hWnd);
TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, static_cast<int>(x),
static_cast<int>(y), 0, hWnd, NULL);
// 发送 WM_NULL 以确保点击空白区域时菜单自动隐藏
PostMessage(hWnd, WM_NULL, 0, 0);
result->Success(flutter::EncodableValue(true));
}
🔍 原理解析 SetForegroundWindow(hWnd);
这个函数确保应用程序的窗口在前台,使得 Windows 可以正确处理点击事件。 TrackPopupMenu(hMenu, ...)
显示托盘菜单,并等待用户交互。 PostMessage(hWnd, WM_NULL, 0, 0);
WM_NULL 是一个空消息,它不会触发任何操作,但它的作用是终止 TrackPopupMenu 的激活状态,从而使得点击空白区域时菜单会自动隐藏。 🚀 适用场景 ✅ 适用于 Flutter Windows 托盘插件 (tray_manager) ✅ 适用于 Windows 平台的原生 C++ 托盘应用 ✅ 修复 点击空白区域菜单不自动隐藏 的问题
如果你在 Flutter Windows 应用中使用了托盘功能,不妨试试这个修改!🎯
补充,可能存在的问题是点击菜单按钮后app窗口会被打开,不过这个问题不是很严重,可以暂时凑合用。
我看了官方的模板,trayManager.popUpContextMenu(bringAppToFront:true);要设置这个参数windows才会自动关闭菜单。我试过了有用(但是作者标记这个为弃用,不过现在依然生效)
我看了官方的模板,trayManager.popUpContextMenu(bringAppToFront:true);要设置这个参数windows才会自动关闭菜单。我试过了有用(但是作者标记这个为弃用,不过现在依然生效)
和这个没关系吧?根据我测试
popUpContextMenu This solution can fix this problem. Thank you. I hope the developer will solve this problem in time.
官方有计划修一下这个问题吗
遇到这个问题可以改用^0.2.1版本,此版本没有这个问题。
我看了官方的模板,trayManager.popUpContextMenu(bringAppToFront:true);要设置这个参数windows才会自动关闭菜单。我试过了有用(但是作者标记这个为弃用,不过现在依然生效)
和这个没关系吧?根据我测试
有的,添加这个参数为true在Windows上菜单就是正常的行为。
The issue is still present (tray_manager 0.5.1).
The issue is still present (tray_manager 0.5.1).
still in 0.5.2. And indeed it can be worked around by trayManager.popUpContextMenu(bringAppToFront:true);.
tray_manager: ^0.5.2 仍然存在无法关闭托盘菜单的问题,trayManager.popUpContextMenu(bringAppToFront:true)操作会导致右键点击托盘图标时,导致窗口置顶,影响用户体验,希望能尽快解决一下,感谢。