Mark Harwood
Mark Harwood

Reputation: 2415

Hide a WPF form from Alt+Tab

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

Upvotes: 10

Views: 2659

Answers (1)

Mark Harwood
Mark Harwood

Reputation: 2415

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.

Upvotes: 15

Related Questions