【问题标题】:How do I prevent the taskbar from appearing with a WPF non-fullscreen window?如何防止任务栏出现 WPF 非全屏窗口?
【发布时间】:2012-06-11 16:36:39
【问题描述】:

我有一个大型 C++ 代码库,带有全屏运行的原生 Windows GUI。 它的一部分应由其顶部显示的 WPF 窗口进行交换。 窗口是这样设置的:

<Window WindowState="Normal" 
        WindowStyle="None"
        ShowInTaskbar="False"
        AllowsTransparency="True"
/>

这会将窗口无缝地融合到应用程序的其余部分中。 窗口的调用是从 C++/CLI 完成的,如下所示:

Windows::Window^ w = window();
Windows::Interop::WindowInteropHelper iHelp(w);
iHelp.Owner = System::IntPtr(_parentHwnd);
w->Show();

_parentHwnd 是本机应用程序的 HWND。 如前所述,应用程序始终全屏显示。 当我现在单击 WPF 窗口时,将出现 Windows 任务栏。 如何防止任务栏出现

【问题讨论】:

    标签: wpf window command-line-interface


    【解决方案1】:

    我使用过这个类(在网上某处发现了一个想法)来隐藏/显示任务栏:

    public static class Taskbar
    {
        [DllImport("user32.dll")]
        private static extern int FindWindow(string className, string windowText);
        [DllImport("user32.dll")]
        private static extern int ShowWindow(int hwnd, int command);
    
        private const int SW_HIDE = 0;
        private const int SW_SHOW = 1;
    
        public static int Handle
        {
            get
            {
                return FindWindow("Shell_TrayWnd", "");
            }
        }
        public static int StartHandle
        {
            get
            {
                return FindWindow("Button", "Start");
            }
        }
    
        public static void Show()
        {
            ShowWindow(Handle, SW_SHOW);
            ShowWindow(StartHandle, SW_SHOW);
        }
    
        public static void Hide()
        {
            ShowWindow(Handle, SW_HIDE);
            ShowWindow(StartHandle, SW_HIDE);
        }
    }
    

    适用于 Windows XP/Vista/7。

    【讨论】:

    • 非常感谢您的回答。我对此进行了测试 - 可能需要一天才能回来。
    • 有趣。我认为这是一个严重的黑客攻击,但满足了我们的需求。谢谢。
    【解决方案2】:

    使用 Flot2011 回答 this 我能够假装我的窗口属于另一个全屏窗口。对于那些对缺失部分感到好奇的人 - 它们是: 我们为激活和停用事件实现处理程序

    <Window
    Activated="DisplayWindow_Activated"
    Deactivated="DisplayWindow_Deactivated"
    />
    

    事件处理程序如下所示

    private void DisplayWindow_Activated(object sender, EventArgs e)
    {
      var screen = System.Windows.Forms.Screen.FromRectangle(
        new System.Drawing.Rectangle(
          (int)this.Left, (int)this.Top,
          (int)this.Width, (int)this.Height));
      if( screen.Primary )
        Taskbar.Hide();
    }
    
    private void DisplayWindow_Deactivated(object sender, EventArgs e)
    {
      var screen = System.Windows.Forms.Screen.FromRectangle(
        new System.Drawing.Rectangle(
          (int)this.Left, (int)this.Top,
          (int)this.Width, (int)this.Height));
      if( screen.Primary )
        Taskbar.Show();
    }
    

    那么现在会发生什么?任务栏将在主应用程序中消失,因为它是全屏的。当我单击覆盖的窗口时,任务栏将被激活,但被属性事件停用。所以混搭就像一个全屏应用程序。

    screen 部分用于处理多显示器设置。如果(mashup)应用程序在主监视器上全屏显示,我们应该只隐藏任务栏。当前的解决方案假设 WPF 窗口和底层全屏应用程序在同一个屏幕上。

    如果您使用 screen 内容,请不要忘记包含对 System.DrawingSystem.Window.Forms 的引用。 usings 在这里并不是一个好主意,因为命名空间会与 .net 中的内容发生冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      相关资源
      最近更新 更多