/// <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> /// �����µ�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; }
/// <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> /// 读入新的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); }
/// <summary> /// 保存整个工程的函数操作,由于在新建工程时,提供的是要保存的工程的名称, /// 在这个路径下会产生该以该工程名称命名的文件夹,该文件夹下会生成一个同名称的,以.caproj为后缀的工程资源管理文件, /// </summary> private void saveProjectOperation() { string projectFullName = null; //代表工程资源管理文件夹的路径,到.caproj文件为止。是绝对路径 string SaveToProjectName = null; //要保存的设计页面的名称 XmlDocument document = new XmlDocument(); //对每个页面所要生成的xml文档 ListObject listObject = new ListObject(); //nodeList链表中的每个元素 System.Windows.Forms.TabPage page = new System.Windows.Forms.TabPage(); //当前设计页面所在的TabPage。 string formPathName = null; //要保存的设计页面的路径(相对路径),在工程资源管理器中的路径,即:ListObject对象中的PathName属性 Control viewControl = new Control(); //指向当前的设计页面的容器控件,即CassView控件 FileStream fStream = null; StreamWriter sWriter = null; try { saveExcepitionFlag = false; //重置 //如果该项目名称不存在,则退出 if (savePath == null || saveName == null) { return; } //将所有控件显示序号属性置为False ShowNumber = true; SetShowNumber(); if (Directory.Exists(Path.Combine(savePath, MainPageName))) { Directory.Delete(Path.Combine(savePath, MainPageName), true); } //创建工程文件,得到工程目录文件夹下的工程资源管理文件,该文件以.caproj为后缀名 projectFullName = Path.Combine(savePath, saveName); //Create 如果文件存在,则不再新建文件;否则,创建新文件。 fStream = new FileStream(projectFullName, FileMode.Create); sWriter = new StreamWriter(fStream); sWriter.WriteLine(ProjectNum + "," + ProjectInfo);//第一行写工程序号,信息20090615 //对链表中的每个页面进行操作 for (int i = 0; i < nodeList.Count; i++) { listObject = (ListObject)nodeList[i]; //获得链表中的每个节点 formPathName = listObject.pathName; //相对路径 page = listObject.tabPage; //CassView cassview = (CassView)(page.Controls[0].GetNextControl(page.Controls[0], false)); //listObject.UndoAndRedo.cassview; //创建每一个文件的所需的文件夹,确保存在 string directoryName = Path.Combine(savePath, Path.GetDirectoryName(formPathName)); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } //通过判断当前的.xml文件是否存在来决定是否需要在工程文件中添加该信息 sWriter.WriteLine(formPathName + ".xml");//在工程文件中记录该XML文件的路径 document.RemoveAll(); //清空前一次所生成的xml文档的内容 SaveToProjectName = Path.Combine(savePath, formPathName); //绝对路径,所需要的创建的.xml文件的路径 viewControl = page.Controls[0].GetNextControl(page.Controls[0], false); //获得CassView控件 if (viewControl == null) { break; } HostDesign design = ((HostControl)page.Controls[0]).HostDesign; XmlNode documentNode = document.CreateElement(viewControl.Site.Name.ToString()); //添加总结点 document.AppendChild(documentNode); //添加总节点 CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader(); //调用CodeDomHostLoader类的SaveProjectToFile函数把该页属性保存到文件 codeDomHostLoader.SaveProjectToFile(document, SaveToProjectName, viewControl, designerPath); } //对链表中的每个页面进行操作 sWriter.Close(); //需要先关闭字符串流,再关闭文件流 fStream.Close(); //保存完页面信息后保存指令信息 int pageIndex = findCodePage(); if (pageIndex != -1) { ConfigCodeEdit codeEditor = (ConfigCodeEdit)(tabControlView.TabPages[pageIndex].Controls[0]); CodeText = codeEditor.CodeEditor.Text; } SaveCodetext(); } catch (Exception ex) { saveExcepitionFlag = true; //发生异常 } finally { if (sWriter != null) { sWriter.Dispose(); } if (fStream != null) { fStream.Dispose(); } } controlfilteredPropertyGrid.Refresh(); }
/// <summary> /// 保存当前页的函数操作 /// /// </summary> private void SaveCurrentForm() { XmlDocument document = new XmlDocument(); ; //定义XML文档 System.Windows.Forms.TabPage page = tabControlView.SelectedTab; //当前打开的页面 string strTab = page.Text; try { if (strTab == CodePageName) { ConfigCodeEdit codeEditor = (ConfigCodeEdit)(tabControlView.SelectedTab.Controls[0]); CodeText = codeEditor.CodeEditor.Text; SaveCodetext(); } else { Control cassControl = page.Controls[0].GetNextControl(page.Controls[0], false); //得到CassView控件 CodeDomHostLoader codeDomHostLoader = new CodeDomHostLoader(); HostDesign design = ((HostControl)page.Controls[0]).HostDesign; if (cassControl == null) { CassMessageBox.Warning("保存失败!"); return; } XmlNode documentNode = document.CreateElement(cassControl.Site.Name.ToString()); //添加总结点 document.AppendChild(documentNode); //添加总节点 //调用CodeDomHostLoader类的SaveToFile函数把当前页属性保存到文件 codeDomHostLoader.SaveToFile(document, strTab, cassControl, designerPath, Path.Combine(savePath, ((ListObject)tabList[tabControlView.SelectedIndex]).pathName + ".xml")); } } catch (Exception ex) { CassMessageBox.Warning("保存过程发生异常!"); } }