2.3 添加引用
在解决方案资源管理器中添加ArcGIS Engine的ESRI.ArcGIS.Geodatabase引用,在SymbolSelectorFrm.cs文件中添加如下引用代码:
代码
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
2.4 初始化
(1)添加SymbolSelectorFrm的全局变量,代码如下:
代码
private IStyleGalleryItem pStyleGalleryItem;
private ILegendClass pLegendClass;
private ILayer pLayer;
public ISymbol pSymbol;
public Image pSymbolImage;
private ILegendClass pLegendClass;
private ILayer pLayer;
public ISymbol pSymbol;
public Image pSymbolImage;
(2)修改SymbolSelectorFrm的构造函数,传入图层和图例接口。代码如下:
代码
/// <summary>
/// 构造函数,初始化全局变量
/// </summary>
/// <param name="tempLegendClass">TOC图例</param>
/// <param name="tempLayer">图层</param>
public SymbolSelectorFrm(ILegendClass tempLegendClass, ILayer tempLayer)
{
InitializeComponent();
this.pLegendClass = tempLegendClass;
this.pLayer = tempLayer;
}
/// 构造函数,初始化全局变量
/// </summary>
/// <param name="tempLegendClass">TOC图例</param>
/// <param name="tempLayer">图层</param>
public SymbolSelectorFrm(ILegendClass tempLegendClass, ILayer tempLayer)
{
InitializeComponent();
this.pLegendClass = tempLegendClass;
this.pLayer = tempLayer;
}
(3) 添加SymbolControl的SymbologyStyleClass设置函数SetFeatureClassStyle(),代码如下:
代码
(4) 添加注册表读取函数ReadRegistry(),此函数从注册表中读取ArcGIS的安装路径,代码如下:
代码
/// <summary>
/// 从注册表中取得指定软件的路径
/// </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>
/// <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");
}
(5)添加SymbolSelectorFrm的Load事件。根据图层类型为SymbologyControl导入相应的符号样式文件,如点、线、面,并设置控件的可视性。代码如下:
代码
(6)双击确定按钮和取消按钮,分别添加如下代码:
代码
(7)为了操作上的方便,我们添加SymbologyControl的DoubleClick事件,当双击符号时同按下确定按钮一样,选定符号并关闭符号选择器窗体。代码如下:
代码
/// <summary>
/// 双击符号同单击确定按钮,关闭符号选择器。
/// </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>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axSymbologyControl_OnDoubleClick(object sender, ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnDoubleClickEvent e)
{
this.btnOK.PerformClick();
}
(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;
}
/// 把选中并设置好的符号在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事件类似,如下:
代码