2.3 添加引用
在解决方案资源管理器中添加ArcGIS Engine的ESRI.ArcGIS.Geodatabase引用,在SymbolSelectorFrm.cs文件中添加如下引用代码:
using ESRI.ArcGIS.Display; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.SystemUI; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
2.4 初始化
(1) 添加SymbolSelectorFrm的全局变量,代码如下:
private IStyleGalleryItem pStyleGalleryItem; private ILegendClass pLegendClass; private ILayer pLayer; public ISymbol pSymbol; public Image pSymbolImage;
(2) 修改SymbolSelectorFrm的构造函数,传入图层和图例接口。代码如下:
/// 构造函数,初始化全局变量 /// </summary> /// <param name="tempLegendClass">TOC图例</param> /// <param name="tempLayer">图层</param> public SymbolSelectorFrm(ILegendClass tempLegendClass, ILayer tempLayer) { InitializeComponent(); this.pLegendClass = tempLegendClass; this.pLayer = tempLayer; }
/// <summary>
(3) 添加SymbolControl的SymbologyStyleClass设置函数SetFeatureClassStyle(),代码如下:
/// <summary> /// 初始化SymbologyControl的StyleClass,图层如果已有符号,则把符号添加到SymbologyControl中的第一个符号,并选中 /// </summary> /// <param name="symbologyStyleClass"></param> private void SetFeatureClassStyle(esriSymbologyStyleClass symbologyStyleClass) { this.axSymbologyControl.StyleClass = symbologyStyleClass; ISymbologyStyleClass pSymbologyStyleClass = this.axSymbologyControl.GetStyleClass(symbologyStyleClass); if (this.pLegendClass != null) { IStyleGalleryItem currentStyleGalleryItem = new ServerStyleGalleryItem(); currentStyleGalleryItem.Name = "当前符号"; currentStyleGalleryItem.Item = pLegendClass.Symbol; pSymbologyStyleClass.AddItem(currentStyleGalleryItem,0); this.pStyleGalleryItem = currentStyleGalleryItem; } pSymbologyStyleClass.SelectItem(0); }
(4) 添加注册表读取函数ReadRegistry(),此函数从注册表中读取ArcGIS的安装路径,代码如下:
/// 从注册表中取得指定软件的路径 /// </summary> /// <param name="sKey"></param> /// <returns></returns> private string ReadRegistry(string sKey) { //Open the subkey for reading Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey, true); if (rk == null) return ""; // Get the data from a specified item in the key. return (string)rk.GetValue("InstallDir"); }
/// <summary>
(5) 添加SymbolSelectorFrm的Load事件。根据图层类型为SymbologyControl导入相应的符号样式文件,如点、线、面,并设置控件的可视性。代码如下:
private void SymbolSelectorFrm_Load(object sender, EventArgs e) { //取得ArcGIS安装路径 string sInstall = ReadRegistry("SOFTWARE\\ESRI\\CoreRuntime");
//载入ESRI.ServerStyle文件到SymbologyControl this.axSymbologyControl.LoadStyleFile(sInstall + "\\Styles\\ESRI.ServerStyle");
//确定图层的类型(点线面),设置好SymbologyControl的StyleClass,设置好各控件的可见性(visible) IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pLayer; switch (((IFeatureLayer)pLayer).FeatureClass.ShapeType) { case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint: this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassMarkerSymbols); this.lblAngle.Visible = true; this.nudAngle.Visible = true; this.lblSize.Visible = true; this.nudSize.Visible = true; this.lblWidth.Visible = false; this.nudWidth.Visible = false; this.lblOutlineColor.Visible = false; this.btnOutlineColor.Visible = false; break; case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline: this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassLineSymbols); this.lblAngle.Visible = false; this.nudAngle.Visible = false; this.lblSize.Visible = false; this.nudSize.Visible = false; this.lblWidth.Visible = true; this.nudWidth.Visible = true; this.lblOutlineColor.Visible = false; this.btnOutlineColor.Visible = false; break; case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon: this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols); this.lblAngle.Visible = false; this.nudAngle.Visible = false; this.lblSize.Visible = false; this.nudSize.Visible = false; this.lblWidth.Visible = true; this.nudWidth.Visible = true; this.lblOutlineColor.Visible = true; this.btnOutlineColor.Visible = true; break; case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultiPatch: this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols); this.lblAngle.Visible = false; this.nudAngle.Visible = false; this.lblSize.Visible = false; this.nudSize.Visible = false; this.lblWidth.Visible = true; this.nudWidth.Visible = true; this.lblOutlineColor.Visible = true; this.btnOutlineColor.Visible = true; break; default: this.Close(); break; } }
(6) 双击确定按钮和取消按钮,分别添加如下代码:
/// 确定按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOK_Click(object sender, EventArgs e) { //取得选定的符号 this.pSymbol = (ISymbol)pStyleGalleryItem.Item; //更新预览图像 this.pSymbolImage = this.ptbPreview.Image; //关闭窗体 this.Close(); } /// <summary> /// 取消按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCancel_Click(object sender, EventArgs e) { this.Close(); }
/// <summary>
(7) 为了操作上的方便,我们添加SymbologyControl的DoubleClick事件,当双击符号时同按下确定按钮一样,选定符号并关闭符号选择器窗体。代码如下:
/// 双击符号同单击确定按钮,关闭符号选择器。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void axSymbologyControl_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnDoubleClickEvent e) { this.btnOK.PerformClick(); }
/// <summary>
(8) 再添加符号预览函数,当用户选定某一符号时,符号可以显示在PictureBox控件中,方便预览,函数代码如下:
/// <summary> /// 把选中并设置好的符号在picturebox控件中预览 /// </summary> private void PreviewImage() { stdole.IPictureDisp picture = this.axSymbologyControl.GetStyleClass(this.axSymbologyControl.StyleClass).PreviewItem(pStyleGalleryItem, this.ptbPreview.Width, this.ptbPreview.Height); System.Drawing.Image image = System.Drawing.Image.FromHbitmap(new System.IntPtr(picture.Handle)); this.ptbPreview.Image = image; }
(9) 当SymbologyControl的样式改变时,需要重新设置符号参数调整控件的可视性,故要添加SymbologyControl的OnStyleClassChanged事件,事件代码与Load事件类似,如下:
/// 当样式(Style)改变时,重新设置符号类型和控件的可视性 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void axSymbologyControl_OnStyleClassChanged(object sender, ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnStyleClassChangedEvent e) { switch ((esriSymbologyStyleClass)(e.symbologyStyleClass)) { case esriSymbologyStyleClass.esriStyleClassMarkerSymbols: this.lblAngle.Visible = true; this.nudAngle.Visible = true; this.lblSize.Visible = true; this.nudSize.Visible = true; this.lblWidth.Visible = false; this.nudWidth.Visible = false; this.lblOutlineColor.Visible = false; this.btnOutlineColor.Visible = false; break; case esriSymbologyStyleClass.esriStyleClassLineSymbols: this.lblAngle.Visible = false; this.nudAngle.Visible = false; this.lblSize.Visible = false; this.nudSize.Visible = false; this.lblWidth.Visible = true; this.nudWidth.Visible = true; this.lblOutlineColor.Visible = false; this.btnOutlineColor.Visible = false; break; case esriSymbologyStyleClass.esriStyleClassFillSymbols: this.lblAngle.Visible = false; this.nudAngle.Visible = false; this.lblSize.Visible = false; this.nudSize.Visible = false; this.lblWidth.Visible = true; this.nudWidth.Visible = true; this.lblOutlineColor.Visible = true; this.btnOutlineColor.Visible = true; break; } }
/// <summary>