dockpanel.cs

来自「全功能c#编译器」· CS 代码 · 共 1,212 行 · 第 1/3 页

CS
1,212
字号
// *****************************************************************************
// 
//  Copyright 2004, Weifen Luo
//  All rights reserved. The software and associated documentation 
//  supplied hereunder are the proprietary information of Weifen Luo
//  and are supplied subject to licence terms.
// 
//  WinFormsUI Library Version 1.0
// *****************************************************************************

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

namespace WeifenLuo.WinFormsUI
{
	public delegate DockContent DeserializeDockContent(string persistString);

	[Designer(typeof(System.Windows.Forms.Design.ControlDesigner))]
	public class DockPanel : Panel
	{
		const int WM_REFRESHACTIVEWINDOW = (int)Win32.Msgs.WM_USER + 1;

		private static IFloatWindowFactory DefaultFloatWindowFactory;
		private static IDockPaneFactory DefaultDockPaneFactory;
		private static StringFormat StringFormatTabHorizontal;
		private static StringFormat StringFormatTabVertical;
		private static Matrix MatrixIdentity;
		private static DockState[] AutoHideDockStates;

		private LocalWindowsHook m_localWindowsHook;

		static DockPanel()
		{
			DefaultFloatWindowFactory = new DefaultFloatWindowFactory();
			DefaultDockPaneFactory = new DefaultDockPaneFactory();

			StringFormatTabHorizontal = new StringFormat();
			StringFormatTabHorizontal.Alignment = StringAlignment.Near;
			StringFormatTabHorizontal.LineAlignment = StringAlignment.Center;
			StringFormatTabHorizontal.FormatFlags = StringFormatFlags.NoWrap;

			StringFormatTabVertical = new StringFormat();
			StringFormatTabVertical.Alignment = StringAlignment.Near;
			StringFormatTabVertical.LineAlignment = StringAlignment.Center;
			StringFormatTabVertical.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.DirectionVertical;

			MatrixIdentity = new Matrix();

			AutoHideDockStates = new DockState[4];
			AutoHideDockStates[0] = DockState.DockLeftAutoHide;
			AutoHideDockStates[1] = DockState.DockRightAutoHide;
			AutoHideDockStates[2] = DockState.DockTopAutoHide;
			AutoHideDockStates[3] = DockState.DockBottomAutoHide;
		}

		public DockPanel()
		{
			m_dragHandler = new DragHandler(this);
			m_panes = new DockPaneCollection();
			m_floatWindows = new FloatWindowCollection();

			SetStyle(ControlStyles.ResizeRedraw |
				ControlStyles.UserPaint |
				ControlStyles.AllPaintingInWmPaint |
				ControlStyles.DoubleBuffer, true);

            SuspendLayout();
            Font = SystemInformation.MenuFont;

			m_autoHideWindow = new AutoHideWindow(this);
			m_autoHideWindow.Visible = false;

			m_dummyControl = new DummyControl();
			m_dummyControl.Bounds = Rectangle.Empty;

			m_dockWindows = new DockWindowCollection(this);
			Controls.AddRange(new Control[]	{
				m_autoHideWindow,
				m_dummyControl,
				DockWindows[DockState.Document],
				DockWindows[DockState.DockLeft],
				DockWindows[DockState.DockRight],
				DockWindows[DockState.DockTop],
				DockWindows[DockState.DockBottom]
				});

			m_localWindowsHook = new LocalWindowsHook(HookType.WH_CALLWNDPROCRET);
			m_localWindowsHook.HookInvoked += new LocalWindowsHook.HookEventHandler(this.HookEventHandler);
			m_localWindowsHook.Install();
            ResumeLayout();
        }

