www.pudn.com > CustomFormsDesigner.rar > MainWindow.cs, change:2005-07-22,size:8436b


using System; 
using System.Collections; 
using System.ComponentModel; 
using System.ComponentModel.Design; 
using System.ComponentModel.Design.Serialization; 
using System.Drawing; 
using System.Drawing.Design; 
using System.Windows.Forms; 
using System.Windows.Forms.Design; 
using FormDesigner.impls; 
using System.Configuration; 
 
namespace FormDesigner 
{ 
	/// <summary> 
	/// Summary description for Form1. 
	/// </summary> 
	public class MainWindow : System.Windows.Forms.Form 
	{ 
		private DesignerHostImpl host ; 
		private IServiceContainer serviceContainer; 
		private System.Windows.Forms.Panel designSurfacePanel; 
		private System.Windows.Forms.PropertyGrid propertyGrid; 
		private FormDesigner.impls.ToolboxServiceImpl toolboxServiceImpl; 
		private System.Windows.Forms.Splitter splitter2; 
		private System.Windows.Forms.MainMenu mainMenu1; 
		private System.Windows.Forms.MenuItem menuItem1; 
		private System.Windows.Forms.MenuItem menuItem2; 
		private System.Windows.Forms.MenuItem menuEditDelete; 
		/// <summary> 
		/// Required designer variable. 
		/// </summary> 
		private System.ComponentModel.Container components = null; 
 
		public MainWindow() 
		{ 
			// 
			// Required for Windows Form Designer support 
			// 
			InitializeComponent(); 
			InitWindow(); 
		} 
 
		/// <summary> 
		/// Clean up any resources being used. 
		/// </summary> 
		protected override void Dispose( bool disposing ) 
		{ 
			if( disposing ) 
			{ 
				if (components != null)  
				{ 
					components.Dispose(); 
				} 
			} 
			base.Dispose( disposing ); 
		} 
 
