确定窗口在 WPF 中是否实际可见的最佳方法是什么

发布于 2024-07-12 22:38:47 字数 922 浏览 4 评论 0原文

我正在尝试根据系统托盘应用程序中的通知图标的单击来切换小窗口的显示。 这很容易实现,但是当显示小窗口并且另一个应用程序获得焦点并因此移动到它前面(z 顺序)时,我希望切换假设小窗口现在是隐藏的,即使它的可见性是仍设置为可见。 否则,单击该图标会将窗口可见性设置为隐藏,即使它已经隐藏在另一个图标后面。 我尝试捕获/覆盖激活和停用方法来跟踪,但单击通知图标始终会导致停用事件首先触发。 使用焦点/失去焦点的类似方法也不起作用,因为窗口似乎认为它仍然具有焦点,即使隐藏在另一个正在使用的应用程序窗口后面。 最后,我不得不求助于本机代码和 WindowFromPoint 方法,如下所示:

using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Drawing;

[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point lpPoint);

public static bool IsWindowVisible(System.Windows.Window window) {
    WindowInteropHelper win = new WindowInteropHelper(window);
    int x = (int)(window.Left + (window.Width / 2));
    int y = (int)(window.Top + (window.Height / 2));
    Point p = new Point(x, y);
    return (win.Handle == WindowFromPoint(p));
}

这会检查在相关窗口中心坐标处返回的窗口是否与所述窗口匹配。 即相关窗口的中心是可见的。

这看起来有点hacky,有没有更好的方法来达到相同的结果?

I'm trying to toggle the display of a small window based on the click of a notify icon in a system tray app. This is simple enough to implement, however when the small window is displayed and another application takes focus and therefore moves in front of it (z-order) I want the toggle to assume that the small window is now hidden, even though it's visibility is still set to visible. Otherwise, clicking the icon would set the windows visiblity to hidden even though it is already hidden behind another. I've tried catching / overriding the activate and deactive methods to keep track but clicking the notify icon will always cause the deactive event to fire first. A similar approach using focus / lost focus didn't work either as the window seemed to think it still had focus even when hidden behind another applications window in active use. In the end I had to resort to native code and the WindowFromPoint method as follows:

using System.Windows.Interop;
using System.Runtime.InteropServices;
using System.Drawing;

[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point lpPoint);

public static bool IsWindowVisible(System.Windows.Window window) {
    WindowInteropHelper win = new WindowInteropHelper(window);
    int x = (int)(window.Left + (window.Width / 2));
    int y = (int)(window.Top + (window.Height / 2));
    Point p = new Point(x, y);
    return (win.Handle == WindowFromPoint(p));
}

This checks if the window returned at the coordinates of the centre of the window in question matches said window. i.e. the centre of the window in question is visible.

This seems a little hacky, is there a nicer way to achieve the same result?

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

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

发布评论

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

评论(1

萌无敌 2024-07-19 22:38:47

您可能不想依赖窗口是否被遮挡,因为有很多因素可以改变窗口大小、位置等,并且所有这些因素都与辅助功能相关,这增加了更多的复杂性。

相反,您可能想检查窗口是否具有焦点。 这就是 MSN Messenger 知道任务栏中是否闪烁橙色的方式; 它会发出通知,如果没有焦点,任务栏就会闪烁。

You may not want to rely on whether a window is obstructed as there are many factors that can change the window size, positing, etc. and all of them tie into accessibility features which add even more complexities.

Instead, you may want to check whether or not the window has focus. This is how MSN Messenger knows whether or not to flash orange in the taskbar; it fires a notification and if it doesn't have focus, the taskbar flashes.

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