gpt4 book ai didi

c# - WPF 最上面的选项使全屏应用程序转义

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:22 27 4
gpt4 key购买 nike

我使用小窗口作为工具提示,在我的应用程序中显示“正在播放”信息。当然,我在窗口声明中设置了 Topmost = "True",当新歌开始播放时,我执行了 tooltip.Show()。问题是当我关注全屏应用程序(例如游戏)时,.Show() 调用使全屏窗口变成非全屏窗口,窗口化,带有边框和顶部栏(就像我按 Alt + Enter) 看起来它也失去了焦点。因此,要恢复全屏,我需要在窗口中单击以使其聚焦并手动按 Alt-Enter。ShowActivated 已设置为 "False"。所以,我看到了三个解决方案:

1) 以某种方式使 Topmost 选项不会导致窃取对 .Show() 的关注

2) 使用一些解决方法使工具提示窗口“始终位于顶部”而无需 Topmost 选项(我不需要在全屏应用程序上弹出工具提示,因为它可以(并且可能会)被绘制通过 D3D 或 OpenGL)

3) 检测系统现在是否有全屏窗口并且不要尝试显示工具提示。

我不知道如何使用这些选项中的任何一个来修复行为,或者也许有更优雅的东西?

最佳答案

所以...如果有人感兴趣,这是我的“研究”。

解决方案 1) 失败。 我发现了一些关于此行为的讨论,一些人说这是一个错误,另一些人说这是一个功能,但他们中的任何人都认为它无法在托管代码中解决。我也尝试了 Sheridan 的回答,但没有成功。

解决方案 2) 失败。我对 P/Invoke 有一些经验,但如果出现问题,我通常会失败。在这种特殊情况下,我尝试使用

this.Handle = new WindowInteropHelper(this).Handle;
SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);

正如它在网络上所建议的那样。此代码不显示窗口。所以我试着用

来展示它
ShowWindow(this.Handle, 4); // I also tried SHOW_NORMAL and few other flags

但是窗口还是不可见

如果我执行 this.Visibility =/*visible*/this.Show() 它就会变得可见,而且我真的看到那个窗口在最上面,但是它不解决问题和全屏转义。如果有人知道 .Show() 的内部工作原理以及我如何使用 P/Invoke 显示 WPF 窗口,请告诉我。

解决方案 3) 成功。 我通过一些谷歌搜索找到了工作代码 here并稍微缩短它:

    internal class FullscreenCheck
{
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetShellWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);

// I hope this handles never changes
private static IntPtr hndlDesktop = GetDesktopWindow();
private static IntPtr hndlShell = GetShellWindow();

public static bool IsFullscreen()
{
var hndlForeground = GetForegroundWindow();
if (hndlForeground == null || hndlForeground == IntPtr.Zero || hndlForeground == hndlDesktop || hndlForeground == hndlShell)
{
return false;
}

RECT appBounds;
GetWindowRect(hndlForeground, out appBounds);
var screenBounds = System.Windows.Forms.Screen.FromHandle(hndlForeground).Bounds;

return ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width);
}

所以最后一步就是不调用 .Show() 如果 IsFullscreen() == true

关于c# - WPF 最上面的选项使全屏应用程序转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18601804/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com