c# – 如何在不重叠全屏窗口的情况下显示最顶层窗口
发布时间:2020-12-15 22:05:27 所属栏目:百科 来源:网络整理
导读:我需要显示最顶层的窗口(系统托盘中的气球),而不会重叠任何全屏窗口.例如,如果用户观看电影时出现我的最顶层窗口,则该窗口不得出现在电影屏幕的顶部.仅当用户关闭其全屏窗口时,窗口才会出现. 现在,我只是这样展示我的窗口: window.show() 在风格中我打开这
|
我需要显示最顶层的窗口(系统托盘中的气球),而不会重叠任何全屏窗口.例如,如果用户观看电影时出现我的最顶层窗口,则该窗口不得出现在电影屏幕的顶部.仅当用户关闭其全屏窗口时,窗口才会出现.
现在,我只是这样展示我的窗口: 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();
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