		#region Windows Form Designer generated code 
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor. 
		/// </summary> 
		private void InitializeComponent() 
		{ 
			this.toolboxServiceImpl = new FormDesigner.impls.ToolboxServiceImpl(); 
			this.designSurfacePanel = new System.Windows.Forms.Panel(); 
			this.splitter2 = new System.Windows.Forms.Splitter(); 
			this.propertyGrid = new System.Windows.Forms.PropertyGrid(); 
			this.mainMenu1 = new System.Windows.Forms.MainMenu(); 
			this.menuItem1 = new System.Windows.Forms.MenuItem(); 
			this.menuItem2 = new System.Windows.Forms.MenuItem(); 
			this.menuEditDelete = new System.Windows.Forms.MenuItem(); 
			this.SuspendLayout(); 
			//  
			// toolboxServiceImpl 
			//  
			this.toolboxServiceImpl.Dock = System.Windows.Forms.DockStyle.Left; 
			this.toolboxServiceImpl.Location = new System.Drawing.Point(0, 0); 
			this.toolboxServiceImpl.Name = "toolboxServiceImpl"; 
			this.toolboxServiceImpl.SelectedCategory = null; 
			this.toolboxServiceImpl.Size = new System.Drawing.Size(250, 470); 
			this.toolboxServiceImpl.TabIndex = 0; 
			//  
			// designSurfacePanel 
			//  
			this.designSurfacePanel.BackColor = System.Drawing.SystemColors.Window; 
			this.designSurfacePanel.Dock = System.Windows.Forms.DockStyle.Fill; 
			this.designSurfacePanel.Location = new System.Drawing.Point(250, 0); 
			this.designSurfacePanel.Name = "designSurfacePanel"; 
			this.designSurfacePanel.Size = new System.Drawing.Size(537, 470); 
			this.designSurfacePanel.TabIndex = 1; 
			//  
			// splitter2 
			//  
			this.splitter2.Location = new System.Drawing.Point(0, 0); 
			this.splitter2.Name = "splitter2"; 
			this.splitter2.TabIndex = 0; 
			this.splitter2.TabStop = false; 
			//  
			// propertyGrid 
			//  
			this.propertyGrid.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192))); 
			this.propertyGrid.CommandsBackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192))); 
			this.propertyGrid.CommandsVisibleIfAvailable = true; 
			this.propertyGrid.Dock = System.Windows.Forms.DockStyle.Right; 
			this.propertyGrid.LargeButtons = false; 
			this.propertyGrid.LineColor = System.Drawing.SystemColors.ScrollBar; 
			this.propertyGrid.Location = new System.Drawing.Point(557, 0); 
			this.propertyGrid.Name = "propertyGrid"; 
			this.propertyGrid.Size = new System.Drawing.Size(230, 470); 
			this.propertyGrid.TabIndex = 2; 
			this.propertyGrid.Text = "propertyGrid1"; 
			this.propertyGrid.ViewBackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(255)), ((System.Byte)(192))); 
			this.propertyGrid.ViewForeColor = System.Drawing.SystemColors.WindowText; 
			//  
			// mainMenu1 
			//  
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					  this.menuItem1, 
																					  this.menuItem2}); 
			//  
			// menuItem1 
			//  
			this.menuItem1.Index = 0; 
			this.menuItem1.Text = "File"; 
			//  
			// menuItem2 
			//  
			this.menuItem2.Index = 1; 
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { 
																					  this.menuEditDelete}); 
			this.menuItem2.Text = "Edit"; 
			//  
			// menuEditDelete 
			//  
			this.menuEditDelete.Index = 0; 
			this.menuEditDelete.Shortcut = System.Windows.Forms.Shortcut.Del; 
			this.menuEditDelete.Text = "&Delete"; 
			this.menuEditDelete.Click += new System.EventHandler(this.menuEditDelete_Click); 
			//  
			// MainWindow 
			//  
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 
			this.ClientSize = new System.Drawing.Size(787, 470); 
			this.Controls.Add(this.propertyGrid); 
			this.Controls.Add(this.designSurfacePanel); 
			this.Controls.Add(this.toolboxServiceImpl); 
			this.Menu = this.mainMenu1; 
			this.Name = "MainWindow"; 
			this.Text = "MainWindow"; 
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized; 
			this.ResumeLayout(false); 
 
		} 
		#endregion 
 
		/// <summary> 
		/// The main entry point for the application. 
		/// </summary> 
		[STAThread] 
		static void Main()  
		{ 
			Application.Run(new MainWindow()); 
		} 
 
		#region private methods 
 
		private void InitWindow() 
		{ 
			serviceContainer = new ServiceContainer(); 
			// create host 
			host = new DesignerHostImpl(serviceContainer); 
			// 
			AddBaseServices(); 
			// 
			Form form = (Form)host.CreateComponent(typeof(Form),null); 
			form.TopLevel = false; 
			form.Text = "Design Surface"; 
			IRootDesigner rootDesigner = (IRootDesigner)host.GetDesigner(form); 
			Control designView = (Control)rootDesigner.GetView(ViewTechnology.WindowsForms); 
			designView.Dock = DockStyle.Fill; 
			designSurfacePanel.Controls.Add(designView); 
 
			// we need to subscribe to selection changed events so  
			// that we can update our properties grid 
 
			ISelectionService selectionService = host.GetService(typeof(ISelectionService)) as ISelectionService; 
			selectionService.SelectionChanged +=new EventHandler(selectionService_SelectionChanged); 
			// activate the host 
			host.Activate(); 
		} 
		private void AddBaseServices() 
		{ 
			FDToolbox tools = ConfigurationSettings.GetConfig("FDToolbox") as FDToolbox; 
			toolboxServiceImpl.LoadToolbox(tools); 
			// add service 
			serviceContainer.AddService(typeof(IToolboxService),toolboxServiceImpl); 
		} 
 
		#endregion 
 
		protected override object GetService(Type service) 
		{ 
			if(serviceContainer!=null && serviceContainer.GetService(service)!=null) 
			{ 
				return serviceContainer.GetService(service); 
			} 
			return host.GetService(service); 
		} 
 
		private void menuEditDelete_Click(object sender, System.EventArgs e) 
		{ 
			IMenuCommandService menuCommandService = GetService(typeof(IMenuCommandService)) as IMenuCommandService; 
			menuCommandService.GlobalInvoke(StandardCommands.Delete); 
		} 
 
		private void selectionService_SelectionChanged(object sender, EventArgs e) 
		{ 
			ISelectionService selectionService = host.GetService(typeof(ISelectionService)) as ISelectionService; 
 
			if (propertyGrid != null && selectionService!=null) 
			{ 
				ICollection selectedComponents = selectionService.GetSelectedComponents(); 
				// if nothing is selected, just select the root component 
				if(selectedComponents==null || selectedComponents.Count==0) 
				{ 
					propertyGrid.SelectedObjects = new object[]{host.RootComponent}; 
				} 
				// we have to copy over the selected components list 
				// into an array and then set the selectedObjects property 
				object[] selections = new Object[selectedComponents.Count]; 
				selectedComponents.CopyTo(selections, 0); 
				propertyGrid.SelectedObjects = selections; 
			} 
		} 
 
	} 
}