2
votes

I want to set the Windows taskbar as the foreground window just like when the user clicks onto it. You can see it is focused because the previously active window is not marked in the taskbar anymore.

I tried SetForegroundWindow by getting the hwnd with FindWindow but that does nothing:

SetForegroundWindow(FindWindow("System_TrayWnd", null));

Specifically I want to prevent the taskbar from auto-hiding when the auto-hide option is enabled. I do not want to disable the auto-hide option temporarily as that causes open windows to shift in position. If the user clicks onto the taskbar it stops auto-hiding as long as it is focused.

How would I go about setting focus to the Windows taskbar?

1
Maybe simulating mouse click will do? What problem you are trying to solve by activating taskbar? For what is it needed?Sinatr
Do you want your app not taking focus (e.g. like here)?Sinatr
Sounds like an XY Problem. What problem are you ultimately trying to solve?IInspectable
Do you want to show auto-hidden taskbar while your app is active?Sinatr
How have you embedded your search box inside the taskbar?David Heffernan

1 Answers

3
votes

You need to use SetWindowPos instead of SetForegroundWindow and feed it a flag to show the window. That flag is 0x0040 according to the documentation.

Then, if you want it to truly have focus, then you can call SetForegroundWindow.

[DllImport("user32.dll", SetLastError = true)]
private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

Here's a simple working example of the "show" of the taskbar:

MainWindow.xaml

<Window x:Class="_65994896.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Show Taskbar" Click="Button_Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Runtime.InteropServices;
using System.Windows;

namespace _65994896
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr FindWindow( string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

        [Flags]
        private enum SetWindowPosFlags : uint
        {
            SWP_HIDEWINDOW = 0x0080,
            SWP_SHOWWINDOW = 0x0040
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var window = FindWindow("Shell_traywnd", "");
            SetWindowPos(window, IntPtr.Zero, 0, 0, 0, 0, (uint)SetWindowPosFlags.SWP_SHOWWINDOW);
        }
    }
}