ESRI.ArcGIS.DisplayUI.SymbolSelectorClass(); bool bOK = false; pSS.AddSymbol(pSym);
bOK = pSS.SelectSymbol(0); if (bOK) {
pLC.Symbol = pSS.GetSymbolAt(0); }
this.axMapControl1.ActiveView.Refresh(); this.axTOCControl1.Refresh(); } } }
(4)编译运行即可。
2.自定义符号选择器
AE9.2提供了SymbologyControl控件,极大的方便了图层符号选择器的制作。本讲实现的符号选择器有如下功能。 用户双击TOCControl控件中图层的符号时,弹出选择符号对话框,对话框能够根据图层类型自动加载相应的符号,如点、线、面。用户可以调整符号的颜色、线宽、角度等参数。还可以打开自定义的符号文件(*.ServerStyle),加载更多的符号。
2.1 新建符号选择器窗体 新建Winodws窗体,命名为SymbolSelectorFrm,修改窗体的Text属性为“选择符号”。并添加SymboloryControl、PictureBox、Button、Label、NumericUpDown、GroupBox、ColorDialog、OpenFileDialog、ContextMenuStrip控件。控件布局如下所示:
图3:
2.2 设置控件属性
设置相应控件的相关属性,如下表所示(空则不用修改):
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;
2.4 初始化
(1) 添加SymbolSelectorFrm的全局变量,代码如下: private IStyleGalleryItem pStyleGalleryItem; private ILegendClass pLegendClass; private ILayer pLayer; public ISymbol pSymbol;
public Image pSymbolImage;
(2) 修改SymbolSelectorFrm的构造函数,传入图层和图例接口。代码如下: ///
/// 构造函数,初始化全局变量 ///
/// ///
public SymbolSelectorFrm(ILegendClass tempLegendClass, ILayer tempLayer) {
InitializeComponent();
this.pLegendClass = tempLegendClass; this.pLayer = tempLayer; }
(3) 添加SymbolControl的SymbologyStyleClass设置函数SetFeatureClassStyle(),代码如下: ///
/// 初始化SymbologyControl的StyleClass,图层如果已有符号,则把符号添加到SymbologyControl中的第一个符号,并选中 ///
///
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的安装路径,代码如下: ///
/// 从注册表中取得指定软件的路径 ///
///
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(\}
(5) 添加SymbolSelectorFrm的Load事件。根据图层类型为SymbologyControl导入相应的符号样式文件,如点、线、面,并设置控件的可视性。代码如下:
private void SymbolSelectorFrm_Load(object sender, EventArgs e) {
//取得ArcGIS安装路径
string sInstall = ReadRegistry(\//载入ESRI.ServerStyle文件到SymbologyControl
this.axSymbologyControl.LoadStyleFile(sInstall + \
//确定图层的类型(点线面),设置好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) 双击确定按钮和取消按钮,分别添加如下代码: ///
///
private void btnOK_Click(object sender, EventArgs e) {
//取得选定的符号
this.pSymbol = (ISymbol)pStyleGalleryItem.Item; //更新预览图像
this.pSymbolImage = this.ptbPreview.Image; //关闭窗体 this.Close(); }
///
///
private void btnCancel_Click(object sender, EventArgs e) {
this.Close(); }
(7) 为了操作上的方便,我们添加SymbologyControl的DoubleClick事件,当双击符号时同按下确定按钮一样,选定符号并关闭符号选择器窗体。代码如下: ///
/// 双击符号同单击确定按钮,关闭符号选择器。 ///
///
this.btnOK.PerformClick(); }
(8) 再添加符号预览函数,当用户选定某一符号时,符号可以显示在PictureBox控件中,方便预览,函数代码如下: ///
/// 把选中并设置好的符号在picturebox控件中预览 ///
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的样式改变时,需要重新设置符号参数调整控件的可视性,故要添加