[英]How to show a topmost window without overlapping fullscreen windows
我需要显示最顶层的窗口(系统托盘中的气球),而不会重叠任何全屏窗口。 例如,如果用户观看电影时出现我的最顶层窗口,则该窗口不得出现在电影屏幕的顶部。 仅当用户关闭其全屏窗口时,窗口才会出现。
现在,我只是这样展示我的窗口:
window.show()
在风格中我打开这些属性:
<Setter Property="Topmost" Value="True" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="ShowActivated" Value="False" />
如果他看电影或玩游戏,你能帮忙弄清楚如何在不打扰用户的情况下展示最顶层的窗户吗?
我不知道对这个wpf的任何内置支持。 因此,如果我必须实现这一点,我会发现我的操作系统中的Foreground窗口是全屏运行,然后不要将我的窗口作为全屏启动。
要在OS中获取当前的Foreground窗口,我们需要导入一些User32函数
[DllImport("user32.dll")]
private static extern bool GetWindowRect(HandleRef hWnd, [In, Out] ref RECT rect);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
现在我们需要添加对System.Windows.Forms
和System.Drawing
引用来获取当前的Screen
。 如果ForegroundWindow以全屏模式运行,则函数返回以下函数。
public bool IsAnyWindowFullScreen()
{
RECT rect = new RECT();
GetWindowRect(new HandleRef(null, GetForegroundWindow()), ref rect);
return new System.Drawing.Rectangle(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top).Contains(Screen.PrimaryScreen.Bounds);
}
所以在启动我的窗口时我会检查
if(!IsAnyWindowFullScreen())
{
window.Topmost = true;
}
window.Show();
相当不寻常的问题,我之前处理过类似的事情,我担心你不能像你想的那样简单(纠正我,如果我错了,我很想知道)。
这就是我处理你的问题的方法:
Dispatcher
运行一个方法,该方法将设置Topmost = true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.