using System; using System.Windows.Forms; using System.Reflection; using System.Collections; using System.ComponentModel; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Diagnostics; namespace myControlFactory { #region ControlFactory /// /// Summary description for FormControlFactory. /// public class ControlFactory { public ControlFactory() { // // TODO: Add constructor logic here // } public static Control CreateControl(string ctrlName, string partialName) { try { Control ctrl; switch(ctrlName) { case "Button": ctrl = new Button(); break; case "CheckBox": ctrl = new CheckBox(); break; case "CheckedListBox": ctrl = new CheckedListBox(); break; case "ComboBox": ctrl = new ComboBox(); break; case "DataGridView": ctrl = new DataGridView(); break; case "DateTimePicker": ctrl = new DateTimePicker(); break; case "DomainUpDown": ctrl = new DomainUpDown(); break; case "Form": ctrl = new Form(); break; case "FlowLayoutPanel": ctrl = new FlowLayoutPanel(); break; case "GroupBox": ctrl = new GroupBox(); break; case "HScrollBar": ctrl = new HScrollBar(); break; case "Label": ctrl = new Label(); break; case "LinkLabel": ctrl = new LinkLabel(); break; case "ListBox": ctrl = new ListBox(); break; case "ListView": ctrl = new ListView(); break; case "MaskedTextBox": ctrl = new MaskedTextBox(); break; case "MenuStrip": ctrl = new MenuStrip(); break; case "MonthCalendar": ctrl = new MonthCalendar(); break; case "NumericUpDown": ctrl = new NumericUpDown(); break; case "Panel": ctrl = new Panel(); break; case "PictureBox": ctrl = new PictureBox(); break; case "ProgressBar": ctrl = new ProgressBar(); break; case "RadioButton": ctrl = new RadioButton(); break; case "RichTextBox": ctrl = new RichTextBox(); break; case "SplitContainer": ctrl = new SplitContainer(); break; case "Splitter": ctrl = new Splitter(); break; case "TabControl": ctrl = new TabControl(); break; case "TableLayoutPanel": ctrl = new TableLayoutPanel(); break; case "TextBox": ctrl = new TextBox(); break; case "ToolStrip": ctrl = new ToolStrip(); break; case "TrackBar": ctrl = new TrackBar(); break; case "TreeView": ctrl = new TreeView(); break; case "VScrollBar": ctrl = new VScrollBar(); break; case "WebBrowser": ctrl = new WebBrowser(); break; default: Assembly controlAsm = Assembly.LoadWithPartialName(partialName); // TODO choice //Assembly controlAsm = Assembly.Load(partialName); Type controlType = controlAsm.GetType(partialName + "." + ctrlName); ctrl = (Control)Activator.CreateInstance(controlType); break; } return ctrl; } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("create control failed:" + ex.Message); return new Control(); } } public static Form CloneForm(Form frm) { Form form = new Form(); ArrayList arrayList = new ArrayList(); try { ArrayList arrayList2 = new ArrayList(frm.Controls); arrayList2.Add(frm); CBFormCtrls cBFormCtrls = new CBFormCtrls(arrayList2.ToArray()); IEnumerator enumerator = cBFormCtrls.CtrlItemList.GetEnumerator(); try { while (enumerator.MoveNext()) { CBFormCtrlItem cBFormCtrlItem = (CBFormCtrlItem)enumerator.Current; object obj = ControlFactory.CreateControl(cBFormCtrlItem.CtrlName, cBFormCtrlItem.PartialName); ControlFactory.SetControlProperties(obj, cBFormCtrlItem.PropertyList); if (obj is Form) { form = (obj as Form); ControlFactory.SetControlProperties(frm, cBFormCtrlItem.PropertyList); } else { if (obj is Control) { arrayList.Add(obj as Control); } } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } catch (Exception ex) { Trace.WriteLine(ex.Message); } IEnumerator enumerator2 = arrayList.GetEnumerator(); try { while (enumerator2.MoveNext()) { Control value = (Control)enumerator2.Current; form.Controls.Add(value); } } finally { IDisposable disposable2 = enumerator2 as IDisposable; if (disposable2 != null) { disposable2.Dispose(); } } return form; } public static void SetControlProperties(object ctrl, Hashtable propertyList) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(ctrl); IEnumerator enumerator = properties.GetEnumerator(); try { while (enumerator.MoveNext()) { PropertyDescriptor propertyDescriptor = (PropertyDescriptor)enumerator.Current; if (propertyList.Contains(propertyDescriptor.Name)) { object value = propertyList[propertyDescriptor.Name]; try { propertyDescriptor.SetValue(ctrl, value); } catch (Exception ex) { Trace.WriteLine(ex.Message); } } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } public static object[] GetCtrlsFromClipBoard() { ArrayList arrayList = new ArrayList(); IDataObject dataObject = Clipboard.GetDataObject(); if (dataObject.GetDataPresent(CBFormCtrls.Format.Name)) { CBFormCtrls cBFormCtrls = dataObject.GetData(CBFormCtrls.Format.Name) as CBFormCtrls; IEnumerator enumerator = cBFormCtrls.CtrlItemList.GetEnumerator(); try { while (enumerator.MoveNext()) { CBFormCtrlItem cBFormCtrlItem = (CBFormCtrlItem)enumerator.Current; Control control = ControlFactory.CreateControl(cBFormCtrlItem.CtrlName, cBFormCtrlItem.PartialName) as Control; ControlFactory.SetControlProperties(control, cBFormCtrlItem.PropertyList); arrayList.Add(control); } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } return arrayList.ToArray(); } public static void CopyCtrls2ClipBoard(object[] ctrls) { CBFormCtrls data = new CBFormCtrls(ctrls); IDataObject dataObject = new DataObject(); dataObject.SetData(CBFormCtrls.Format.Name, true, data); Clipboard.SetDataObject(dataObject, true); } public static void SerializeForm2File(Form frm, string fileName) { try { ArrayList arrayList = new ArrayList(frm.Controls); arrayList.Add(frm); CBFormCtrls cBFormCtrls = new CBFormCtrls(arrayList.ToArray()); Stream stream = File.Open(fileName, FileMode.Create); BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(stream, cBFormCtrls); stream.Close(); } catch (Exception ex) { Trace.WriteLine(ex.Message); } } public static void DeserializeFormFromFile(Form frm, string fileName) { Debug.Assert(frm != null); frm.Controls.Clear(); Stream stream = File.Open(fileName, FileMode.Open); BinaryFormatter binaryFormatter = new BinaryFormatter(); CBFormCtrls cBFormCtrls = binaryFormatter.Deserialize(stream) as CBFormCtrls; stream.Close(); IEnumerator enumerator = cBFormCtrls.CtrlItemList.GetEnumerator(); try { while (enumerator.MoveNext()) { CBFormCtrlItem cBFormCtrlItem = (CBFormCtrlItem)enumerator.Current; object obj = ControlFactory.CreateControl(cBFormCtrlItem.CtrlName, cBFormCtrlItem.PartialName); ControlFactory.SetControlProperties(obj, cBFormCtrlItem.PropertyList); if (obj is Form) { ControlFactory.SetControlProperties(frm, cBFormCtrlItem.PropertyList); } else { if (obj is Control) { frm.Controls.Add(obj as Control); } } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } } #endregion #region CBFormCtrls [Serializable] public class CBFormCtrls { private static DataFormats.Format format; private ArrayList ctrlItemList = new ArrayList(); public static DataFormats.Format Format { get { return CBFormCtrls.format; } } public ArrayList CtrlItemList { get { return this.ctrlItemList; } } static CBFormCtrls() { CBFormCtrls.format = DataFormats.GetFormat(typeof(CBFormCtrls).FullName); } public CBFormCtrls() { } public CBFormCtrls(object[] ctrls) { for (int i = 0; i < ctrls.Length; i++) { object ctrl = ctrls[i]; CBFormCtrlItem cBFormCtrlItem = new CBFormCtrlItem(ctrl); this.ctrlItemList.Add(cBFormCtrlItem); } } } #endregion #region CBFormCtrlItem [Serializable] public class CBFormCtrlItem { private string ctrlName; private string partialName; private Hashtable propertyList = new Hashtable(); public string CtrlName { get { return this.ctrlName; } set { this.ctrlName = value; } } public string PartialName { get { return this.partialName; } set { this.partialName = value; } } public Hashtable PropertyList { get { return this.propertyList; } } public CBFormCtrlItem() { this.ctrlName = ""; this.partialName = ""; } public CBFormCtrlItem(object ctrl) { this.CtrlName = ctrl.GetType().Name; this.PartialName = ctrl.GetType().Namespace; PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(ctrl); IEnumerator enumerator = properties.GetEnumerator(); try { while (enumerator.MoveNext()) { PropertyDescriptor propertyDescriptor = (PropertyDescriptor)enumerator.Current; try { if (!propertyDescriptor.PropertyType.IsArray) { if (propertyDescriptor.PropertyType.IsSerializable) { this.propertyList.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(ctrl)); } } else { Array array = propertyDescriptor.GetValue(ctrl) as Array; if (array.Length > 0 && array.GetValue(0).GetType().IsSerializable) { this.propertyList.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(ctrl)); } } } catch (Exception ex) { Trace.WriteLine(ex.Message); } } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } } } #endregion }