		// Windows hook event handler
		private void HookEventHandler(object sender, HookEventArgs e)
		{
			Win32.CWPRETSTRUCT cwpret = (Win32.CWPRETSTRUCT)Marshal.PtrToStructure(e.lParam, typeof(Win32.CWPRETSTRUCT));
			int msg = cwpret.message;

			if (msg == (int)Win32.Msgs.WM_KILLFOCUS)
			{
				DockPane pane = GetPaneFromHandle((IntPtr)cwpret.wParam);
				if (pane == null)
					User32.PostMessage(this.Handle, WM_REFRESHACTIVEWINDOW, 0, 0);
			}
			else if (msg == (int)Win32.Msgs.WM_SETFOCUS)
				User32.PostMessage(this.Handle, WM_REFRESHACTIVEWINDOW, 0, 0);
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				m_localWindowsHook.Uninstall();
				FloatWindows.Dispose();
				Panes.Dispose();
			}
			base.Dispose(disposing);
		}

		[Browsable(false)]
		public DockContent ActiveAutoHideContent
		{
			get	{	return AutoHideWindow.ActiveContent;	}
			set	{	AutoHideWindow.ActiveContent = value;	}
		}

		private DockContent m_activeContent = null;
		[Browsable(false)]
		public DockContent ActiveContent
		{
			get	{	return m_activeContent;	}
		}
		internal void SetActiveContent()
		{
			DockContent value = ActivePane == null ? null : ActivePane.ActiveContent;

			if (m_activeContent == value)
				return;

			if (m_activeContent != null)
				m_activeContent.SetIsActivated(false);

			m_activeContent = value;

			if (m_activeContent != null)
				m_activeContent.SetIsActivated(true);

			OnActiveContentChanged(EventArgs.Empty);
		}

		private DockPane m_activePane = null;
		[Browsable(false)]
		public DockPane ActivePane
		{
			get	{	return m_activePane;	}
		}
		private void SetActivePane()
		{
			DockPane value = GetPaneFromHandle(User32.GetFocus());
			if (m_activePane == value)
				return;

			if (m_activePane != null)
				m_activePane.SetIsActivated(false);

			m_activePane = value;

			if (m_activePane != null)
				m_activePane.SetIsActivated(true);
		}
		private DockPane GetPaneFromHandle(IntPtr hWnd)
		{
			Control control = Control.FromChildHandle(hWnd);
			DockPane pane = null;
			for (; control != null; control = control.Parent)
			{
				pane = control as DockPane;
				if (pane != null)
					break;
			}

			if (pane == null)
				return null;
			else if (pane.DockPanel != this)
				return null;
			else
				return pane;
		}

		private DockContent m_activeDocument = null;
		[Browsable(false)]
		public DockContent ActiveDocument
		{
			get	{	return m_activeDocument;	}
		}
		private void SetActiveDocument()
		{
			DockContent value = ActiveDocumentPane == null ? null : ActiveDocumentPane.ActiveContent;

			if (m_activeDocument == value)
				return;

			m_activeDocument = value;
			if (m_activeDocument != null)
				if (m_activeDocument.HiddenMdiChild != null)
					m_activeDocument.HiddenMdiChild.Activate();

			OnActiveDocumentChanged(EventArgs.Empty);
		}

		private DockPane m_activeDocumentPane = null;
		[Browsable(false)]
		public DockPane ActiveDocumentPane
		{
			get	{	return m_activeDocumentPane;	}
		}
		private void SetActiveDocumentPane()
		{
			DockPane value = null;

			if (ActivePane != null && ActivePane.DockState == DockState.Document)
				value = ActivePane;

			if (value == null)
			{
				if (ActiveDocumentPane == null)
					value = DockWindows[DockState.Document].DefaultPane;
				else if (ActiveDocumentPane.DockPanel != this || ActiveDocumentPane.DockState != DockState.Document)
					value = DockWindows[DockState.Document].DefaultPane;
				else
					value = m_activeDocumentPane;
			}

			if (m_activeDocumentPane == value)
				return;

			if (m_activeDocumentPane != null)
				m_activeDocumentPane.SetIsActiveDocumentPane(false);

			m_activeDocumentPane = value;

			if (m_activeDocumentPane != null)
				m_activeDocumentPane.SetIsActiveDocumentPane(true);
		}

