/// <summary> /// 加载 /// </summary> /// <param name="serializationManager"></param> protected override void PerformLoad(IDesignerSerializationManager serializationManager) { try { ArrayList errors = new ArrayList(); bool successful = true; //获取"控件更改"服务 IComponentChangeService cs = this.LoaderHost.GetService(typeof(IComponentChangeService)) as IComponentChangeService; cs.ComponentAdded -= new ComponentEventHandler(cs_ComponentAdded); cs.ComponentRemoved -= new ComponentEventHandler(cs_ComponentRemoved); cs.ComponentChanged -= new ComponentChangedEventHandler(cs_ComponentChanged); cs.ComponentAdded += new ComponentEventHandler(cs_ComponentAdded); cs.ComponentRemoved += new ComponentEventHandler(cs_ComponentRemoved); cs.ComponentChanged += new ComponentChangedEventHandler(cs_ComponentChanged); //加载设计面板 DesignPanel ctrlContainer = null; if (this.Host.RootComponent == null) { ctrlContainer = Host.CreateComponent(typeof(DesignPanel)) as DesignPanel; } else//如果设计面板不为空,则为导入数据 { ctrlContainer = this.Host.RootComponent as DesignPanel; ctrlContainer.Controls.Clear(); } //设置设计面板最大尺寸 ctrlContainer.MaximumSize = new System.Drawing.Size(ConstValue.DesignPanelMaxWidth, ctrlContainer.MaximumSize.Height); if (this._data != null && this._data.Any(i => i.ControlName.Contains(typeof(DesignPanel).Name))) { EntityCPNode entityRoot = this._data.Find(i => i.ControlName.Contains(typeof(DesignPanel).Name)); ctrlContainer.Width = (int)entityRoot.Width; ctrlContainer.Height = (int)entityRoot.Height; this._data.Remove(entityRoot); } else { ctrlContainer.Width = (this.InitWidth > 0 ? this.InitWidth : System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - 420); ctrlContainer.Height = (this.InitHeight > 0 ? this.InitHeight : 162); } ctrlContainer.BackColor = (this.EfFlag ? Color.White : Color.FromArgb(231, 240, 249)); //Color.FromArgb(9, 111, 133)); #region 未确定代码 if (_templateControlFormID > 0) { //导入模板控件 List <EntityCPNode> tempControl = null;// FormLoader.GetControls(_templateControlFormID, ref _formVersion); CustomFormControlCreatorCP creator = new CustomFormControlCreatorCP(); List <IRuntimeDesignControl> t = new List <IRuntimeDesignControl>(); creator.CreateCntrols(0, ctrlContainer, tempControl, ref t); } #endregion ctrlContainer.designerHost = this.GetService(typeof(IDesignerHost)) as IDesignerHost; //反序列化生成控件 Deserialize(ctrlContainer, errors); //} this.LoaderHost.EndLoad(typeof(DesignPanel).Name, successful, errors); Isdirty = false; Unsave = false; } catch (Exception ex) { weCare.Core.Utils.ExceptionLog.OutPutException(ex); throw; } }
/// <summary> /// (递归)序列化单个控件 /// </summary> /// <param name="nametable"></param> /// <param name="parentSiteName"></param> /// <param name="level"></param> /// <param name="value"></param> /// <param name="serializedData"></param> private void SerializeControl(Hashtable nametable, string parentSiteName, object value, List <EntityCPNode> serializedData, bool recursive) { try { IComponent component = value as IComponent; EntityCPNode entity = new EntityCPNode(); entity.ControlType = value.GetType().AssemblyQualifiedName; if (component != null && component.Site != null && component.Site.Name != null) { entity.ControlName = component.Site.Name; //entity.Parent = parentSiteName; nametable[value] = component.Site.Name; } bool isControl = (value is Control); Control ctrl = value as Control; if (isControl) { if (ctrl is IRuntimeDesignControl) { IRuntimeDesignControl ictrl = ctrl as IRuntimeDesignControl; entity.Height = (int)ictrl.Height; entity.Width = (int)ictrl.Width; entity.Top = (int)ictrl.Location.Y; entity.Left = (int)ictrl.Location.X; entity.NodeDesc = ictrl.Text; entity.ForeColor = ictrl.ForeColor; //序列化字体 if (ictrl.TextFont != null) { entity.TextFont = FontSerializationService.Serialize(ictrl.TextFont); } } else { entity.Height = (int)ctrl.Height; entity.Width = (int)ctrl.Width; entity.Top = (int)ctrl.Top; entity.Left = (int)ctrl.Left; } if (value is ICpNode) { ICpNode iNode = value as ICpNode; entity.NodeName = iNode.NodeName; entity.NodeType = iNode.NodeType; entity.NodeDays = iNode.NodeDays; entity.ParentNodeName = iNode.ParentNodeName; } } if (component != null && isControl) { if (recursive) { foreach (Control child in ((Control)value).Controls) { if (child.Site != null && child.Site.Container == designerHost.Container) { if (!nametable.ContainsKey(child)) { SerializeControl(nametable, component.Site.Name, child, serializedData, recursive); } } } } } serializedData.Add(entity); } catch (Exception ex) { throw; } }
/// <summary> /// 递归创建控件(单个) /// </summary> /// <param name="entity"></param> /// <returns></returns> private object CreateControl(EntityCPNode entityParent, int presentationMode) { try { if (entityParent.ControlName == typeof(DesignPanel).Name + "1") { return(null); } Type type = Type.GetType(entityParent.ControlType); object instance = null; if (type != null) { instance = Activator.CreateInstance(type); if (instance != null && instance is Control) { Control ctrlParent = instance as Control; ctrlParent.Name = entityParent.ControlName; ctrlParent.Text = entityParent.NodeDesc; ctrlParent.ForeColor = entityParent.ForeColor; ctrlParent.BringToFront(); if (ctrlParent is IRuntimeDesignControl) { try { IRuntimeDesignControl ictrl = ctrlParent as IRuntimeDesignControl; this._createdControls.Add(ictrl); if (ictrl is ICpNode) { ICpNode iNode = ictrl as ICpNode; iNode.NodeName = entityParent.NodeName; iNode.NodeType = entityParent.NodeType; iNode.NodeDays = entityParent.NodeDays; iNode.ParentNodeName = entityParent.ParentNodeName; } } catch (Exception ex) { throw ex; } } //查找当前控件的子控件 //var query = from item in this._controlsdata // where item.Parent == ctrlParent.Name // select item; foreach (var itemChild in this._controlsdata)//query) { object obj = CreateControl(itemChild, presentationMode); Control ctrlChild = obj as Control; if (ctrlChild != null) { ctrlParent.Controls.Add(ctrlChild); if (ctrlChild is IRuntimeDesignControl) { IRuntimeDesignControl ICtrl = ctrlChild as IRuntimeDesignControl; ICtrl.Location = new System.Drawing.Point((int)itemChild.Left, (int)itemChild.Top); ICtrl.Width = (int)itemChild.Width; ICtrl.Height = (int)itemChild.Height; } else { ctrlChild.Location = new System.Drawing.Point((int)itemChild.Left, (int)itemChild.Top); ctrlChild.Width = (int)itemChild.Width; ctrlChild.Height = (int)itemChild.Height; } } } } } return(instance); } catch (Exception ex) { throw; } }
/// <summary> /// (递归)反序列化单个控件 /// 反序列化后如果当前控件名字已存在则重新命名当前控件:如果当前控件为子控件,则把序列化数据中的控件的父控件名字改为新的控件名字 /// </summary> /// <param name="entity"></param> /// <param name="listAll"></param> /// <returns></returns> private IComponent DeserializeControl(EntityCPNode entity, List <EntityCPNode> listAll) { try { if (entity.ControlName.Contains(typeof(DesignPanel).Name)) { return(null); } Type type = Type.GetType(entity.ControlType); object instance = null; if (type != null) { if (typeof(IComponent).IsAssignableFrom(type)) { instance = designerHost.CreateComponent(type, CreateControlName(entity.ControlName, designerHost as IContainer, Type.GetType(entity.ControlType), listAll)); } else { instance = Activator.CreateInstance(type); } if (instance != null && instance is Control) { Control ctrlParent = instance as Control; if (string.IsNullOrEmpty(ctrlParent.Name)) { ctrlParent.Name = CreateControlName(entity.ControlName, designerHost as IContainer, Type.GetType(entity.ControlType), listAll); } if (ctrlParent is ICpNode) { ICpNode iNode = ctrlParent as ICpNode; iNode.NodeName = entity.NodeName; iNode.NodeType = entity.NodeType; iNode.NodeDays = entity.NodeDays; iNode.ParentNodeName = entity.ParentNodeName; } if (ctrlParent is IRuntimeDesignControl) { IRuntimeDesignControl ictrl = ctrlParent as IRuntimeDesignControl; ictrl.Width = (int)entity.Width; ictrl.Height = (int)entity.Height; ictrl.Location = new System.Drawing.Point((int)entity.Left, (int)entity.Top); ictrl.Text = entity.NodeDesc; ictrl.ForeColor = entity.ForeColor; if (!string.IsNullOrEmpty(entity.TextFont)) { ictrl.TextFont = FontSerializationService.Deserialize(entity.TextFont); ctrlParent.Font = FontSerializationService.Deserialize(entity.TextFont); } } else { ctrlParent.Width = (int)entity.Width; ctrlParent.Height = (int)entity.Height; ctrlParent.Location = new System.Drawing.Point((int)entity.Left, (int)entity.Top); } //var query = from item in listAll // where item.Parent == ctrlParent.Name // select item; //foreach (var item in query) //{ // IComponent objChild = DeserializeControl(item, listAll); // if (objChild is Control) // { // if (objChild is DevExpress.XtraTab.XtraTabPage) // { // (ctrlParent as ITabControl).TabPages.Add(objChild as DevExpress.XtraTab.XtraTabPage); // } // else // { // ctrlParent.Controls.Add(objChild as Control); // } // } //} } } return(instance as IComponent); } catch (Exception ex) { throw ex; } }