<p>Is there any complete guidance on doing AppBar docking (such as locking to the screen edge) in WPF? I understand there are InterOp calls that need to be made, but I'm looking for either a proof of concept based on a simple WPF form, or a componentized version that can be consumed.</p>

<p>Related resources:</p>

<ul>
<li><a href="http://www.codeproject.com/KB/dotnet/AppBar.aspx" rel="noreferrer">http://www.codeproject.com/KB/dotnet/AppBar.aspx</a></li>
<li><a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/05c73c9c-e85d-4ecd-b9b6-4c714a65e72b/" rel="noreferrer">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/05c73c9c-e85d-4ecd-b9b6-4c714a65e72b/</a></li>
</ul>

[{'answer_id': 84987, 'author': 'Philip Rieck', 'author_id': 12643, 'author_profile': 'https://Stackoverflow.com/users/12643', 'pm_score': 8, 'selected': True, 'text': '<p><strong>Please Note:</strong> This question gathered a good amount of feedback, and some people below have made great points or fixes.  Therefore, while I\'ll keep the code here (and possibly update it), I\'ve also <strong>created a <a href="https://github.com/PhilipRieck/WpfAppBar" rel="noreferrer">WpfAppBar project on github</a></strong>.  Feel free to send pull requests.  </p>\n\n<p>That same project also builds to a <a href="https://www.nuget.org/packages/WpfAppBar/" rel="noreferrer">WpfAppBar nuget package</a> </p>\n\n<hr>\n\n<p>I took the code from the first link provided in the question ( <a href="http://www.codeproject.com/KB/dotnet/AppBar.aspx" rel="noreferrer">http://www.codeproject.com/KB/dotnet/AppBar.aspx</a> ) and modified it to do two things:</p>\n\n<ol>\n<li>Work with WPF</li>\n<li>Be "standalone" - if you put this single file in your project, you can call AppBarFunctions.SetAppBar(...) without any further modification to the window.</li>\n</ol>\n\n<p>This approach doesn\'t create a base class.</p>\n\n<p>To use, just call this code from anywhere within a normal wpf window (say a button click or the initialize).   Note that you can not call this until AFTER the window is initialized, if the HWND hasn\'t been created yet (like in the constructor), an error will occur.</p>\n\n<p>Make the window an appbar:</p>\n\n<pre><code>AppBarFunctions.SetAppBar( this, ABEdge.Right );\n</code></pre>\n\n<p>Restore the window to a normal window:</p>\n\n<pre><code>AppBarFunctions.SetAppBar( this, ABEdge.None );\n</code></pre>\n\n<p>Here\'s the full code to the file - <strong>note</strong> you\'ll want to change the namespace on line 7 to something apropriate.</p>\n\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\nusing System.Windows;\nusing System.Windows.Interop;\nusing System.Windows.Threading;\n\nnamespace AppBarApplication\n{    \n    public enum ABEdge : int\n    {\n        Left = 0,\n        Top,\n        Right,\n        Bottom,\n        None\n    }\n\n    internal static class AppBarFunctions\n    {\n        [StructLayout(LayoutKind.Sequential)]\n        private struct RECT\n        {\n            public int left;\n            public int top;\n            public int right;\n            public int bottom;\n        }\n\n        [StructLayout(LayoutKind.Sequential)]\n        private struct APPBARDATA\n        {\n            public int cbSize;\n            public IntPtr hWnd;\n            public int uCallbackMessage;\n            public int uEdge;\n            public RECT rc;\n            public IntPtr lParam;\n        }\n\n        private enum ABMsg : int\n        {\n            ABM_NEW = 0,\n            ABM_REMOVE,\n            ABM_QUERYPOS,\n            ABM_SETPOS,\n            ABM_GETSTATE,\n            ABM_GETTASKBARPOS,\n            ABM_ACTIVATE,\n            ABM_GETAUTOHIDEBAR,\n            ABM_SETAUTOHIDEBAR,\n            ABM_WINDOWPOSCHANGED,\n            ABM_SETSTATE\n        }\n        private enum ABNotify : int\n        {\n            ABN_STATECHANGE = 0,\n            ABN_POSCHANGED,\n            ABN_FULLSCREENAPP,\n            ABN_WINDOWARRANGE\n        }\n\n        [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]\n        private static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);\n\n        [DllImport("User32.dll", CharSet = CharSet.Auto)]\n        private static extern int RegisterWindowMessage(string msg);\n\n        private class RegisterInfo\n        {\n            public int CallbackId { get; set; }\n            public bool IsRegistered { get; set; }\n            public Window Window { get; set; }\n            public ABEdge Edge { get; set; }\n            public WindowStyle OriginalStyle { get; set; }            \n            public Point OriginalPosition { get; set; }\n            public Size OriginalSize { get; set; }\n            public ResizeMode OriginalResizeMode { get; set; }\n\n\n            public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, \n                                    IntPtr lParam, ref bool handled)\n            {\n                if (msg == CallbackId)\n                {\n                    if (wParam.ToInt32() == (int)ABNotify.ABN_POSCHANGED)\n                    {\n                        ABSetPos(Edge, Window);\n                        handled = true;\n                    }\n                }\n                return IntPtr.Zero;\n            }\n\n        }\n        private static Dictionary&lt;Window, RegisterInfo&gt; s_RegisteredWindowInfo \n            = new Dictionary&lt;Window, RegisterInfo&gt;();\n        private static RegisterInfo GetRegisterInfo(Window appbarWindow)\n        {\n            RegisterInfo reg;\n            if( s_RegisteredWindowInfo.ContainsKey(appbarWindow))\n            {\n                reg = s_RegisteredWindowInfo[appbarWindow];\n            }\n            else\n            {\n                reg = new RegisterInfo()\n                    {\n                        CallbackId = 0,\n                        Window = appbarWindow,\n                        IsRegistered = false,\n                        Edge = ABEdge.Top,\n                        OriginalStyle = appbarWindow.WindowStyle,                        \n                        OriginalPosition =new Point( appbarWindow.Left, appbarWindow.Top),\n                        OriginalSize = \n                            new Size( appbarWindow.ActualWidth, appbarWindow.ActualHeight),\n                        OriginalResizeMode = appbarWindow.ResizeMode,\n                    };\n                s_RegisteredWindowInfo.Add(appbarWindow, reg);\n            }\n            return reg;\n        }\n\n        private static void RestoreWindow(Window appbarWindow)\n        {\n            RegisterInfo info = GetRegisterInfo(appbarWindow);\n\n            appbarWindow.WindowStyle = info.OriginalStyle;            \n            appbarWindow.ResizeMode = info.OriginalResizeMode;\n            appbarWindow.Topmost = false;\n\n            Rect rect = new Rect(info.OriginalPosition.X, info.OriginalPosition.Y, \n                info.OriginalSize.Width, info.OriginalSize.Height);\n            appbarWindow.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,\n                    new ResizeDelegate(DoResize), appbarWindow, rect);\n\n        }\n\n        public static void SetAppBar(Window appbarWindow, ABEdge edge)\n        {\n            RegisterInfo info = GetRegisterInfo(appbarWindow);\n            info.Edge = edge;\n\n            APPBARDATA abd = new APPBARDATA();\n            abd.cbSize = Marshal.SizeOf(abd);\n            abd.hWnd = new WindowInteropHelper(appbarWindow).Handle;\n\n            if( edge == ABEdge.None)\n            {\n                if( info.IsRegistered)\n                {\n                    SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);\n                    info.IsRegistered = false;\n                }\n                RestoreWindow(appbarWindow);\n                return;\n            }\n\n            if (!info.IsRegistered)\n            {\n                info.IsRegistered = true; \n                info.CallbackId = RegisterWindowMessage("AppBarMessage");\n                abd.uCallbackMessage = info.CallbackId;\n\n                uint ret = SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);\n\n                HwndSource source = HwndSource.FromHwnd(abd.hWnd);\n                source.AddHook(new HwndSourceHook(info.WndProc));\n            }\n\n            appbarWindow.WindowStyle = WindowStyle.None;            \n            appbarWindow.ResizeMode = ResizeMode.NoResize;\n            appbarWindow.Topmost = true;\n\n            ABSetPos(info.Edge, appbarWindow);                \n        }\n\n        private delegate void ResizeDelegate(Window appbarWindow, Rect rect);\n        private static void DoResize(Window appbarWindow, Rect rect)\n        {\n            appbarWindow.Width = rect.Width;\n            appbarWindow.Height = rect.Height;\n            appbarWindow.Top = rect.Top;\n            appbarWindow.Left = rect.Left;\n        }\n\n\n\n        private static void ABSetPos(ABEdge edge, Window appbarWindow)\n        {\n            APPBARDATA barData = new APPBARDATA();\n            barData.cbSize = Marshal.SizeOf(barData);\n            barData.hWnd = new WindowInteropHelper(appbarWindow).Handle;\n            barData.uEdge = (int)edge;\n\n            if (barData.uEdge == (int)ABEdge.Left || barData.uEdge == (int)ABEdge.Right)\n            {\n                barData.rc.top = 0;\n                barData.rc.bottom = (int)SystemParameters.PrimaryScreenHeight;\n                if (barData.uEdge == (int)ABEdge.Left)\n                {\n                    barData.rc.left = 0;\n                    barData.rc.right = (int)Math.Round(appbarWindow.ActualWidth);\n                }\n                else\n                {\n                    barData.rc.right = (int)SystemParameters.PrimaryScreenWidth;\n                    barData.rc.left = barData.rc.right - (int)Math.Round(appbarWindow.ActualWidth);\n                }\n            }\n            else\n            {\n                barData.rc.left = 0;\n                barData.rc.right = (int)SystemParameters.PrimaryScreenWidth;\n                if (barData.uEdge == (int)ABEdge.Top)\n                {\n                    barData.rc.top = 0;\n                    barData.rc.bottom = (int)Math.Round(appbarWindow.ActualHeight);\n                }\n                else\n                {\n                    barData.rc.bottom = (int)SystemParameters.PrimaryScreenHeight;\n                    barData.rc.top = barData.rc.bottom - (int)Math.Round(appbarWindow.ActualHeight);\n                }\n            }\n\n            SHAppBarMessage((int)ABMsg.ABM_QUERYPOS, ref barData);\n            SHAppBarMessage((int)ABMsg.ABM_SETPOS, ref barData);\n\n            Rect rect = new Rect((double)barData.rc.left, (double)barData.rc.top, \n                (double)(barData.rc.right - barData.rc.left), (double)(barData.rc.bottom - barData.rc.top));\n            //This is done async, because WPF will send a resize after a new appbar is added.  \n            //if we size right away, WPFs resize comes last and overrides us.\n            appbarWindow.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, \n                new ResizeDelegate(DoResize), appbarWindow, rect);\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 624235, 'author': 'Shuft', 'author_id': 1587, 'author_profile': 'https://Stackoverflow.com/users/1587', 'pm_score': 2, 'selected': False, 'text': '<p>Very happy to have found this question. Above class is really useful, but doesnt quite cover all the bases of AppBar implementation. </p>\n\n<p>To fully implement all the behaviour of an AppBar (cope with fullscreen apps etc) you\'re going to want to read this MSDN article too.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/bb776821.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/bb776821.aspx</a></p>\n'}, {'answer_id': 779175, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Sorry, the last code I posted didn't work when the Taskbar is resized. The following code change seems to work better:</p>\n\n<blockquote>\n<pre><code>  SHAppBarMessage((int)ABMsg.ABM_QUERYPOS, ref barData);\n\n  if (barData.uEdge == (int)ABEdge.Top)\n    barData.rc.bottom = barData.rc.top + (int)Math.Round(appbarWindow.ActualHeight);\n  else if (barData.uEdge == (int)ABEdge.Bottom)\n    barData.rc.top = barData.rc.bottom - (int)Math.Round(appbarWindow.ActualHeight);\n\n  SHAppBarMessage((int)ABMsg.ABM_SETPOS, ref barData);\n</code></pre>\n</blockquote>\n"}, {'answer_id': 1117766, 'author': 'logicnp', 'author_id': 51919, 'author_profile': 'https://Stackoverflow.com/users/51919', 'pm_score': 1, 'selected': False, 'text': '<p>As a commercial alternative, see the ready-to-use <a href="http://www.ssware.com/shlobj/shlobj.htm" rel="nofollow noreferrer">ShellAppBar</a> component for WPF which supports all cases and secnarios such as taskbar docked to left,right,top,bottom edge, support for multiple monitors, drag-docking, autohide , etc etc. It may save you time and money over trying to handle all these cases yourself.</p>\n\n<p><strong>DISCLAIMER</strong>: I work for LogicNP Software, the developer of ShellAppBar.</p>\n'}, {'answer_id': 5610186, 'author': 'Dmitry Andreev', 'author_id': 700625, 'author_profile': 'https://Stackoverflow.com/users/700625', 'pm_score': 2, 'selected': False, 'text': '<p>Sorry for my English... Here is the Philip Rieck\'s solution with some corrects. It correctly works with Taskbar position and size changes.</p>\n\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\nusing System.Windows;\nusing System.Windows.Interop;\nusing System.Windows.Threading;\n\nnamespace wpf_appbar\n{\n    public enum ABEdge : int\n    {\n        Left,\n        Top,\n        Right,\n        Bottom,\n        None\n    }\n\n    internal static class AppBarFunctions\n    {\n        [StructLayout(LayoutKind.Sequential)]\n        private struct RECT\n        {\n            public int Left;\n            public int Top;\n            public int Right;\n            public int Bottom;\n            public RECT(Rect r)\n            {\n                Left = (int)r.Left;\n                Right = (int)r.Right;\n                Top = (int)r.Top;\n                Bottom = (int)r.Bottom;\n            }\n            public static bool operator ==(RECT r1, RECT r2)\n            {\n                return r1.Bottom == r2.Bottom &amp;&amp; r1.Left == r2.Left &amp;&amp; r1.Right == r2.Right &amp;&amp; r1.Top == r2.Top;\n            }\n            public static bool operator !=(RECT r1, RECT r2)\n            {\n                return !(r1 == r2);\n            }\n            public override bool Equals(object obj)\n            {\n                return base.Equals(obj);\n            }\n            public override int GetHashCode()\n            {\n                return base.GetHashCode();\n            }\n        }\n\n        [StructLayout(LayoutKind.Sequential)]\n        private struct APPBARDATA\n        {\n            public int cbSize;\n            public IntPtr hWnd;\n            public int uCallbackMessage;\n            public int uEdge;\n            public RECT rc;\n            public IntPtr lParam;\n        }\n\n        private enum ABMsg : int\n        {\n            ABM_NEW = 0,\n            ABM_REMOVE,\n            ABM_QUERYPOS,\n            ABM_SETPOS,\n            ABM_GETSTATE,\n            ABM_GETTASKBARPOS,\n            ABM_ACTIVATE,\n            ABM_GETAUTOHIDEBAR,\n            ABM_SETAUTOHIDEBAR,\n            ABM_WINDOWPOSCHANGED,\n            ABM_SETSTATE\n        }\n        private enum ABNotify : int\n        {\n            ABN_STATECHANGE = 0,\n            ABN_POSCHANGED,\n            ABN_FULLSCREENAPP,\n            ABN_WINDOWARRANGE\n        }\n\n        private enum TaskBarPosition : int\n        {\n            Left,\n            Top,\n            Right,\n            Bottom\n        }\n\n        [StructLayout(LayoutKind.Sequential)]\n        class TaskBar\n        {\n            public TaskBarPosition Position;\n            public TaskBarPosition PreviousPosition;\n            public RECT Rectangle;\n            public RECT PreviousRectangle;\n            public int Width;\n            public int PreviousWidth;\n            public int Height;\n            public int PreviousHeight;\n            public TaskBar()\n            {\n                Refresh();\n            }\n            public void Refresh()\n            {\n                APPBARDATA msgData = new APPBARDATA();\n                msgData.cbSize = Marshal.SizeOf(msgData);\n                SHAppBarMessage((int)ABMsg.ABM_GETTASKBARPOS, ref msgData);\n                PreviousPosition = Position;\n                PreviousRectangle = Rectangle;\n                PreviousHeight = Height;\n                PreviousWidth = Width;\n                Rectangle = msgData.rc;\n                Width = Rectangle.Right - Rectangle.Left;\n                Height = Rectangle.Bottom - Rectangle.Top;\n                int h = (int)SystemParameters.PrimaryScreenHeight;\n                int w = (int)SystemParameters.PrimaryScreenWidth;\n                if (Rectangle.Bottom == h &amp;&amp; Rectangle.Top != 0) Position = TaskBarPosition.Bottom;\n                else if (Rectangle.Top == 0 &amp;&amp; Rectangle.Bottom != h) Position = TaskBarPosition.Top;\n                else if (Rectangle.Right == w &amp;&amp; Rectangle.Left != 0) Position = TaskBarPosition.Right;\n                else if (Rectangle.Left == 0 &amp;&amp; Rectangle.Right != w) Position = TaskBarPosition.Left;\n            }\n        }\n\n        [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]\n        private static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);\n\n        [DllImport("User32.dll", CharSet = CharSet.Auto)]\n        private static extern int RegisterWindowMessage(string msg);\n\n        private class RegisterInfo\n        {\n            public int CallbackId { get; set; }\n            public bool IsRegistered { get; set; }\n            public Window Window { get; set; }\n            public ABEdge Edge { get; set; }\n            public ABEdge PreviousEdge { get; set; }\n            public WindowStyle OriginalStyle { get; set; }\n            public Point OriginalPosition { get; set; }\n            public Size OriginalSize { get; set; }\n            public ResizeMode OriginalResizeMode { get; set; }\n\n\n            public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam,\n                                    IntPtr lParam, ref bool handled)\n            {\n                if (msg == CallbackId)\n                {\n                    if (wParam.ToInt32() == (int)ABNotify.ABN_POSCHANGED)\n                    {\n                        PreviousEdge = Edge;\n                        ABSetPos(Edge, PreviousEdge, Window);\n                        handled = true;\n                    }\n                }\n                return IntPtr.Zero;\n            }\n\n        }\n        private static Dictionary&lt;Window, RegisterInfo&gt; s_RegisteredWindowInfo\n            = new Dictionary&lt;Window, RegisterInfo&gt;();\n        private static RegisterInfo GetRegisterInfo(Window appbarWindow)\n        {\n            RegisterInfo reg;\n            if (s_RegisteredWindowInfo.ContainsKey(appbarWindow))\n            {\n                reg = s_RegisteredWindowInfo[appbarWindow];\n            }\n            else\n            {\n                reg = new RegisterInfo()\n                {\n                    CallbackId = 0,\n                    Window = appbarWindow,\n                    IsRegistered = false,\n                    Edge = ABEdge.None,\n                    PreviousEdge = ABEdge.None,\n                    OriginalStyle = appbarWindow.WindowStyle,\n                    OriginalPosition = new Point(appbarWindow.Left, appbarWindow.Top),\n                    OriginalSize =\n                        new Size(appbarWindow.ActualWidth, appbarWindow.ActualHeight),\n                    OriginalResizeMode = appbarWindow.ResizeMode,\n                };\n                s_RegisteredWindowInfo.Add(appbarWindow, reg);\n            }\n            return reg;\n        }\n\n        private static void RestoreWindow(Window appbarWindow)\n        {\n            RegisterInfo info = GetRegisterInfo(appbarWindow);\n\n            appbarWindow.WindowStyle = info.OriginalStyle;\n            appbarWindow.ResizeMode = info.OriginalResizeMode;\n            appbarWindow.Topmost = false;\n\n            Rect rect = new Rect(info.OriginalPosition.X, info.OriginalPosition.Y,\n                info.OriginalSize.Width, info.OriginalSize.Height);\n            appbarWindow.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,\n                    new ResizeDelegate(DoResize), appbarWindow, rect);\n\n        }\n\n\n        public static void SetAppBar(Window appbarWindow, ABEdge edge)\n        {\n            RegisterInfo info = GetRegisterInfo(appbarWindow);\n            info.Edge = edge;\n\n            APPBARDATA abd = new APPBARDATA();\n            abd.cbSize = Marshal.SizeOf(abd);\n            abd.hWnd = new WindowInteropHelper(appbarWindow).Handle;\n\n            if (edge == ABEdge.None)\n            {\n                if (info.IsRegistered)\n                {\n                    SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);\n                    info.IsRegistered = false;\n                }\n                RestoreWindow(appbarWindow);\n                info.PreviousEdge = info.Edge;\n                return;\n            }\n\n            if (!info.IsRegistered)\n            {\n                info.IsRegistered = true;\n                info.CallbackId = RegisterWindowMessage("AppBarMessage");\n                abd.uCallbackMessage = info.CallbackId;\n\n                uint ret = SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);\n\n                HwndSource source = HwndSource.FromHwnd(abd.hWnd);\n                source.AddHook(new HwndSourceHook(info.WndProc));\n            }\n\n            appbarWindow.WindowStyle = WindowStyle.None;\n            appbarWindow.ResizeMode = ResizeMode.NoResize;\n            appbarWindow.Topmost = true;\n\n            ABSetPos(info.Edge, info.PreviousEdge, appbarWindow);\n        }\n\n        private delegate void ResizeDelegate(Window appbarWindow, Rect rect);\n        private static void DoResize(Window appbarWindow, Rect rect)\n        {\n            appbarWindow.Width = rect.Width;\n            appbarWindow.Height = rect.Height;\n            appbarWindow.Top = rect.Top;\n            appbarWindow.Left = rect.Left;\n        }\n\n        static TaskBar tb = new TaskBar();\n\n        private static void ABSetPos(ABEdge edge, ABEdge prevEdge, Window appbarWindow)\n        {\n            APPBARDATA barData = new APPBARDATA();\n            barData.cbSize = Marshal.SizeOf(barData);\n            barData.hWnd = new WindowInteropHelper(appbarWindow).Handle;\n            barData.uEdge = (int)edge;\n            RECT wa = new RECT(SystemParameters.WorkArea);\n            tb.Refresh();\n            switch (edge)\n            {\n                case ABEdge.Top:\n                    barData.rc.Left = wa.Left - (prevEdge == ABEdge.Left ? (int)Math.Round(appbarWindow.ActualWidth) : 0);\n                    barData.rc.Right = wa.Right + (prevEdge == ABEdge.Right ? (int)Math.Round(appbarWindow.ActualWidth) : 0);\n                    barData.rc.Top = wa.Top - (prevEdge == ABEdge.Top ? (int)Math.Round(appbarWindow.ActualHeight) : 0) - ((tb.Position != TaskBarPosition.Top &amp;&amp; tb.PreviousPosition == TaskBarPosition.Top) ? tb.Height : 0) + ((tb.Position == TaskBarPosition.Top &amp;&amp; tb.PreviousPosition != TaskBarPosition.Top) ? tb.Height : 0);\n                    barData.rc.Bottom = barData.rc.Top + (int)Math.Round(appbarWindow.ActualHeight);\n                    break;\n                case ABEdge.Bottom:\n                    barData.rc.Left = wa.Left - (prevEdge == ABEdge.Left ? (int)Math.Round(appbarWindow.ActualWidth) : 0);\n                    barData.rc.Right = wa.Right + (prevEdge == ABEdge.Right ? (int)Math.Round(appbarWindow.ActualWidth) : 0);\n                    barData.rc.Bottom = wa.Bottom + (prevEdge == ABEdge.Bottom ? (int)Math.Round(appbarWindow.ActualHeight) : 0) - 1 + ((tb.Position != TaskBarPosition.Bottom &amp;&amp; tb.PreviousPosition == TaskBarPosition.Bottom) ? tb.Height : 0) - ((tb.Position == TaskBarPosition.Bottom &amp;&amp; tb.PreviousPosition != TaskBarPosition.Bottom) ? tb.Height : 0);\n                    barData.rc.Top = barData.rc.Bottom - (int)Math.Round(appbarWindow.ActualHeight);\n                    break;\n            }\n\n            SHAppBarMessage((int)ABMsg.ABM_QUERYPOS, ref barData);\n            switch (barData.uEdge)\n            {\n                case (int)ABEdge.Bottom:\n                    if (tb.Position == TaskBarPosition.Bottom &amp;&amp; tb.PreviousPosition == tb.Position)\n                    {\n                        barData.rc.Top += (tb.PreviousHeight - tb.Height);\n                        barData.rc.Bottom = barData.rc.Top + (int)appbarWindow.ActualHeight;\n                    }\n                    break;\n                case (int)ABEdge.Top:\n                    if (tb.Position == TaskBarPosition.Top &amp;&amp; tb.PreviousPosition == tb.Position)\n                    {\n                        if (tb.PreviousHeight - tb.Height &gt; 0) barData.rc.Top -= (tb.PreviousHeight - tb.Height);\n                        barData.rc.Bottom = barData.rc.Top + (int)appbarWindow.ActualHeight;\n                    }\n                    break;\n            }\n            SHAppBarMessage((int)ABMsg.ABM_SETPOS, ref barData);\n\n            Rect rect = new Rect((double)barData.rc.Left, (double)barData.rc.Top, (double)(barData.rc.Right - barData.rc.Left), (double)(barData.rc.Bottom - barData.rc.Top));\n            appbarWindow.Dispatcher.BeginInvoke(new ResizeDelegate(DoResize), DispatcherPriority.ApplicationIdle, appbarWindow, rect);\n        }\n    }\n}\n</code></pre>\n\n<p>The same code you can write for the Left and Right edges.\nGood job, Philip Rieck, thank you!</p>\n'}, {'answer_id': 18896608, 'author': 'Miky Jadro', 'author_id': 2795750, 'author_profile': 'https://Stackoverflow.com/users/2795750', 'pm_score': 2, 'selected': False, 'text': '<p>I modified code from Philip Rieck (btw. Thanks a lot) to work in multiple display settings. Here\'s my solution.</p>\n\n<pre><code>using System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\nusing System.Windows;\nusing System.Windows.Interop;\nusing System.Windows.Threading;\n\nnamespace AppBarApplication\n{\n    public enum ABEdge : int\n    {\n        Left = 0,\n        Top,\n        Right,\n        Bottom,\n        None\n    }\n\n    internal static class AppBarFunctions\n    {\n        [StructLayout(LayoutKind.Sequential)]\n        private struct RECT\n        {\n            public int left;\n            public int top;\n            public int right;\n            public int bottom;\n        }\n\n        [StructLayout(LayoutKind.Sequential)]\n        private struct APPBARDATA\n        {\n            public int cbSize;\n            public IntPtr hWnd;\n            public int uCallbackMessage;\n            public int uEdge;\n            public RECT rc;\n            public IntPtr lParam;\n        }\n\n        [StructLayout(LayoutKind.Sequential)]\n        private struct MONITORINFO\n        {\n          public int cbSize;\n          public RECT  rcMonitor;\n          public RECT  rcWork;\n          public int dwFlags;\n        }\n\n        private enum ABMsg : int\n        {\n            ABM_NEW = 0,\n            ABM_REMOVE,\n            ABM_QUERYPOS,\n            ABM_SETPOS,\n            ABM_GETSTATE,\n            ABM_GETTASKBARPOS,\n            ABM_ACTIVATE,\n            ABM_GETAUTOHIDEBAR,\n            ABM_SETAUTOHIDEBAR,\n            ABM_WINDOWPOSCHANGED,\n            ABM_SETSTATE\n        }\n        private enum ABNotify : int\n        {\n            ABN_STATECHANGE = 0,\n            ABN_POSCHANGED,\n            ABN_FULLSCREENAPP,\n            ABN_WINDOWARRANGE\n        }\n\n        [DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]\n        private static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);\n\n        [DllImport("User32.dll", CharSet = CharSet.Auto)]\n        private static extern int RegisterWindowMessage(string msg);\n\n        [DllImport("User32.dll", CharSet = CharSet.Auto)]\n        private static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);\n\n        [DllImport("User32.dll", CharSet = CharSet.Auto)]\n        private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO mi);\n\n\n        private const int MONITOR_DEFAULTTONEAREST = 0x2;\n        private const int MONITORINFOF_PRIMARY = 0x1;\n\n        private class RegisterInfo\n        {\n            public int CallbackId { get; set; }\n            public bool IsRegistered { get; set; }\n            public Window Window { get; set; }\n            public ABEdge Edge { get; set; }\n            public WindowStyle OriginalStyle { get; set; }\n            public Point OriginalPosition { get; set; }\n            public Size OriginalSize { get; set; }\n            public ResizeMode OriginalResizeMode { get; set; }\n\n\n            public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam,\n                                    IntPtr lParam, ref bool handled)\n            {\n                if (msg == CallbackId)\n                {\n                    if (wParam.ToInt32() == (int)ABNotify.ABN_POSCHANGED)\n                    {\n                        ABSetPos(Edge, Window);\n                        handled = true;\n                    }\n                }\n                return IntPtr.Zero;\n            }\n\n        }\n        private static Dictionary&lt;Window, RegisterInfo&gt; s_RegisteredWindowInfo\n            = new Dictionary&lt;Window, RegisterInfo&gt;();\n        private static RegisterInfo GetRegisterInfo(Window appbarWindow)\n        {\n            RegisterInfo reg;\n            if (s_RegisteredWindowInfo.ContainsKey(appbarWindow))\n            {\n                reg = s_RegisteredWindowInfo[appbarWindow];\n            }\n            else\n            {\n                reg = new RegisterInfo()\n                {\n                    CallbackId = 0,\n                    Window = appbarWindow,\n                    IsRegistered = false,\n                    Edge = ABEdge.Top,\n                    OriginalStyle = appbarWindow.WindowStyle,\n                    OriginalPosition = new Point(appbarWindow.Left, appbarWindow.Top),\n                    OriginalSize =\n                        new Size(appbarWindow.ActualWidth, appbarWindow.ActualHeight),\n                    OriginalResizeMode = appbarWindow.ResizeMode,\n                };\n                s_RegisteredWindowInfo.Add(appbarWindow, reg);\n            }\n            return reg;\n        }\n\n        private static void RestoreWindow(Window appbarWindow)\n        {\n            RegisterInfo info = GetRegisterInfo(appbarWindow);\n\n            appbarWindow.WindowStyle = info.OriginalStyle;\n            appbarWindow.ResizeMode = info.OriginalResizeMode;\n            appbarWindow.Topmost = false;\n\n            Rect rect = new Rect(info.OriginalPosition.X, info.OriginalPosition.Y,\n                info.OriginalSize.Width, info.OriginalSize.Height);\n            appbarWindow.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,\n                    new ResizeDelegate(DoResize), appbarWindow, rect);\n\n        }\n\n        public static void SetAppBar(Window appbarWindow, ABEdge edge)\n        {\n            RegisterInfo info = GetRegisterInfo(appbarWindow);\n\n            info.Edge = edge;\n\n            APPBARDATA abd = new APPBARDATA();\n            abd.cbSize = Marshal.SizeOf(abd);\n            abd.hWnd = new WindowInteropHelper(appbarWindow).Handle;\n\n            if (edge == ABEdge.None)\n            {\n                if (info.IsRegistered)\n                {\n                    SHAppBarMessage((int)ABMsg.ABM_REMOVE, ref abd);\n                    info.IsRegistered = false;\n                }\n                RestoreWindow(appbarWindow);\n                return;\n            }\n\n            if (!info.IsRegistered)\n            {\n                info.IsRegistered = true;\n                info.CallbackId = RegisterWindowMessage("AppBarMessage");\n                abd.uCallbackMessage = info.CallbackId;\n\n                uint ret = SHAppBarMessage((int)ABMsg.ABM_NEW, ref abd);\n\n                HwndSource source = HwndSource.FromHwnd(abd.hWnd);\n                source.AddHook(new HwndSourceHook(info.WndProc));\n            }\n\n            appbarWindow.WindowStyle = WindowStyle.None;\n            appbarWindow.ResizeMode = ResizeMode.NoResize;\n            appbarWindow.Topmost = true;\n\n            ABSetPos(info.Edge, appbarWindow);\n        }\n\n        private delegate void ResizeDelegate(Window appbarWindow, Rect rect);\n        private static void DoResize(Window appbarWindow, Rect rect)\n        {\n            appbarWindow.Width = rect.Width;\n            appbarWindow.Height = rect.Height;\n            appbarWindow.Top = rect.Top;\n            appbarWindow.Left = rect.Left;\n        }\n\n        private static void GetActualScreenData(ABEdge edge, Window appbarWindow, ref int leftOffset, ref int topOffset, ref int actualScreenWidth, ref int actualScreenHeight)\n        {\n            IntPtr handle = new WindowInteropHelper(appbarWindow).Handle;\n            IntPtr monitorHandle = MonitorFromWindow(handle, MONITOR_DEFAULTTONEAREST);\n\n            MONITORINFO mi = new MONITORINFO();\n            mi.cbSize = Marshal.SizeOf(mi);\n\n            if (GetMonitorInfo(monitorHandle, ref mi))\n            {\n                if (mi.dwFlags == MONITORINFOF_PRIMARY)\n                {\n                    return;\n                }\n                leftOffset = mi.rcWork.left;\n                topOffset = mi.rcWork.top;\n                actualScreenWidth = mi.rcWork.right - leftOffset;\n                actualScreenHeight = mi.rcWork.bottom - mi.rcWork.top;\n            }\n        }\n\n        private static void ABSetPos(ABEdge edge, Window appbarWindow)\n        {\n            APPBARDATA barData = new APPBARDATA();\n            barData.cbSize = Marshal.SizeOf(barData);\n            barData.hWnd = new WindowInteropHelper(appbarWindow).Handle;\n            barData.uEdge = (int)edge;\n\n            int leftOffset = 0;\n            int topOffset = 0;\n            int actualScreenWidth = (int)SystemParameters.PrimaryScreenWidth;\n            int actualScreenHeight = (int)SystemParameters.PrimaryScreenHeight;\n\n            GetActualScreenData(edge, appbarWindow, ref leftOffset, ref topOffset, ref actualScreenWidth, ref actualScreenHeight);\n\n            if (barData.uEdge == (int)ABEdge.Left || barData.uEdge == (int)ABEdge.Right)\n            {\n                barData.rc.top = topOffset;\n                barData.rc.bottom = actualScreenHeight;\n                if (barData.uEdge == (int)ABEdge.Left)\n                {\n                    barData.rc.left = leftOffset;\n                    barData.rc.right = (int)Math.Round(appbarWindow.ActualWidth) + leftOffset;\n                }\n                else\n                {\n                    barData.rc.right = actualScreenWidth + leftOffset;\n                    barData.rc.left = barData.rc.right - (int)Math.Round(appbarWindow.ActualWidth);\n                }\n            }\n            else\n            {\n                barData.rc.left = leftOffset;\n                barData.rc.right = actualScreenWidth + leftOffset;\n                if (barData.uEdge == (int)ABEdge.Top)\n                {\n                    barData.rc.top = topOffset;\n                    barData.rc.bottom = (int)Math.Round(appbarWindow.ActualHeight) + topOffset;\n                }\n                else\n                {\n                    barData.rc.bottom = actualScreenHeight + topOffset;\n                    barData.rc.top = barData.rc.bottom - (int)Math.Round(appbarWindow.ActualHeight);\n                }\n            }\n\n            SHAppBarMessage((int)ABMsg.ABM_QUERYPOS, ref barData);\n            SHAppBarMessage((int)ABMsg.ABM_SETPOS, ref barData);\n\n            Rect rect = new Rect((double)barData.rc.left, (double)barData.rc.top,\n                (double)(barData.rc.right - barData.rc.left), (double)(barData.rc.bottom - barData.rc.top));\n            //This is done async, because WPF will send a resize after a new appbar is added.  \n            //if we size right away, WPFs resize comes last and overrides us.\n            appbarWindow.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,\n                new ResizeDelegate(DoResize), appbarWindow, rect);\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 33272341, 'author': 'Hugh', 'author_id': 5347352, 'author_profile': 'https://Stackoverflow.com/users/5347352', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve spent some weeks exploring this challenge and finally created a very solid NuGet package delivering this functionality in very friendly way. Simply create a new WPF app then change the main window\'s class from Window to DockWindow (in the XAML) and that\'s it!</p>\n\n<p>Get the package <a href="https://www.nuget.org/packages/Canyonix.UI.Windows/1.0.1" rel="nofollow">here</a> and see the Git repo for a demonstration app.</p>\n'}, {'answer_id': 43024177, 'author': 'Mitch', 'author_id': 138200, 'author_profile': 'https://Stackoverflow.com/users/138200', 'pm_score': 3, 'selected': False, 'text': '<p>There is an excellent MSDN article from 1996 which is entertainingly up to date: <a href="http://web.archive.org/web/20170704171718/https://www.microsoft.com/msj/archive/S274.aspx" rel="nofollow noreferrer">Extend the Windows 95 Shell with Application Desktop Toolbars</a>.  Following its guidance produces an WPF based appbar which handles a number of scenarios that the other answers on this page do not:</p>\n\n<ul>\n<li>Allow dock to any side of the screen</li>\n<li>Allow dock to a particular monitor</li>\n<li>Allow resizing of the appbar (if desired)</li>\n<li>Handle screen layout changes and monitor disconnections</li>\n<li>Handle <kbd>Win</kbd> + <kbd>Shift</kbd> + <kbd>Left</kbd> and attempts to minimize or move the window</li>\n<li>Handle co-operation with other appbars (OneNote et al.)</li>\n<li>Handle per-monitor DPI scaling</li>\n</ul>\n\n<p>I have both a <a href="https://github.com/mgaffigan/WpfAppBar" rel="nofollow noreferrer">demo app and the implementation of <code>AppBarWindow</code> on GitHub</a>.</p>\n\n<p>Example use:</p>\n\n<pre><code>&lt;apb:AppBarWindow x:Class="WpfAppBarDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"\n    xmlns:apb="clr-namespace:WpfAppBar;assembly=WpfAppBar"\n    DataContext="{Binding RelativeSource={RelativeSource Self}}" Title="MainWindow" \n    DockedWidthOrHeight="200" MinHeight="100" MinWidth="100"&gt;\n    &lt;Grid&gt;\n        &lt;Button x:Name="btClose" Content="Close" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="23" Margin="10,10,0,0" Click="btClose_Click"/&gt;\n        &lt;ComboBox x:Name="cbMonitor" SelectedItem="{Binding Path=Monitor, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="10,38,0,0"/&gt;\n        &lt;ComboBox x:Name="cbEdge" SelectedItem="{Binding Path=DockMode, Mode=TwoWay}" HorizontalAlignment="Left" Margin="10,65,0,0" VerticalAlignment="Top" Width="120"/&gt;\n\n        &lt;Thumb Width="5" HorizontalAlignment="Right" Background="Gray" x:Name="rzThumb" Cursor="SizeWE" DragCompleted="rzThumb_DragCompleted" /&gt;\n    &lt;/Grid&gt;\n&lt;/apb:AppBarWindow&gt;\n</code></pre>\n\n<p>Codebehind:</p>\n\n<pre><code>public partial class MainWindow\n{\n    public MainWindow()\n    {\n        InitializeComponent();\n\n        this.cbEdge.ItemsSource = new[]\n        {\n            AppBarDockMode.Left,\n            AppBarDockMode.Right,\n            AppBarDockMode.Top,\n            AppBarDockMode.Bottom\n        };\n        this.cbMonitor.ItemsSource = MonitorInfo.GetAllMonitors();\n    }\n\n    private void btClose_Click(object sender, RoutedEventArgs e)\n    {\n        Close();\n    }\n\n    private void rzThumb_DragCompleted(object sender, DragCompletedEventArgs e)\n    {\n        this.DockedWidthOrHeight += (int)(e.HorizontalChange / VisualTreeHelper.GetDpi(this).PixelsPerDip);\n    }\n}\n</code></pre>\n\n<p>Changing docked position:</p>\n\n<blockquote>\n  <p><a href="https://i.stack.imgur.com/f13P8.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f13P8.gif" alt="AppBar docked to edges"></a></p>\n</blockquote>\n\n<p>Resizing with thumb:</p>\n\n<blockquote>\n  <p><a href="https://i.stack.imgur.com/ifgn8.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ifgn8.gif" alt="Resize"></a></p>\n</blockquote>\n\n<p>Cooperation with other appbars:</p>\n\n<blockquote>\n  <p><a href="https://i.stack.imgur.com/PiydR.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PiydR.gif" alt="Coordination"></a></p>\n</blockquote>\n\n<p>Clone <a href="https://github.com/mgaffigan/WpfAppBar" rel="nofollow noreferrer">from GitHub</a> if you want to use it.  The library itself is only three files, and can easily be dropped in a project.</p>\n'}]
['https://Stackoverflow.com/questions/75785', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/7301/']