		private bool m_allowRedocking = true;
		[LocalizedCategory("Category.Docking")]
		[LocalizedDescription("DockPanel.AllowRedocking.Description")]
		[DefaultValue(true)]
		public bool AllowRedocking
		{
			get	{	return m_allowRedocking;	}
			set	{	m_allowRedocking = value;	}
		}

		private AutoHideWindow m_autoHideWindow;
		internal AutoHideWindow AutoHideWindow
		{
			get	{	return m_autoHideWindow;	}
		}

		private DockContentCollection m_contents = new DockContentCollection();
		[Browsable(false)]
		public DockContentCollection Contents
		{
			get	{	return m_contents;	}
		}

		protected internal virtual IDockPaneFactory DockPaneFactory
		{
			get	{	return DefaultDockPaneFactory;	}
		}

		private DockPaneCollection m_panes;
		[Browsable(false)]
		public DockPaneCollection Panes
		{
			get	{	return m_panes;	}
		}

		internal Rectangle DockArea
		{
			get
			{
				return new Rectangle(DockPadding.Left, DockPadding.Top,
					ClientRectangle.Width - DockPadding.Left - DockPadding.Right,
					ClientRectangle.Height - DockPadding.Top - DockPadding.Bottom);
			}
		}

		private double m_dockBottomPortion = 0.25;
		[LocalizedCategory("Category.Docking")]
		[LocalizedDescription("DockPanel.DockBottomPortion.Description")]
		[DefaultValue(0.25)]
		public double DockBottomPortion
		{
			get	{	return m_dockBottomPortion;	}
			set
			{
				if (value <= 0 || value >= 1)
					throw new ArgumentOutOfRangeException();

				if (value == m_dockBottomPortion)
					return;

				m_dockBottomPortion = value;

				if (m_dockTopPortion + m_dockBottomPortion > 1)
					m_dockTopPortion = 1 - m_dockBottomPortion;

				PerformLayout();
			}
		}

		private double m_dockLeftPortion = 0.25;
		[LocalizedCategory("Category.Docking")]
		[LocalizedDescription("DockPanel.DockLeftPortion.Description")]
		[DefaultValue(0.25)]
		public double DockLeftPortion
		{
			get	{	return m_dockLeftPortion;	}
			set
			{
				if (value <= 0 || value >= 1)
					throw new ArgumentOutOfRangeException();

				if (value == m_dockLeftPortion)
					return;

				m_dockLeftPortion = value;

				if (m_dockLeftPortion + m_dockRightPortion > 1)
					m_dockRightPortion = 1 - m_dockLeftPortion;
				
				PerformLayout();
			}
		}

		private double m_dockRightPortion = 0.25;
		[LocalizedCategory("Category.Docking")]
		[LocalizedDescription("DockPanel.DockRightPortion.Description")]
		[DefaultValue(0.25)]
		public double DockRightPortion
		{
			get	{	return m_dockRightPortion;	}
			set
			{
				if (value <= 0 || value >= 1)
					throw new ArgumentOutOfRangeException();

				if (value == m_dockRightPortion)
					return;

				m_dockRightPortion = value;

				if (m_dockLeftPortion + m_dockRightPortion > 1)
					m_dockLeftPortion = 1 - m_dockRightPortion;

				PerformLayout();
			}
		}

		private double m_dockTopPortion = 0.25;
		[LocalizedCategory("Category.Docking")]
		[LocalizedDescription("DockPanel.DockTopPortion.Description")]
		[DefaultValue(0.25)]
		public double DockTopPortion
		{
			get	{	return m_dockTopPortion;	}
			set
			{
				if (value <= 0 || value >= 1)
					throw new ArgumentOutOfRangeException();

				if (value == m_dockTopPortion)
					return;

				m_dockTopPortion = value;

				if (m_dockTopPortion + m_dockBottomPortion > 1)
					m_dockBottomPortion = 1 - m_dockTopPortion;

				PerformLayout();
			}
		}

		private DockWindowCollection m_dockWindows;
		public DockWindowCollection DockWindows
		{
			get	{	return m_dockWindows;	}
		}

		[Browsable(false)]
		public DockContent[] Documents
		{
			get	{	return Contents.Select(DockAreas.Document);	}
		}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?