Click here to Skip to main content
15,885,244 members
Articles / Desktop Programming / WPF

WPF Docking Library

Rate me:
Please Sign up or sign in to vote.
4.78/5 (85 votes)
17 Jul 2007CPOL3 min read 1M   24.4K   317  
A WPF library to easily integrate Windows docking features in applications like VS
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Windows.Controls.Primitives;

namespace DockingLibrary
{
    /// <summary>
    /// Interaction logic for FloatingWindow.xaml
    /// </summary>

    public partial class FloatingWindow : System.Windows.Window
    {
        private const int WM_MOVE = 0x0003;
        private const int WM_SIZE = 0x0005;
        private const int WM_NCMOUSEMOVE = 0xa0;
        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int WM_NCLBUTTONUP = 0xA2;
        private const int WM_NCLBUTTONDBLCLK = 0xA3;
        private const int WM_NCRBUTTONDOWN = 0xA4;
        private const int WM_NCRBUTTONUP = 0xA5;
        private const int HTCAPTION = 2;
        private const int SC_MOVE = 0xF010;
        private const int WM_SYSCOMMAND = 0x0112;

        internal readonly FloatingWindowHostedPane HostedPane;
        //public readonly DockablePane ReferencedPane;

        public FloatingWindow(DockablePane pane)
        {
            InitializeComponent();

            #region Hosted Pane
            HostedPane = new FloatingWindowHostedPane(this, pane);
            HostedPane.ReferencedPane.OnTitleChanged += new EventHandler(HostedPane_OnTitleChanged);
            Content = HostedPane;
            Title = HostedPane.Title;
            #endregion
        }

        void HostedPane_OnTitleChanged(object sender, EventArgs e)
        {
            Title = HostedPane.Title;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            HostedPane.ReferencedPane.SaveFloatingWindowSizeAndPosition(this);
            HostedPane.ReferencedPane.OnTitleChanged -= new EventHandler(HostedPane_OnTitleChanged);
            HostedPane.Close();
            
            if (_hwndSource != null)
                _hwndSource.RemoveHook(_wndProcHandler);

            base.OnClosing(e);
        }

        HwndSource _hwndSource;
        HwndSourceHook _wndProcHandler;

        protected void OnLoaded(object sender, EventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);
            _hwndSource = HwndSource.FromHwnd(helper.Handle);
            _wndProcHandler = new HwndSourceHook(HookHandler);
            _hwndSource.AddHook(_wndProcHandler);
        }
        

        private IntPtr HookHandler(
            IntPtr hwnd,
            int msg,
            IntPtr wParam,
            IntPtr lParam,
            ref bool handled
        )
        {
            handled = false;

            switch (msg)
            {
                case WM_SIZE:
                case WM_MOVE:
                    HostedPane.ReferencedPane.SaveFloatingWindowSizeAndPosition(this);
                    break;
                case WM_NCLBUTTONDOWN:
                    if (HostedPane.ReferencedPane.State == PaneState.DockableWindow && wParam.ToInt32() == HTCAPTION)
                    {
                        short x = (short)((lParam.ToInt32() & 0xFFFF));
                        short y = (short)((lParam.ToInt32() >> 16));


                        HostedPane.ReferencedPane.DockManager.Drag(this, new Point(x, y), new Point(x - Left, y - Top));

                        handled = true;
                    }
                    break;
                case WM_NCLBUTTONDBLCLK:
                    if (HostedPane.ReferencedPane.State == PaneState.DockableWindow && wParam.ToInt32() == HTCAPTION)
                    {
                        //
                        HostedPane.ReferencedPane.ChangeState(PaneState.Docked);
                        HostedPane.ReferencedPane.Show();
                        this.Close();

                        handled = true;
                    }
                    break;
                case WM_NCRBUTTONDOWN:
                    if (wParam.ToInt32() == HTCAPTION)
                    {
                        short x = (short)((lParam.ToInt32() & 0xFFFF));
                        short y = (short)((lParam.ToInt32() >> 16));

                        ContextMenu cxMenu = HostedPane.OptionsMenu;
                        cxMenu.Placement = PlacementMode.AbsolutePoint;
                        cxMenu.PlacementRectangle = new Rect(new Point(x, y), new Size(0, 0));
                        cxMenu.PlacementTarget = this;
                        cxMenu.IsOpen = true;

                        handled = true;
                    }
                    break;
                case WM_NCRBUTTONUP:
                    if (wParam.ToInt32() == HTCAPTION)
                    {

                        handled = true;
                    }
                    break;
                
            }
            

            return IntPtr.Zero;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
Italy Italy
I bought my first computer in Nov 1991, 21st and I started programming with QBasic under MSDOS.
Today my main interest is developping applications with .NET and HTML5 stack.

Comments and Discussions