窗口设计:
所用到的控件:axSymbolControl、groupBox、MarkerSymbol(Button)、Label、Button、NumericUpDown
符号窗口代码(记得绑定【确定】【取消】窗口加载等事件):
public partial class SymbolForm : Form
{
private IMapControlDefault m_mapControl;
private ILayer m_Layer;
private ILegendClass m_LegendClass;
private ISymbologyStyleClass m_SymbologyStyleClass;
/// <summary>
/// 符号属性集合
/// </summary>
private Dictionary<int, double> outlineWidths;
private Dictionary<int, Color> outlineColors;
private Dictionary<int, Color> fillColors;
public SymbolForm(IMapControlDefault m_mapControl, ILayer m_Layer, ILegendClass m_LegendClass)
{
InitializeComponent();
// 初始化地图符号对象
this.m_mapControl = m_mapControl;
this.m_Layer = m_Layer;
this.m_LegendClass = m_LegendClass;
// 初始化符号属性集合
this.outlineWidths = new Dictionary<int, double>();
this.outlineColors = new Dictionary<int, Color>();
this.fillColors = new Dictionary<int, Color>();
// 初始化符号控件
Init();
}
private void Init()
{
//此处需要在文件中找到ESRI.ServerStyle文件复制到文件夹下
axSymbologyControl1.LoadStyleFile(AppDomain.CurrentDomain.BaseDirectory + " ESRI.ServerStyle");
axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassFillSymbols;
m_SymbologyStyleClass = axSymbologyControl1.GetStyleClass(axSymbologyControl1.StyleClass);
// 添加当前符号
IStyleGalleryItem pStyleGalleryItem = new ServerStyleGalleryItem();
pStyleGalleryItem.Name = "当前符号";
pStyleGalleryItem.Item = m_LegendClass.Symbol;
m_SymbologyStyleClass.AddItem(pStyleGalleryItem, 0);
// 符号属性集合
for (int i = 0; i < m_SymbologyStyleClass.get_ItemCount(); i++)
{
IStyleGalleryItem item = m_SymbologyStyleClass.GetItem(i);
IFillSymbol pFillSymbol = item.Item as IFillSymbol;
ILineSymbol pLineSymbol = pFillSymbol.Outline;
if (!outlineWidths.ContainsKey(item.ID))
{
outlineWidths.Add(item.ID, pLineSymbol.Width);
}
if (!outlineColors.ContainsKey(item.ID))
{
outlineColors.Add(item.ID, ConvertToColor(pLineSymbol.Color));
}
if (!fillColors.ContainsKey(item.ID))
{
if (pFillSymbol.Color == null)
{
fillColors.Add(item.ID, this.BackColor);
}
else
{
fillColors.Add(item.ID, ConvertToColor(pFillSymbol.Color));
}
}
}
m_SymbologyStyleClass.SelectItem(0);
}
/// <summary>
/// 双击选中符号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axSymbologyControl1_OnDoubleClick(object sender, ISymbologyControlEvents_OnDoubleClickEvent e)
{
btnOK.PerformClick();
}
/// <summary>
/// 切换符号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axSymbologyControl1_OnItemSelected(object sender, ISymbologyControlEvents_OnItemSelectedEvent e)
{
IStyleGalleryItem pStyleGalleryItem = e.styleGalleryItem as IStyleGalleryItem;
numLineWidth.Value = (decimal)outlineWidths[pStyleGalleryItem.ID];
LineColor.BackColor = outlineColors[pStyleGalleryItem.ID];
FillColor.BackColor = fillColors[pStyleGalleryItem.ID];
// 预览
IFillSymbol pFillSymbol = pStyleGalleryItem.Item as IFillSymbol;
ILineSymbol pLineSymbol = pFillSymbol.Outline;
pLineSymbol.Width = outlineWidths[pStyleGalleryItem.ID];
pLineSymbol.Color = ConvertToRgbColor(outlineColors[pStyleGalleryItem.ID]);
pFillSymbol.Color = ConvertToRgbColor(fillColors[pStyleGalleryItem.ID]);
pFillSymbol.Outline = pLineSymbol;
PreviewSymbol(pStyleGalleryItem);
}
/// <summary>
/// 切换填充颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FillColor_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == DialogResult.OK)
{
FillColor.BackColor = colorDialog.Color;
// 预览
IStyleGalleryItem pStyleGalleryItem = m_SymbologyStyleClass.GetSelectedItem();
IFillSymbol pFillSymbol = pStyleGalleryItem.Item as IFillSymbol;
ILineSymbol pLineSymbol = pFillSymbol.Outline;
pLineSymbol.Width = (double)numLineWidth.Value;
pLineSymbol.Color = ConvertToRgbColor(LineColor.BackColor);
pFillSymbol.Color = ConvertToRgbColor(FillColor.BackColor);
pFillSymbol.Outline = pLineSymbol;
PreviewSymbol(pStyleGalleryItem);
}
}
/// <summary>
/// 切换边框颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LineColor_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == DialogResult.OK)
{
LineColor.BackColor = colorDialog.Color;
// 预览
IStyleGalleryItem pStyleGalleryItem = m_SymbologyStyleClass.GetSelectedItem();
IFillSymbol pFillSymbol = pStyleGalleryItem.Item as IFillSymbol;
ILineSymbol pLineSymbol = pFillSymbol.Outline;
pLineSymbol.Width = (double)numLineWidth.Value;
pLineSymbol.Color = ConvertToRgbColor(LineColor.BackColor);
pFillSymbol.Color = ConvertToRgbColor(FillColor.BackColor);
pFillSymbol.Outline = pLineSymbol;
PreviewSymbol(pStyleGalleryItem);
}
}
/// <summary>
/// 切换边框宽度
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void numLineWidth_ValueChanged(object sender, EventArgs e)
{
// 预览
IStyleGalleryItem pStyleGalleryItem = m_SymbologyStyleClass.GetSelectedItem();
IFillSymbol pFillSymbol = pStyleGalleryItem.Item as IFillSymbol;
ILineSymbol pLineSymbol = pFillSymbol.Outline;
pLineSymbol.Width = (double)numLineWidth.Value;
pLineSymbol.Color = ConvertToRgbColor(LineColor.BackColor);
pFillSymbol.Color = ConvertToRgbColor(FillColor.BackColor);
pFillSymbol.Outline = pLineSymbol;
PreviewSymbol(pStyleGalleryItem);
}
/// <summary>
/// 确定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOK_Click(object sender, EventArgs e)
{
IGeoFeatureLayer pGeoFeatureLayer = m_Layer as IGeoFeatureLayer;
IFeatureRenderer pFeatureRenderer = pGeoFeatureLayer.Renderer;
ISimpleRenderer pSimpleRenderer = pFeatureRenderer as ISimpleRenderer;
pSimpleRenderer.Symbol = m_SymbologyStyleClass.GetSelectedItem().Item as ISymbol;
m_mapControl.Refresh();
this.Close();
}
/// <summary>
/// 取消
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
/// <summary>
/// Color转换为IColor
/// </summary>
/// <param name="color"></param>
/// <returns></returns>
private IColor ConvertToRgbColor(Color color)
{
IColor pColor = new RgbColor();
pColor.RGB = color.R + color.G * 256 + color.B * 65536;
return pColor;
}
/// <summary>
/// IColor转换为Color
/// </summary>
/// <param name="pColor"></param>
/// <returns></returns>
private Color ConvertToColor(IColor pColor)
{
return ColorTranslator.FromOle(pColor.RGB);
}
/// <summary>
/// 预览符号
/// </summary>
private void PreviewSymbol(IStyleGalleryItem pStyleGalleryItem)
{
IPictureDisp pPictureDisp = m_SymbologyStyleClass.PreviewItem(pStyleGalleryItem, MarkerSymbol.Width, MarkerSymbol.Height);
Image priviewImage = Image.FromHbitmap(new IntPtr(pPictureDisp.Handle));
MarkerSymbol.Image = priviewImage;
}
}
系统主窗口代码:(记得绑定TOCControl事件,这里为了测试方便,添加了右键等功能代码)
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
ESRI.ArcGIS.Controls.esriTOCControlItem Item = ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemNone;
IBasicMap pBasicMap = null;
ILayer pLayer = null;
object other = null;
object index = null;
axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index); //实现赋值
//图层左键
if (e.button == 1)
{
if (Item == esriTOCControlItem.esriTOCControlItemLegendClass)
{
if (pLayer is IFeatureLayer)
{
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
if (pFeatureLayer.FeatureClass.ShapeType == esriGeometryType.esriGeometryPolygon)
{
ILegendGroup pLegendGroup = other as ILegendGroup;
ILegendClass pLegendClass = pLegendGroup.get_Class((int)index);
SymbolForm frm = new SymbolForm(axMapControl1.Object as IMapControlDefault, pLayer, pLegendClass);
frm.ShowDialog();
}
}
}
}
}
运行图:
说明:包括后文中的点符号、面符号(填充符号)都需要此文件:ESRI.ServerStyle。只要装过ArcGIS就会有此文件,可电脑搜索,也可到此链接下载
https://download.csdn.net/download/say_book/85001155
扫码关注公众号