当“不再需要”窗口时关闭它

发布于 2024-09-05 15:10:15 字数 358 浏览 13 评论 0原文

这是一个很难描述的问题,我已经搜索了大约一个小时,但没有结果。

本质上,想象一个小的“弹出”窗口,如 Windows 7 无线控件或系统托盘(通知区域)中的音量滑块。当您单击该图标时,应用程序会弹出并具有焦点,如果您单击它,窗口会自行销毁。

我认为这很容易解决,只需让我的窗口在失去焦点时自行销毁(我一直在监听 WM_KILLFOCUS),但问题是,如果单击图标,我的窗口并不总是获得焦点。由于情况并非如此,如果用户单击我的图标,然后因为错误而单击离开(例如在桌面上),那么我如何将我的应用程序设置为关闭?

我尝试过使用 SPY++,但检查音量控制/无线控制应用程序被证明很困难,因为当我尝试获取它们的窗口/进程句柄时它们会消失。

谢谢!

This is kind of a hard question to describe, and I've searched for about an hour now to no avail.

Essentially, picture a small 'flyout' window like the Windows 7 Wireless Control or the Volume Slider from the system tray(notification area). When you click on the icon, the application pops up with focus, and if you click off of it, the window destroys itself.

I thought it woudl be easily solved by simply having my window destroy it self when it loses focus (I've been listening for WM_KILLFOCUS), but the problem is, if the icon is clicked, my window does not always get focus. Since this isn't the case, if the user clicks my icon, and then clicks away because it was a mistake (on the desktop say), then how can I set my app to close?

I've tried messing with SPY++ but checking the volume control / wireless control apps are proving difficult as they disappear when I try to get their window/process handles.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∝单色的世界 2024-09-12 15:10:15

实现这一点的通常方法是在窗口创建时启动计时器。如果窗口在计时器触发之前获得焦点,则意味着用户已与窗口进行交互。在这种情况下,窗口只会停止计时器,并在失去焦点时自行销毁。如果窗口在计时器触发之前没有获得焦点,则窗口将在计时器事件发生时销毁自身。

这通常也与不透明动画结合使用,以便窗口在等待用户时淡出。类似于向用户提供的视觉反馈,表明它很快就会消失。然而,不透明度动画主要用于通知提示,很少用于音量控制等控制窗口。

另一种方法是当用户与系统托盘图标交互时强制在窗口中设置焦点。

另请注意,如果您的窗口是顶级窗口,则首选侦听消息不是 WM_KILLFOCUS,而是 WM_ACTIVATEWM_MOUSEACTIVATE。您还可以收听 WM_NCACTIVATE ,但是如果您正在执行自定义非客户区域,那么该区域有一些具体细节。

更新:您可以通过调用 SetActiveWindowSetFocus 当您创建它时(或者当您使其可见时,如果您隐藏它)。

The usual way this is implemented is by starting a timer on the window creation. If the window gets the focus before the timer has triggered, this means the user has interacted with the window. In this case, the window will just stop the timer and will destroy itself when it loses the focus. In the case the window did not get the focus before the timer was triggered, the window will destroy itself on the timer event.

This is also usually combined with opacity animation, so that the window is fading out while waiting for the user. Sort of a visual feedback to the user that it will be soon gone. However, the opacity animation is used mostly for notification toasts, and is rarely used for control windows like the volume control.

The alternative is to force set the focus in your window when the user interacts with your systray icon.

Also note that if your window is a top-level window, the preferred message to listen is not WM_KILLFOCUS, but WM_ACTIVATE and WM_MOUSEACTIVATE. You can also listen to WM_NCACTIVATE, but that one has some specifics, if you are doing a custom non-client area.

Update: You can set the focus to your window by calling either SetActiveWindow or SetFocus on it when you create it (or when you make it visible, if you're hiding it).

梦晓ヶ微光ヅ倾城 2024-09-12 15:10:15

很久很久以前,我编写了一个 Windows 3.1 任务管理器的直接替代品,它通过处理 WM_ACTIVATEAPP。尝试一下。

A long, long time ago I wrote a drop-in replacement for the Windows 3.1 Task Manager that accomplished this by handling WM_ACTIVATEAPP. Give that a try.

音盲 2024-09-12 15:10:15

您查看过弹出窗口吗?一旦您在其外部单击,该按钮就会消失(除非您将 StaysOpen 设置为 true)。

Have you looked into the Popup? That one will disappear once you click outside it (unless you set StaysOpen to true).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文