Maximize a custom WPF Window without hiding the taskbar

This strategy (see Design Patterns) will helps you to maximize a customized WPF Window without hiding the Windows Taskbar.

 

Last but not least

If you have set 

WindowStyle="None"

Dont forget to set 

AllowsTransparency="True"

To disapear the fixed bar on the top of the Window...

Enjoy !

 

The code : 

AntiHideWinTaskBarStrategy.cs (7,74 kb)

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

namespace WinExtensions
{
    internal sealed class AntiHideWinTaskBarStrategy
    {
        private const int WM_GetMinMaxInfo = 0x0024;
        private const int W32_MONITOR_DEFAULTTONEAREST = 0x00000002;

        private static readonly IntPtr defaultIntPtr = (IntPtr)0;

        [DllImport("user32")]
        private static extern bool GetMonitorInfo(IntPtr hMonitor, W32MonitorInfo lpmi);

        [DllImport("user32.dll")]
        private static extern bool GetCursorPos(ref Point lpPoint);

        [DllImport("User32", EntryPoint = "MonitorFromWindow")]
        private static extern IntPtr GetMonitorFromWindow(IntPtr handle, int flags);

        private readonly Window window;

        public AntiHideWinTaskBarStrategy(Window window)
        {
            this.window = window;
            this.window.AllowsTransparency = true;

            window.SourceInitialized += WindowOnSourceInitialized;
        }

        private void WindowOnSourceInitialized(object sender, EventArgs e)
        {
            var currentWindow = sender as Window;
            var handle = (new System.Windows.Interop.WindowInteropHelper(window)).Handle;
            System.Windows.Interop.HwndSource.FromHwnd(handle)?.AddHook(OnAllWinMessagesReceived_WinMinMaxInfo);
        }

        private static IntPtr OnAllWinMessagesReceived_WinMinMaxInfo(
            IntPtr hwnd,
            int w32MessageCode,
            IntPtr wParam,
            IntPtr lParam,
            ref bool handled)
        {
            if (!w32MessageCode.Equals(WM_GetMinMaxInfo)) return defaultIntPtr;

            WmOverrideWinMinMaxSize(hwnd, lParam);
            handled = true;

            return defaultIntPtr;
        }

        private static void WmOverrideWinMinMaxSize(IntPtr hwnd, IntPtr lParam)
        {
            var minMaxInfo = (W32MinMaxInfo)Marshal.PtrToStructure(lParam, typeof(W32MinMaxInfo));

            // Adjust the maximized size and position to fit the work area of the correct monitor
            var monitor = GetMonitorFromWindow(hwnd, W32_MONITOR_DEFAULTTONEAREST);

            if (monitor != IntPtr.Zero)
            {
                var monitorInfo = new W32MonitorInfo();
                GetMonitorInfo(monitor, monitorInfo);
                var rcWorkArea = monitorInfo.rcWork;
                var rcMonitorArea = monitorInfo.rcMonitor;

                minMaxInfo.ptMaxTrackSize.x = Math.Abs(rcWorkArea.Width);
                minMaxInfo.ptMaxTrackSize.y = Math.Abs(rcWorkArea.Height);
                minMaxInfo.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
                minMaxInfo.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
                minMaxInfo.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
                minMaxInfo.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
            }

            Marshal.StructureToPtr(minMaxInfo, lParam, true);
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct W32Point
        {
            /// <summary>
            /// x coordinate of point.
            /// </summary>
            public int x;
            /// <summary>
            /// y coordinate of point.
            /// </summary>
            public int y;

            /// <summary>
            /// Construct a point of coordinates (x,y).
            /// </summary>
            public W32Point(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
        }

        [StructLayout(LayoutKind.Sequential, Pack = 0)]
        private struct W32Rectangle
        {
            /// <summary> Win32 </summary>
            public int left;
            /// <summary> Win32 </summary>
            public int top;
            /// <summary> Win32 </summary>
            public int right;
            /// <summary> Win32 </summary>
            public int bottom;

            /// <summary> Win32 </summary>
            public static readonly W32Rectangle Empty = new W32Rectangle();

            /// <summary> Win32 </summary>
            public int Width
            {
                get { return Math.Abs(right - left); }  // Abs needed for BIDI OS
            }
            /// <summary> Win32 </summary>
            public int Height
            {
                get { return bottom - top; }
            }

            /// <summary> Win32 </summary>
            public W32Rectangle(int left, int top, int right, int bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }


            /// <summary> Win32 </summary>
            public W32Rectangle(W32Rectangle rcSrc)
            {
                left = rcSrc.left;
                top = rcSrc.top;
                right = rcSrc.right;
                bottom = rcSrc.bottom;
            }

            /// <summary> Win32 </summary>
            public bool IsEmpty
            {
                get
                {
                    // BUGBUG : On Bidi OS (hebrew arabic) left > right
                    return left >= right || top >= bottom;
                }
            }
            /// <summary> Return a user friendly representation of this struct </summary>
            public override string ToString()
            {
                if (this == Empty) { return "RECT {Empty}"; }
                return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
            }

            /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
            public override bool Equals(object obj)
            {
                if (!(obj is Rect)) { return false; }
                return (this == (W32Rectangle)obj);
            }

            /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
            public override int GetHashCode()
            {
                return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
            }


            /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
            public static bool operator ==(W32Rectangle rect1, W32Rectangle rect2)
            {
                return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
            }

            /// <summary> Determine if 2 RECT are different(deep compare)</summary>
            public static bool operator !=(W32Rectangle rect1, W32Rectangle rect2)
            {
                return !(rect1 == rect2);
            }


        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        private class W32MonitorInfo
        {
            /// <summary>
            /// </summary>            
            public int cbSize = Marshal.SizeOf(typeof(W32MonitorInfo));

            /// <summary>
            /// </summary>            
            public W32Rectangle rcMonitor = new W32Rectangle();

            /// <summary>
            /// </summary>            
            public W32Rectangle rcWork = new W32Rectangle();

            /// <summary>
            /// </summary>            
            public int dwFlags = 0;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct W32MinMaxInfo
        {
            public W32Point ptReserved;
            public W32Point ptMaxSize;
            public W32Point ptMaxPosition;
            public W32Point ptMinTrackSize;
            public W32Point ptMaxTrackSize;
        };
    }
}

 

Comments are closed