/// <summary> /// 创建与原删除控件相同的控件 /// </summary> /// <param name="info"></param> /// <returns></returns> private void CreateControl(ref Operation info) { HostDesign design = (HostDesign)info.Change[0]; Control Oldctrl = (Control)info.Item; IDesignerHost tems = design.GetService(typeof(IDesignerHost)) as IDesignerHost; Control Newctrl = (Control)tems.CreateComponent(Oldctrl.GetType()); Attribute[] tempAtr = new Attribute[] { new CategoryAttribute(PublicVariable.ControlCategoryName) }; PropertyDescriptor portNameProperty = TypeDescriptor.GetProperties(Newctrl)["PortName"]; PropertyDescriptorCollection OldPropertyDescriptors = TypeDescriptor.GetProperties(Oldctrl, tempAtr); PropertyDescriptorCollection NewPropertyDescriptors = TypeDescriptor.GetProperties(Newctrl, tempAtr); foreach (PropertyDescriptor elementA in OldPropertyDescriptors) { foreach (PropertyDescriptor elementB in NewPropertyDescriptors) { if (elementA.Name == elementB.Name) { elementB.SetValue(Newctrl, elementA.GetValue(Oldctrl)); } } } Newctrl.Tag = Newctrl.Site.Name; if (info.Change[1].ToString() != Newctrl.Site.Name) {//新旧控件名不同时需更新 UpdateCtrlInfo(ref info, info.Change[1].ToString(), Newctrl.Site.Name); } info.Item = Newctrl; if (portNameProperty != null) { string[] tempCodeinfo = ((ControlInfo)info.Change[2]).CodeInfo; string curPnum = tempCodeinfo[2].Substring(tempCodeinfo[1].Length); for (int i = 0; i < CassViewGenerator.PortInfoList.Count; i++) { if (((string[])(CassViewGenerator.PortInfoList[i][0]))[0] == tempCodeinfo[0]) {//找到的对应的控件类 ((List <string>)(CassViewGenerator.PortInfoList[i][1])).Add(curPnum); break; } } if (tempCodeinfo[1] + curPnum != tempCodeinfo[2]) { CassMessageBox.Error("撤销控件点名出错!"); } } }
/// <summary> /// 生成一个新的HostDesign,并且进行加载。 /// </summary> /// <param name="formName">所创建的设计页面的名称</param> /// <returns> 返回一个加载了设计器的HostControl对象</returns> public HostControl GetNewHost(string formName) { try { HostDesign hostSurface = (HostDesign)this.CreateDesignSurface(this.ServiceContainer); CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader(formName); hostSurface.BeginLoad(codeDomHostLoader); //使用给定的设计器加载程序开始加载过程。 hostSurface.Initialize(); return(new HostControl(hostSurface)); } catch (Exception ex) { return(null); } }
//<summary> /// /获取当前设计器的视图,并将当前的类控件设置为设计器视图的根组件。 /// </summary> /// <param name="hostDesignSurface">HostDesign类型变量,该变量为设计器类型变量,函数中获取该设计器的视图</param> private void InitializeHost(HostDesign hostDesignSurface) { try { hostDesign = hostDesignSurface; if (hostDesign == null || hostDesign.View == null) { return; } Control control = (Control)hostDesign.View; //获取根设计器的视图,该View并不是类; control.Parent = this; control.Dock = DockStyle.Fill; control.Visible = true; loadFlag = true; } catch(NotSupportedException e) { loadFlag = false; CassMessageBox.Error("未提供与此设计图面兼容的视图"); } catch(ObjectDisposedException e) { loadFlag = false; CassMessageBox.Error("设计器对象被释放!"); } catch(InvalidOperationException e) { loadFlag = false; CassMessageBox.Error("设计器加载程序尚未创建根设计器或加载失败!"); } catch (Exception ex) { CassMessageBox.Error("加载程序发生致命性错误!"); this.Dispose();//20090603便于加载至梯形图内部,关闭当前程序窗口 //Application.Exit(); } }
//<summary> /// /获取当前设计器的视图,并将当前的类控件设置为设计器视图的根组件。 /// </summary> /// <param name="hostDesignSurface">HostDesign类型变量,该变量为设计器类型变量,函数中获取该设计器的视图</param> private void InitializeHost(HostDesign hostDesignSurface) { try { hostDesign = hostDesignSurface; if (hostDesign == null || hostDesign.View == null) { return; } Control control = (Control)hostDesign.View; //获取根设计器的视图,该View并不是类; control.Parent = this; control.Dock = DockStyle.Fill; control.Visible = true; loadFlag = true; } catch (NotSupportedException e) { loadFlag = false; CassMessageBox.Error("未提供与此设计图面兼容的视图"); } catch (ObjectDisposedException e) { loadFlag = false; CassMessageBox.Error("设计器对象被释放!"); } catch (InvalidOperationException e) { loadFlag = false; CassMessageBox.Error("设计器加载程序尚未创建根设计器或加载失败!"); } catch (Exception ex) { CassMessageBox.Error("加载程序发生致命性错误!"); this.Dispose();//20090603便于加载至梯形图内部,关闭当前程序窗口 //Application.Exit(); } }
/// <summary> /// 读入新的Xml文件时创建新的设计器, /// 对读入的xml文档进行处理,如果xml文档的名称和设计器Form的名称不一致,则将设计器的名称设为和xml文档的名称一致。 /// 同时,判断输入的xml文档的格式是否为正确的描述文件; /// 对于描述文件中有控件叠加的信息,则将需叠加的控件的信息添加到设计器中。 /// </summary> /// <param name="fileName">要读入的xml文件的名称/param> /// <returns></returns> public HostControl GetNewDesignHost(string fileName) { HostDesign hostSurface = (HostDesign)this.CreateDesignSurface(this.ServiceContainer); //判断文件名称和文件中的CassView对象是否一致 string designName = Path.GetFileNameWithoutExtension(fileName); string documentDesignName = designName; XmlDocument document = new XmlDocument(); try { document.Load(fileName); XmlNode node = document.FirstChild; for (int i = 0; i < node.ChildNodes.Count; i++) // 对读入的xml文档进行处理,如果xml文档的名称和设计器Form的名称不一致,则将设计器的名称设为和xml文档的名称一致。 { if (node.ChildNodes[i].Name.Equals("Object")) { if (node.ChildNodes[i].Attributes["type"] == null) { //CassMessageBox.Warning("描述文件格式错误!"); return(null); } else { string[] type = node.ChildNodes[i].Attributes["type"].Value.Split(','); if (type.Length > 0 && type[0].Equals(PublicVariable.viewName)) { if (node.ChildNodes[i].Attributes["name"] != null) { documentDesignName = node.ChildNodes[i].Attributes["name"].Value; if (!documentDesignName.Equals(designName))//如果名字不相同,则进行修改 { node.ChildNodes[i].Attributes["name"].Value = designName; for (int j = 0; j < node.ChildNodes[i].ChildNodes.Count; j++) //修改属性节点中的名称值 { if (node.ChildNodes[i].ChildNodes[j].Name.Equals("Property") && node.ChildNodes[i].ChildNodes[j].Attributes["name"] != null && node.ChildNodes[i].ChildNodes[j].Attributes["name"].Value.Equals("Name")) { node.ChildNodes[i].ChildNodes[j].InnerText = designName; break; } } } } break; }//cassView节点 else { return(null); } } //type != null } //object } //修改名称问题 if (document != null) { CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader(designName); hostSurface.BeginLoad(codeDomHostLoader); if (!codeDomHostLoader.MyPerformLoad(document)) //false(加载失败),跳出 { return(null); } hostSurface.Initialize(); //已加载后才可进行初始化操作 IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost)); }//xml文档不为空 return(new HostControl(hostSurface)); } catch (Exception ex) { } return(null); }
public HostControl(HostDesign hostDesignSurface) { InitializeComponent(); InitializeHost(hostDesignSurface); }