Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

WPF Docking Library

, 17 Jul 2007
A WPF library to easily integrate Windows docking features in applications like VS
source.zip
DockingLibrary
DockingLibrary
bin
Debug
Release
ClassDiagram1.cd
icons
DockBottom.PNG
DockLeft.PNG
DockPane.PNG
DockRight.PNG
DockTop.PNG
Ritaglio.shs
Thumbs.db
images
DockBottom.PNG
DockLeft.PNG
DockPane.PNG
DockRight.PNG
DockTop.PNG
obj
Debug
Refactor
TempPE
themes
Release
Refactor
TempPE
themes
Properties
Settings.settings
themes
DockingLibraryDemo
bin
Debug
Release
Icons
props.ico
view_remove.ico
view_tree.ico
obj
Debug
TempPE
Release
TempPE
Properties
Settings.settings
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)

Share

About the Author

adospace.NET
Systems Engineer
Italy Italy
I bought my first computer in Nov 1991, 21th and I started programming with QBasic under MSDOS.
Today my main interest is developping application with .NET 3.5.
Visit YouDev.net for tutorials on AvalonDock, download WPF controls, samples and code.

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 17 Jul 2007
Article Copyright 2007 by adospace.NET
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid