我不知道对此 wpf 有任何内置支持。所以如果我必须实现这个,我会发现我的操作系统中的前景窗口是否全屏运行,然后不要全屏启动我的窗口。
要在操作系统中获取当前的前台窗口,我们需要导入一些 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();