/// <summary> /// 把数据库记录反序列化为控件 /// </summary> /// <param name="rootControl"></param> private void Deserialize(Control rootControl, int presentationMode) { return; if (rootControl is XtraScrollableControl) { //说明:XtraScrollableControl存在bug,先设置坐标再添加控件,如果滚动条位置不为0会影响加入的控件坐标 XtraScrollableControl scrollControl = rootControl as XtraScrollableControl; scrollControl.VerticalScroll.Value = 0; scrollControl.HorizontalScroll.Value = 0; } //找出所有根控件(且不为ctlLayoutControl的分组和单元格) var query = from item in this._controlsdata where (item.Parent == null || item.Parent.Trim() == string.Empty) && (item.ItemParent == null || item.ItemParent.Trim() == string.Empty) select item; //生成根控件 foreach (EntityFormCtrl entity in query) { object obj = CreateControl(entity, presentationMode); if (obj is Control) { Control ctrl = obj as Control; rootControl.Controls.Add(ctrl); if (ctrl is IRuntimeDesignControl) { IRuntimeDesignControl ICtrl = ctrl as IRuntimeDesignControl; ICtrl.Location = new System.Drawing.Point((int)entity.Left, (int)entity.Top); ICtrl.Width = (int)entity.Width; ICtrl.Height = (int)entity.Height; if (!string.IsNullOrEmpty(entity.TextFont)) { ICtrl.TextFont = FontSerializationService.Deserialize(entity.TextFont); } } else { ctrl.Location = new System.Drawing.Point((int)entity.Left, (int)entity.Top); ctrl.Width = (int)entity.Width; ctrl.Height = (int)entity.Height; } } } }
/// <summary> /// 选择的控件改变 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void selectionService_SelectionChanged(object sender, EventArgs e) { if (this.Visible == false) { //窗体关闭后还会触发此事件导致错误 return; } ISelectionService selectionService = designSurface.GetService(typeof(ISelectionService)) as ISelectionService; object[] selection; //propertyGridControl1.RetrieveFields(); if (selectionService.SelectionCount == 0) { controlPropertyGrid.SelectedObject = null; } else { selection = new object[selectionService.SelectionCount]; selectionService.GetSelectedComponents().CopyTo(selection, 0); string fieldName = string.Empty; string parentNodeName = string.Empty; Component ctrl = selection[0] as Component; IRuntimeDesignControl ICtrl = ctrl as IRuntimeDesignControl; if (ICtrl != null) { wrapper = new BaseControlWarpper(ICtrl, idh); wrapper.FormId = this.FormId; wrapper.PropertyGrid = controlPropertyGrid; controlPropertyGrid.SetEditObject(wrapper); fieldName = wrapper.项目代码; parentNodeName = wrapper.父节点名; } else { controlPropertyGrid.SelectedObject = null; } wrapper_PropertyChanged(fieldName, parentNodeName); } }
/// <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 <EntityFormCtrl> serializedData, bool recursive) { try { IComponent component = value as IComponent; EntityFormCtrl entity = new EntityFormCtrl(); 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.Text = ictrl.Text; entity.ForeColor = ictrl.ForeColor; entity.BackColor = ictrl.BackColor; entity.TabIndex = ictrl.TabIndex; entity.PresentationMode = ictrl.PresentationMode; entity.ReferenceType = ictrl.Referencetype ? 1 : 0; entity.Essential = ictrl.Essential ? 1 : 0; //序列化字体 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; entity.ForeColor = ctrl.ForeColor; entity.BackColor = ctrl.BackColor; } if (value is IFormCtrl) { IFormCtrl iForm = value as IFormCtrl; entity.ItemName = iForm.ItemName; entity.ItemCaption = iForm.ItemCaption; entity.ItemType = iForm.ItemType; entity.ParentNode = iForm.ParentNode; entity.CalProperty = iForm.CalProperty; entity.RowShrinkDigit = iForm.RowShrinkDigit; } if (string.IsNullOrEmpty(parentSiteName) && ctrl.Parent != null && ctrl.Parent != designerHost.RootComponent) { if (ctrl != designerHost.RootComponent) { entity.Parent = ctrl.Parent.Site.Name; } } if (value is ICheckBox) { ICheckBox iChk = value as ICheckBox; entity.GroupName = iChk.GroupName; entity.SumName = iChk.SumName; entity.CheckedWeightValue = iChk.CheckedWeightValue; entity.Checked = (iChk.Checked ? "1" : "0"); } else if (value is ICombox) { ICombox iCbx = value as ICombox; if (iCbx.Items.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (string item in iCbx.Items) { sb.Append(string.Format(ConstValue.CONTROL_ITEMS_SPLITER + "{0}", item)); } sb.Remove(0, 1); entity.Items = sb.ToString(); } } else if (value is ICtlLine) { ICtlLine iLine = value as ICtlLine; entity.LineStyle = iLine.LineStyle.ToString(); entity.LineWidth = iLine.LineWidth; } else if (value is IPictureBox) { IPictureBox iPic = value as IPictureBox; entity.PicFileName = iPic.FileName; } else if (value is IPanel) { IPanel ipnl = ctrl as IPanel; entity.ReserveField = string.Format("{0}{1}{2}", ipnl.Columns, ConstValue.CONTROL_ITEMS_SPLITER, ipnl.Rows); entity.Items = ipnl.BorderStyle.ToString(); } else if (ctrl is ITabControl) { ITabControl iTab = ctrl as ITabControl; entity.Items = iTab.HeaderLocation.ToString().ToLower(); } else if (ctrl is DevExpress.XtraTab.XtraTabPage) { entity.Text = ((DevExpress.XtraTab.XtraTabPage)ctrl).Text; } else if (ctrl is IXtraDateTime) { IXtraDateTime ictrl = ctrl as IXtraDateTime; entity.Items = string.Format("{0}{1}{2}{3}{4}" , ictrl.DateTimeValue , ConstValue.CONTROL_ITEMS_SPLITER , ictrl.EditMask , ConstValue.CONTROL_ITEMS_SPLITER , ictrl.SPDefaultValue ); } else if (ctrl is ISignatureControl) { ISignatureControl iSign = ctrl as ISignatureControl; entity.Items = string.Format("{0}{1}{2}{3}{4}" , iSign.Caption , ConstValue.EVENT_STRING_SPLITER , iSign.IsAllowSignNull.ToString() , ConstValue.EVENT_STRING_SPLITER , iSign.IsAutoSignature.ToString()); entity.Text = string.Empty; } else if (ctrl is IRtfEditor) { IRtfEditor iRtx = ctrl as IRtfEditor; entity.Items = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}" , (iRtx.Multiline ? "1" : "0") , ConstValue.EVENT_STRING_SPLITER , (iRtx.FixedHeight ? "1" : "0") , ConstValue.EVENT_STRING_SPLITER , iRtx.RowShrinkdigit.ToString() , ConstValue.EVENT_STRING_SPLITER , iRtx.DefaultRows.ToString() , ConstValue.EVENT_STRING_SPLITER , iRtx.FirstlineCaption); } else if (value is IPatientControl) { IPatientControl iPatctrl = ctrl as IPatientControl; entity.Items = string.Format("{0}{1}{2}{3}{4}{5}{6}{7}{8}" , iPatctrl.CaptionText , ConstValue.EVENT_STRING_SPLITER , iPatctrl.InfoType.ToString() , ConstValue.EVENT_STRING_SPLITER , iPatctrl.ShowCaption.ToString() , ConstValue.EVENT_STRING_SPLITER , iPatctrl.CalcAgeType.ToString() , ConstValue.EVENT_STRING_SPLITER , iPatctrl.BandingPage.ToString()); } } 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 ex; } }
/// <summary> /// (递归)反序列化单个控件 /// 反序列化后如果当前控件名字已存在则重新命名当前控件:如果当前控件为子控件,则把序列化数据中的控件的父控件名字改为新的控件名字 /// </summary> /// <param name="entity"></param> /// <param name="listAll"></param> /// <returns></returns> private IComponent DeserializeControl(EntityFormCtrl entity, List <EntityFormCtrl> 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 IFormCtrl) { IFormCtrl iForm = ctrlParent as IFormCtrl; iForm.ItemName = entity.ItemName; iForm.ItemCaption = entity.ItemCaption; iForm.ItemType = entity.ItemType; iForm.ParentNode = entity.ParentNode; iForm.CalProperty = entity.CalProperty; iForm.RowShrinkDigit = entity.RowShrinkDigit; } if (ctrlParent is ICheckBox) { ICheckBox iChk = ctrlParent as ICheckBox; iChk.GroupName = entity.GroupName; iChk.SumName = entity.SumName; iChk.CheckedWeightValue = entity.CheckedWeightValue; iChk.Checked = (entity.Checked == "1" ? true : false); } else if (ctrlParent is ICombox) { if (!string.IsNullOrEmpty(entity.Items)) { ICombox iCbx = ctrlParent as ICombox; string[] items = entity.Items.Split(ConstValue.CONTROL_ITEMS_SPLITER); foreach (string i in items) { iCbx.Items.Add(i); } } } else if (ctrlParent is ICtlLine) { ICtlLine iLine = ctrlParent as ICtlLine; iLine.LineStyle = (CtlLineStyle)Enum.Parse(typeof(CtlLineStyle), entity.LineStyle);; iLine.LineWidth = entity.LineWidth; } else if (ctrlParent is IPictureBox) { IPictureBox iPic = ctrlParent as IPictureBox; iPic.FileName = entity.PicFileName; } else if (ctrlParent is IPanel) { if (!string.IsNullOrEmpty(entity.Items)) { IPanel ipnl = ctrlParent as IPanel; if (!string.IsNullOrEmpty(entity.ReserveField)) { string[] items = entity.ReserveField.Split(ConstValue.CONTROL_ITEMS_SPLITER); if (items.Length >= 2) { int cols = 1; int rows = 1; if (int.TryParse(items[0], out cols) && int.TryParse(items[1], out rows)) { ipnl.Columns = cols; ipnl.Rows = rows; } } } try { ipnl.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), entity.Items); } catch (Exception ex) { weCare.Core.Utils.ExceptionLog.OutPutException(ex); } } } else if (ctrlParent is ITabControl) { if (!string.IsNullOrEmpty(entity.Items)) { ITabControl ITabCtrl = ctrlParent as ITabControl; ITabCtrl.TabPages.Clear(); if (entity.Items == "top") { ITabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Top; } else if (entity.Items == "left") { ITabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Left; } else if (entity.Items == "bottom") { ITabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Bottom; } else if (entity.Items == "right") { ITabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Right; } } } else if (ctrlParent is IPatientControl) { IPatientControl ipatctrl = ctrlParent as IPatientControl; if (!string.IsNullOrEmpty(entity.Items)) { string[] items = entity.Items.Split(new string[] { ConstValue.EVENT_STRING_SPLITER }, StringSplitOptions.None); if (items.Length >= 3) { ipatctrl.CaptionText = items[0]; ipatctrl.InfoType = (EnumPatientInfoType)Enum.Parse(typeof(EnumPatientInfoType), items[1]); ipatctrl.ShowCaption = Convert.ToBoolean(items[2]); //ipatctrl.ShowUnderLine = Convert.ToBoolean(items[3]); } int intCalcAgeType = 0; if (items.Length >= 4) { int.TryParse(items[3], out intCalcAgeType); } ipatctrl.CalcAgeType = intCalcAgeType; if (items.Length >= 5) { ipatctrl.BandingPage = Convert.ToBoolean(items[4]); } } } else if (ctrlParent is ISignatureControl) { ISignatureControl iSign = ctrlParent as ISignatureControl; if (!string.IsNullOrEmpty(entity.Items)) { string[] items = entity.Items.Split(new string[] { EmrTool.EVENT_STRING_SPLITER }, StringSplitOptions.None); if (items.Length >= 3) { iSign.Caption = items[0]; iSign.IsAllowSignNull = Function.Int(items[1]); iSign.IsAutoSignature = Function.Int(items[2]); } } } else if (ctrlParent is IRtfEditor) { IRtfEditor iRtx = ctrlParent as IRtfEditor; if (!string.IsNullOrEmpty(entity.Items)) { string[] items = entity.Items.Split(new string[] { EmrTool.EVENT_STRING_SPLITER }, StringSplitOptions.None); if (items.Length >= 5) { iRtx.Multiline = Function.Int(items[0]) == 1 ? true : false; iRtx.FixedHeight = Function.Int(items[1]) == 1 ? true : false; iRtx.RowShrinkdigit = Function.Int(items[2]); iRtx.DefaultRows = Function.Int(items[3]); iRtx.FirstlineCaption = items[4]; } } } else if (ctrlParent is IXtraDateTime) { if (!string.IsNullOrEmpty(entity.Items)) { IXtraDateTime idatetime = ctrlParent as IXtraDateTime; string[] items = entity.Items.Split(ConstValue.CONTROL_ITEMS_SPLITER); if (items.Count() > 0) { if (!string.IsNullOrEmpty(items[0]))//默认时间 { DateTime dtDefDate = DateTime.Now; if (DateTime.TryParse(items[0], out dtDefDate)) { idatetime.DateTimeValue = dtDefDate; } } } if (items.Count() > 1) { if (!string.IsNullOrEmpty(items[1])) { idatetime.EditMask = items[1]; } } if (items.Count() > 2) { if (!string.IsNullOrEmpty(items[2])) { idatetime.SPDefaultValue = items[2]; } } } } 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.Text; ictrl.ForeColor = entity.ForeColor; ictrl.BackColor = entity.BackColor; if (!string.IsNullOrEmpty(entity.TextFont)) { ictrl.TextFont = FontSerializationService.Deserialize(entity.TextFont); ctrlParent.Font = FontSerializationService.Deserialize(entity.TextFont); } ictrl.TabIndex = entity.TabIndex; ictrl.PresentationMode = entity.PresentationMode; ictrl.Referencetype = entity.ReferenceType == 1 ? true : false; ictrl.Essential = entity.Essential == 1 ? true : false; } 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) { ((DevExpress.XtraTab.XtraTabPage)objChild).Text = item.Text; (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; } }
/// <summary> /// 递归创建控件(单个) /// </summary> /// <param name="entity"></param> /// <returns></returns> private object CreateControl(EntityFormCtrl 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.Text; ctrlParent.ForeColor = entityParent.ForeColor; ctrlParent.BackColor = entityParent.BackColor; ctrlParent.BringToFront(); if (ctrlParent is IRuntimeDesignControl) { try { IRuntimeDesignControl ictrl = ctrlParent as IRuntimeDesignControl; this._createdControls.Add(ictrl); ((IRuntimeDesignControl)ictrl).TabIndex = entityParent.TabIndex; ((IRuntimeDesignControl)ictrl).PresentationMode = entityParent.PresentationMode; ((IRuntimeDesignControl)ictrl).Referencetype = entityParent.ReferenceType == 1 ? true : false; ((IRuntimeDesignControl)ictrl).Essential = entityParent.Essential == 1 ? true : false; if (ictrl is IFormCtrl) { IFormCtrl iForm = ictrl as IFormCtrl; iForm.ItemName = entityParent.ItemName; iForm.ItemCaption = entityParent.ItemCaption; iForm.ItemType = entityParent.ItemType; iForm.ParentNode = entityParent.ParentNode; iForm.CalProperty = entityParent.CalProperty; iForm.RowShrinkDigit = entityParent.RowShrinkDigit; if (ctrlParent is ctlTextBox) { ctrlParent.BringToFront(); } } if (ictrl is ICheckBox) { ICheckBox iChk = ictrl as ICheckBox; iChk.GroupName = entityParent.GroupName; iChk.SumName = entityParent.SumName; iChk.CheckedWeightValue = entityParent.CheckedWeightValue; iChk.Checked = (entityParent.Checked == "1" ? true : false); } else if (ictrl is ICombox) { if (!string.IsNullOrEmpty(entityParent.Items)) { ICombox iCbx = ictrl as ICombox; string[] items = entityParent.Items.Split(EmrTool.CONTROL_ITEMS_SPLITER); foreach (string i in items) { iCbx.Items.Add(i); } } } else if (ictrl is ICtlLine) { ICtlLine iLine = ictrl as ICtlLine; iLine.LineStyle = (CtlLineStyle)Enum.Parse(typeof(CtlLineStyle), entityParent.LineStyle); iLine.LineWidth = entityParent.LineWidth; } else if (ictrl is IPictureBox) { IPictureBox iPic = ictrl as IPictureBox; iPic.FileName = entityParent.PicFileName; } else if (ictrl is IPanel) { IPanel ipnl = ctrlParent as IPanel; if (!string.IsNullOrEmpty(entityParent.ReserveField)) { string[] items = entityParent.ReserveField.Split(EmrTool.CONTROL_ITEMS_SPLITER); if (items.Length >= 2) { int cols = 1; int rows = 1; if (int.TryParse(items[0], out cols) && int.TryParse(items[1], out rows)) { ipnl.Columns = cols; ipnl.Rows = rows; } } } try { //entity.ReserveField = string.Format("{0}{1}{2}", ipnl.Columns, ConstValue.CONTROL_ITEMS_SPLITER, ipnl.Rows); //entity.Items = ipnl.BorderStyle.ToString().ToLower(); ipnl.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), entityParent.Items); } catch (Exception ex) { weCare.Core.Utils.ExceptionLog.OutPutException(ex); } } else if (ctrlParent is ITabControl) { ITabControl iTabCtrl = ctrlParent as ITabControl; ctlTabControl tabctrl = ctrlParent as ctlTabControl; tabctrl.AppearancePage.Header.Font = new System.Drawing.Font("宋体", 9.5f); if (entityParent.Items == "top") { iTabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Top; } else if (entityParent.Items == "left") { iTabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Left; } else if (entityParent.Items == "bottom") { iTabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Bottom; } else if (entityParent.Items == "right") { iTabCtrl.HeaderLocation = DevExpress.XtraTab.TabHeaderLocation.Right; } } else if (ictrl is IPatientControl) { IPatientControl ipatctrl = ctrlParent as IPatientControl; if (!string.IsNullOrEmpty(entityParent.Items)) { string[] items = entityParent.Items.Split(new string[] { EmrTool.EVENT_STRING_SPLITER }, StringSplitOptions.None); if (items.Length >= 3) { ipatctrl.CaptionText = items[0]; ipatctrl.InfoType = (EnumPatientInfoType)Enum.Parse(typeof(EnumPatientInfoType), items[1]); ipatctrl.ShowCaption = Convert.ToBoolean(items[2]); } int intCalcAgeType = 0; if (items.Length >= 4) { int.TryParse(items[3], out intCalcAgeType); } ipatctrl.CalcAgeType = intCalcAgeType; if (items.Length >= 5) { ipatctrl.BandingPage = Convert.ToBoolean(items[4]); } } } else if (ictrl is IXtraDateTime) { if (!string.IsNullOrEmpty(entityParent.Items)) { IXtraDateTime idatetime = ctrlParent as IXtraDateTime; string[] items = entityParent.Items.Split(EmrTool.CONTROL_ITEMS_SPLITER); //(new string[] { EmrTool.EVENT_STRING_SPLITER }, StringSplitOptions.None); if (items.Count() > 0) { if (!string.IsNullOrEmpty(items[0]))//默认时间 { DateTime dtDefDate = DateTime.Now; if (DateTime.TryParse(items[0], out dtDefDate)) { idatetime.DateTimeValue = dtDefDate; if (ctrlParent is IFormCtrl) { ((IFormCtrl)ctrlParent).ValueChangedFlag = false; } } } } if (items.Count() > 1) { if (!string.IsNullOrEmpty(items[1])) { idatetime.EditMask = items[1]; } } if (items.Count() > 2) { if (!string.IsNullOrEmpty(items[2])) { idatetime.SPDefaultValue = items[2]; } } } } } catch (Exception ex) { throw ex; } } else { ctrlParent.Text = entityParent.Text; // .DesignTimeText; ctrlParent.BackColor = entityParent.BackColor; ctrlParent.ForeColor = entityParent.ForeColor; } //查找当前控件的子控件 var query = from item in this._controlsdata where item.Parent == ctrlParent.Name select item; foreach (var itemChild in query) { object obj = CreateControl(itemChild, presentationMode); Control ctrlChild = obj as Control; if (ctrlChild != null) { if (ctrlParent is ITabControl && ctrlChild is DevExpress.XtraTab.XtraTabPage) { ITabControl tabCtrl = ctrlParent as ITabControl; DevExpress.XtraTab.XtraTabPage tabpage = ctrlChild as DevExpress.XtraTab.XtraTabPage; tabpage.Text = itemChild.Text; tabCtrl.TabPages.Add(tabpage); } else { 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 ex; } }
/// <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> /// Load /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ShowPanelEF_Load(object sender, EventArgs e) { if (DesignMode) { return; } try { this.SuspendLayout(); if (!string.IsNullOrEmpty(this.FormLayout)) { FormCtrls = FormTool.Entities(this.FormLayout); } else { if (Formid > 0) { EntityFormDesign vo = null; ProxyFormDesign proxy = new ProxyFormDesign(); proxy.Service.GetForm(Formid, out vo); FormCtrls = FormTool.Entities(vo.Layout); } } #region 用下面代码设定:宽*高 //if (!string.IsNullOrEmpty(eafMain.PanelSize)) //{ // string[] size = eafMain.PanelSize.Split('|'); // if (size.Length == 2) // { // int height = HopeBridge.FrameWork.Utils.Function.Int(size[0]); // if (height > 0) // { // this.Height = height + 8; // } // int width = HopeBridge.FrameWork.Utils.Function.Int(size[1]); // if (width > 0) // { // this.Width = width + 8; // } // } //} #endregion #region Controls int intMaxTop = 0; int intMaxLeft = 0; if (FormCtrls != null) { int diffTableHeight = 0; object obj = null; Control ctrl = null; EntityCtrlPoint point = null; IRuntimeDesignControl ICtrl = null; List <EntityCtrlPoint> lstCtrlPoint = new List <EntityCtrlPoint>(); FormTool.SetParentObject(this, ControlContainer); FormCtrls.Sort(); foreach (EntityFormCtrl node in FormCtrls) { if (!string.IsNullOrEmpty(node.Parent)) { continue; } obj = FormTool.CreateControl(node, FormCtrls); if (obj == null) { continue; } if (obj is Control) { ctrl = obj as Control; ctrl.Name = node.ControlName; ControlContainer.Controls.Add(ctrl); if (ctrl is IRuntimeDesignControl) { if (diffTableHeight > 0 && intMaxTop > node.Top) { node.Top += diffTableHeight; } ICtrl = ctrl as IRuntimeDesignControl; ICtrl.Location = new System.Drawing.Point(node.Left, node.Top); ICtrl.Width = node.Width; ICtrl.Height = node.Height; if (ctrl is IRtfEditor) { } else { ICtrl.Text = node.Text; } ICtrl.ForeColor = node.ForeColor; if (!string.IsNullOrEmpty(node.TextFont)) { ((Control)ctrl).Font = FontSerializationService.Deserialize(node.TextFont); } if (ctrl is ICheckBox && node.Checked == "1") { ctrl.ForeColor = Color.Blue; } if (ctrl is IFormCtrl) { ((Control)ctrl).Tag = node; } } else if (ctrl is ctlTableCase) { node.Top += diffTableHeight; ctrl.Location = new System.Drawing.Point(node.Left, node.Top); diffTableHeight = ctrl.Height - node.Height; node.Width = ctrl.Width; node.Height = ctrl.Height; ctrl.Width = node.Width; ctrl.Height = node.Height; } else { if (diffTableHeight > 0 && intMaxTop > node.Top) { node.Top += diffTableHeight; } ctrl.Location = new System.Drawing.Point(node.Left, node.Top); ctrl.Width = node.Width; ctrl.Height = node.Height; } point = new EntityCtrlPoint(); point.CtrlName = node.ControlName; point.X = node.Left; point.Y = node.Top; lstCtrlPoint.Add(point); intMaxTop = Math.Max(node.Top + node.Height, intMaxTop); intMaxLeft = Math.Max(node.Left + node.Width, intMaxLeft); } } ButtonItemClick(ControlContainer); this.Width = intMaxLeft + 20; this.Height = intMaxTop + 20; FormTool.maxHeight = this.Height; #region 多个表格时,有时需要重整坐标 if (diffTableHeight > 0) { foreach (Control ctrl1 in ControlContainer.Controls) { EntityCtrlPoint point1 = lstCtrlPoint.FirstOrDefault(t => t.CtrlName == ctrl1.Name); ctrl1.Location = new Point(point1.X, point1.Y); } } #endregion } #endregion } finally { this.ResumeLayout(); } }
/// <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> /// <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; } }