通过 Alt+Tab 隐藏 WPF 表单

I.T*_*ent 10 c# wpf xaml

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

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

  1. VS 社区上有同样的问题,没有答案
  2. StackOverflow 上有同样的问题,但适用于 WinForms
  3. 同样的问题,但针对通用网站上的 WinForms
  4. 这不符合我的要求,因为我仍然希望 WPF 窗口可见,只是在 Alt+Tab 菜单中看不到

这是我的 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>
Run Code Online (Sandbox Code Playgroud)

这是我的 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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

无论我如何尝试,我都无法让 WPF 表单不显示在 Alt+Tab 菜单中。很感谢任何形式的帮助 :)

重复标志后更新 查看提供的链接(以及之前在提出此问题之前查看的链接)后,我想声明,事实上,我在这里找到了答案,并将发布我的完整代码作为此问题的答案: )

I.T*_*ent 14

我的最终代码遵循此StackOverflow 问题的答案,如下所示:

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);
            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 你是圣人!! (2认同)