Programmatically show and hide the Windows taskbar

I've been working on an application launcher for multi-touch surfaces. As a part of the experience, I wanted to hide the Windows taskbar, as it didn't feel very 360 degrees - multi-touch - multi-user, as was the intention of my launcher. Anyway, this is what I compiled:

WPF App Constructor

        public App()
        {
            ViewModel = new AppViewModel();
            _systemTrayHandle = FindWindow("Shell_traywnd", "");
            _startButtonHandle = FindStartButton();
        }

Imports
        #region Windows API Imports & Utilities
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", SetLastError = false)]
        static extern IntPtr GetDesktopWindow();

        private IntPtr FindStartButton()
        {
            IntPtr handleOfDesktop = GetDesktopWindow();
            IntPtr handleOfStartButton = FindWindowEx(handleOfDesktop, IntPtr.Zero, "Button", null);
            return handleOfStartButton;
        }

What you're here for:
        private void HideWindowsTaskbar()
        {
            SetWindowPos(_systemTrayHandle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_HIDEWINDOW);
            SetWindowPos(_startButtonHandle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_HIDEWINDOW);
        }

        private void ShowWindowsTaskbar()
        {
            SetWindowPos(_systemTrayHandle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_SHOWWINDOW);
            SetWindowPos(_startButtonHandle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_SHOWWINDOW);
        }
        #endregion

The SetWindowsPosFlags enumeration, I found at pinvoke.net - your source for platform invoke-stuff.

Comments

Popular posts from this blog

Auto Mapper and Record Types - will they blend?

Unit testing your Azure functions - part 2: Queues and Blobs

Testing WCF services with user credentials and binary endpoints