10

I am wanting to hide a WPF window that has WindowStyle="None" , AllowTransparency="True" and ShowInTaskbar="False" from the task menu (Alt+Tab).

I have already researched this but all the results appear to be for WinForms or don't have an answer. Here are some of the sources I have already looked into:

  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

Here is my XAML code:

<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>

Here is my C# code:

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();
        }
    }
}

No matter what I try, I cannot get the WPF form to not show up in the Alt+Tab menu. Any help is very much appreciated :)

UPDATE AFTER DUPLICATE FLAG After looking at the link that was provided (and previously viewed before asking this question), I would like to state that I, in fact, found the answer here and will post my full code as an answer to this question :)

I.T Delinquent
  • 2,305
  • 2
  • 16
  • 33
  • [What about this?](https://stackoverflow.com/questions/23672595/how-to-hide-winform-or-wpf-application-from-alttab-and-from-task-managers-ap/23672596) – ProgrammingLlama Jun 18 '19 at 08:56
  • @John Thank you for the link, I will update my question with why this doesn't fit my requirements :) – I.T Delinquent Jun 18 '19 at 08:58
  • Did you try this https://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher – Andy Jun 18 '19 at 09:16
  • @Andy I actually saw this and it looked promising. Some of the comments on the answer state that it no longer works, however. It looks like a lot of work and to be honest I'm not sure where the code should go. – I.T Delinquent Jun 18 '19 at 09:24
  • 1
    Possible duplicate of [Best way to hide a window from the Alt-Tab program switcher?](https://stackoverflow.com/questions/357076/best-way-to-hide-a-window-from-the-alt-tab-program-switcher) – Flater Jun 18 '19 at 11:17
  • Wait, the title says WPF but you're using Windows.Forms? – ecth Mar 27 '23 at 15:02

1 Answers1

13

My final code following the answer from this StackOverflow question can be seen below:

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);
            
        }
    }
}

My form now runs as a background task, is still visible and cannot be seen in the Alt+Tab menu. Thank you, everyone, for your help :) I'm a little ashamed I didn't find the winning link before posting the question.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
I.T Delinquent
  • 2,305
  • 2
  • 16
  • 33