⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dockregion.cs

📁 c#精彩编程百例(源代码)
💻 CS
字号:
//  DockRegion.cs
//  Copyright (C) 2001 Mike Krueger
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace SharpDevelop.Gui.Docking {
	
	public class DockContainer : UserControl
	{
		TabControl tabcontrol  = new TabControl();
		public ArrayList    docked    = new ArrayList();
		DockRegion region;
		
		public event EventHandler UndockEvent;
		MouseEventArgs meventargs;
		ContextMenu       formatmenu = null;
		
		
		protected override void OnMouseDown(MouseEventArgs e)
		{
			meventargs = e;
			base.OnMouseDown(e);
		}
	
		protected override void OnClick(EventArgs e)
		{
			base.OnClick(e);
			
			if (meventargs.Button == MouseButtons.Middle && formatmenu != null) {
				formatmenu.Show(this, new Point(meventargs.X, meventargs.Y));
			}
		}
		
		public DockContainer(DockRegion region)
		{
			tabcontrol.Dock = DockStyle.Fill;
			this.region = region;
			formatmenu = new ContextMenu( new MenuItem[] {
				new MenuItem("Undock", new EventHandler(Undock)),
				new MenuItem("TabUp", new EventHandler(TabUp)),
				new MenuItem("TabDown", new EventHandler(TabDown)),
				new MenuItem("TabLeft", new EventHandler(TabLeft)),
				new MenuItem("TabRight", new EventHandler(TabRight)),
	//			new MenuItem("MoveDown", new EventHandler(MoveDown)),
	//			new MenuItem("MoveUp", new EventHandler(MoveUp))
			});
		}
		
		public void AddDockableControl(DockableControl control)
		{
			control.formatmenu = new ContextMenu( new MenuItem[] {
				new MenuItem("Undock", new EventHandler(Undock)),
	//			new MenuItem("MoveDown", new EventHandler(MoveDown)),
	//			new MenuItem("MoveUp", new EventHandler(MoveUp))
			});
		
			switch (docked.Count) {
				case 0:
					control.TabControl = null;
					control.TabPage = null;
					Controls.Add(control);
					break;
				case 1:
					Controls.Clear();
					Controls.Add(tabcontrol);
					DockableControl control2 = (DockableControl)docked[0];
					TabPage page2 = new TabPage(control2.TabName);
					page2.Controls.Add(control2);
					control2.TabControl = tabcontrol;
					control2.TabPage = page2;
					tabcontrol.TabPages.Add(page2);
					goto default;
				default:
					TabPage page = new TabPage(control.TabName);
					page.Controls.Add(control);
					control.TabControl = tabcontrol;
					control.TabPage = page;
					tabcontrol.TabPages.Add(page);
					break;
			}
			docked.Add(control);
		}
		
		void TabUp(object sender, EventArgs e)
		{
			tabcontrol.Alignment  = TabAlignment.Top;
		}
		void TabDown(object sender, EventArgs e)
		{
			tabcontrol.Alignment  = TabAlignment.Bottom;
		}
		void TabLeft(object sender, EventArgs e)
		{
			tabcontrol.Alignment  = TabAlignment.Left;
		}
		void TabRight(object sender, EventArgs e)
		{
			tabcontrol.Alignment  = TabAlignment.Right;
		}
		
	//	void MoveDown(object sender, EventArgs e)
	//	{
	//		
	//	}
	//	
	//	void MoveUp(object sender, EventArgs e)
	//	{
	//		
	//	}
		
		void Undock(object sender, EventArgs e)
		{
			DockableControl dc;
			if (docked.Count > 1) {
				dc = (DockableControl)tabcontrol.SelectedTab.Controls[0];
				tabcontrol.TabPages.Remove(tabcontrol.SelectedTab);
			} else {
				dc = (DockableControl)Controls[0];
			}
			
			dc.formatmenu = null;
			new FloatingWindow(region.manager, dc).Show();
			docked.Remove(dc);
			
			if (docked.Count == 1) {
				Controls.Clear();
				Controls.Add((DockableControl)docked[0]);
				((DockableControl)docked[0]).TabPage = null;
				tabcontrol.TabPages.Clear();
			}
			
			if (docked.Count == 0) {
				Visible = false;
				Dock = DockStyle.None;
			}
			if (UndockEvent != null)
				UndockEvent(this, null);
		}
		
	}
	
	public class DockRegion : UserControl
	{
		public DockManager  manager;
		Splitter     splitter      = new Splitter();
		DockStyle    mydockstyle   = DockStyle.None;
		ArrayList    dockcontainer = new ArrayList();
		
		
		public DockRegion(DockManager manager)
		{
			this.manager = manager;
			
			
			splitter.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			splitter.Location = new System.Drawing.Point(0, 200);
			splitter.TabIndex = 5;
			splitter.TabStop = false;
			splitter.Size = new System.Drawing.Size(3, 273);
			
			Visible = false;
			splitter.Visible = false;
			
			manager.parent.Controls.Add(splitter);
			manager.parent.Controls.Add(this);
		}
		
		void Undock(object sender, EventArgs e)
		{
			ArrayList remove = new ArrayList();		
			for (int i = 0; i < dockcontainer.Count; ++i) {
				DockContainer container = (DockContainer)dockcontainer[i];
				if (container.docked.Count == 0) {
					remove.Add(container);
				}
			}
			foreach (object o in remove)
				dockcontainer.Remove(o);
			
			if (dockcontainer.Count == 0) {
				splitter.Visible = false;
				Visible = false;
//				manager.parent.Controls.Remove(this);
			}
		}
		
		void ControlsVisibleChanged(object sender, EventArgs e)
		{
			bool somethingvisible = false;		
			foreach (DockContainer container in dockcontainer)
			foreach (DockableControl control in container.docked)
				somethingvisible |= control.DockState.Visible;
			
			if (!somethingvisible) {
				Visible = false;
				splitter.Visible = false;
			} else {
				if (!Visible) {
					splitter.Visible = true;
					Visible = true;
				}
			}
		}
		

		public void SetDock(DockStyle dock)
		{
			mydockstyle = dock;
		}
	
		public void AddDockableControl(DockableControl control)
		{
			control.DockState.Float = false;
			control.FormContainer = null;
			control.DockedVisibleChanged += new EventHandler(ControlsVisibleChanged);
		
			if (dockcontainer.Count == 0) {
				DockContainer newcontainer = new DockContainer(this);
				dockcontainer.Add(newcontainer);
				Controls.Add(newcontainer);
				newcontainer.UndockEvent += new EventHandler(Undock);
				newcontainer.Dock = DockStyle.Fill;
			}
			
			DockContainer container = (DockContainer)dockcontainer[0];
			container.AddDockableControl(control);
			if (!Visible) {
//				manager.parent.Controls.Add(this);
				splitter.Dock = mydockstyle;
				Dock = mydockstyle;
				splitter.Visible = true;
				Visible = true;
			}
		}
	}
}

⌨️ 快捷键说明

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