从Alt + Tab隐藏WPF表单

时间:2019-06-18 08:53:05

标签: c# wpf xaml

我想从任务菜单(Alt + Tab)中隐藏具有WindowStyle="None"AllowTransparency="True"ShowInTaskbar="False"的WPF窗口。

我已经对此进行了研究,但是所有结果似乎都是针对WinForms的,或者没有答案。以下是一些我已经研究过的资源:

  1. Same question on VS community WITHOUT an answer
  2. Same question on StackOverflow but for WinForms
  3. Same question but for WinForms on generic site
  4. This doesn't meet my requirements because I still want the WPF window to be visible, just not seen in the Alt+Tab menu

这是我的XAML代码:

<Window x:Class="DesktopInfo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DesktopInfo"
        mc:Ignorable="d"
        Title="MainWindow" SizeToContent="WidthAndHeight" WindowStyle="None" AllowsTransparency="True"  Background="Transparent" ShowInTaskbar="False" Loaded="FormLoaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Testing" Name="UsernameTextBlock" FontSize="20" FontWeight="Bold" Foreground="White"/>
        <TextBlock Name="ComputernameTextBlock" Grid.Row="1" FontSize="20" FontWeight="Bold" Foreground="White"/>
    </Grid>
</Window>

这是我的C#代码:

using System;
using System.Windows;
using System.Windows.Forms;

namespace DesktopInfo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

无论我如何尝试,我都无法使WPF表单不显示在Alt + Tab菜单中。非常感谢您的帮助:)

在复制标记后更新 在查看了提供的链接(并在询问该问题之前查看过该链接)之后,我想说明一下,实际上,我找到了answer here,并将发布我的完整代码作为对此问题的答案:)

1 个答案:

答案 0 :(得分:1)

以下StackOverflow question给出的答案之后,我的最终代码如下:

using System;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace DesktopInfo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window
    {

        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        private const int GWL_EX_STYLE = -20;
        private const int WS_EX_APPWINDOW = 0x00040000, WS_EX_TOOLWINDOW = 0x00000080;

        public MainWindow()
        {
            InitializeComponent();
        }
        //Form loaded event handler
        void FormLoaded(object sender, RoutedEventArgs args)
        {
            //Variable to hold the handle for the form
            var helper = new WindowInteropHelper(this).Handle;
            //Performing some magic to hide the form from Alt+Tab
            SetWindowLong(helper, GWL_EX_STYLE, (GetWindowLong(helper, GWL_EX_STYLE) | WS_EX_TOOLWINDOW) & ~WS_EX_APPWINDOW);

        }
    }
}

我的表单现在作为后台任务运行,仍然可见,并且在Alt + Tab菜单中看不到。谢谢大家的帮助:)我有点)愧,在发布问题之前没有找到获胜的链接。