SymbolSelectorFrm.cs
上传用户:songlsx
上传日期:2022-06-19
资源大小:2227k
文件大小:17k
源码类别:

GIS编程

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ESRI.ArcGIS.Carto;
  9. using ESRI.ArcGIS.Display;
  10. using ESRI.ArcGIS.esriSystem;
  11. using ESRI.ArcGIS.SystemUI;
  12. using ESRI.ArcGIS.Controls;
  13. namespace LinGIS
  14. {
  15.     public partial class SymbolSelectorFrm : DevComponents.DotNetBar.Office2007Form
  16.     {
  17.         private IStyleGalleryItem pStyleGalleryItem;
  18.         private ILegendClass pLegendClass;
  19.         private ILayer pLayer;
  20.         public ISymbol pSymbol;
  21.         public Image pSymbolImage;
  22.         /// <summary>
  23.         /// 构造函数,初始化全局变量
  24.         /// </summary>
  25.         /// <param name="tempLegendClass">TOC图例</param>
  26.         /// <param name="tempLayer">图层</param>
  27.         public SymbolSelectorFrm(ILegendClass tempLegendClass, ILayer tempLayer)
  28.         {
  29.             InitializeComponent();
  30.             this.pLegendClass = tempLegendClass;
  31.             this.pLayer = tempLayer;
  32.         }
  33.        
  34.         /// <summary>
  35.         /// 窗体Load,设好哪些空间可见,哪些不可见,设好按钮的颜色
  36.         /// </summary>
  37.         /// <param name="sender"></param>
  38.         /// <param name="e"></param>
  39.         private void SymbolSelectorFrm_Load(object sender, EventArgs e)
  40.         {
  41.             //Get the ArcGIS install location
  42.             string sInstall = ReadRegistry("SOFTWARE\ESRI\CoreRuntime");
  43.             //Load the ESRI.ServerStyle file into the SymbologyControl
  44.             this.axSymbologyControl.LoadStyleFile(sInstall + "\Styles\ESRI.ServerStyle");
  45.             
  46.             //确定图层的类型(点线面),设置好SymbologyControl的StyleClass,设置好各控件的可见性(visible)
  47.             IGeoFeatureLayer pGeoFeatureLayer = (IGeoFeatureLayer)pLayer;
  48.             switch (((IFeatureLayer)pLayer).FeatureClass.ShapeType)
  49.             {
  50.                 case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
  51.                     this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassMarkerSymbols);
  52.                     this.lblAngle.Visible = true;
  53.                     this.nudAngle.Visible = true;
  54.                     this.lblSize.Visible = true;
  55.                     this.nudSize.Visible = true;
  56.                     this.lblWidth.Visible = false;
  57.                     this.nudWidth.Visible = false;
  58.                     this.lblOutlineColor.Visible = false;
  59.                     this.btnOutlineColor.Visible = false;
  60.                     break;
  61.                 case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
  62.                     this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassLineSymbols);
  63.                     this.lblAngle.Visible = false;
  64.                     this.nudAngle.Visible = false;
  65.                     this.lblSize.Visible = false;
  66.                     this.nudSize.Visible = false;
  67.                     this.lblWidth.Visible = true;
  68.                     this.nudWidth.Visible = true;
  69.                     this.lblOutlineColor.Visible = false;
  70.                     this.btnOutlineColor.Visible = false;
  71.                     break;
  72.                 case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
  73.                     this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols);
  74.                     this.lblAngle.Visible = false;
  75.                     this.nudAngle.Visible = false;
  76.                     this.lblSize.Visible = false;
  77.                     this.nudSize.Visible = false;
  78.                     this.lblWidth.Visible = true;
  79.                     this.nudWidth.Visible = true;
  80.                     this.lblOutlineColor.Visible = true;
  81.                     this.btnOutlineColor.Visible = true;
  82.                     break;
  83.                 case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultiPatch:
  84.                     this.SetFeatureClassStyle(esriSymbologyStyleClass.esriStyleClassFillSymbols);
  85.                     this.lblAngle.Visible = false;
  86.                     this.nudAngle.Visible = false;
  87.                     this.lblSize.Visible = false;
  88.                     this.nudSize.Visible = false;
  89.                     this.lblWidth.Visible = true;
  90.                     this.nudWidth.Visible = true;
  91.                     this.lblOutlineColor.Visible = true;
  92.                     this.btnOutlineColor.Visible = true;
  93.                     break;
  94.                 default:
  95.                     this.Close();
  96.                     break;
  97.             }
  98.         }
  99.         /// <summary>
  100.         /// 设置好SymbologyControl的StyleClass,如果有图例,把当前的TOC图例的符号添加到当前SymbologyStyleClass中去,并让之处于选中状态
  101.         /// </summary>
  102.         /// <param name="symbologyStyleClass"></param>
  103.         private void SetFeatureClassStyle(esriSymbologyStyleClass symbologyStyleClass)
  104.         {
  105.             this.axSymbologyControl.StyleClass = symbologyStyleClass;
  106.             ISymbologyStyleClass pSymbologyStyleClass = this.axSymbologyControl.GetStyleClass(symbologyStyleClass);
  107.             if (this.pLegendClass != null)
  108.             {
  109.                 IStyleGalleryItem currentStyleGalleryItem = new ServerStyleGalleryItem();
  110.                 currentStyleGalleryItem.Name = "当前符号";
  111.                 currentStyleGalleryItem.Item = pLegendClass.Symbol;
  112.                 pSymbologyStyleClass.AddItem(currentStyleGalleryItem, 0);
  113.                 this.pStyleGalleryItem = currentStyleGalleryItem;
  114.             }
  115.             pSymbologyStyleClass.SelectItem(0);
  116.         }
  117.         /// <summary>
  118.         /// 读取注册表中的制定软件的路径
  119.         /// </summary>
  120.         /// <param name="sKey"></param>
  121.         /// <returns></returns>
  122.         private string ReadRegistry(string sKey)
  123.         {
  124.             //Open the subkey for reading
  125.             Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey, true);
  126.             if (rk == null) return "";
  127.             // Get the data from a specified item in the key.
  128.             return (string)rk.GetValue("InstallDir");
  129.         }
  130.         private void btnCancel_Click(object sender, EventArgs e)
  131.         {
  132.             
  133.             this.Close();
  134.         }
  135.         private void axSymbologyControl_OnItemSelected(object sender, ISymbologyControlEvents_OnItemSelectedEvent e)
  136.         {
  137.             pStyleGalleryItem = (IStyleGalleryItem)e.styleGalleryItem;
  138.             Color color;
  139.             switch (this.axSymbologyControl.StyleClass)
  140.             {
  141.                 case esriSymbologyStyleClass.esriStyleClassMarkerSymbols:
  142.                     color = this.ConvertIRgbColorToColor(((IMarkerSymbol)pStyleGalleryItem.Item).Color as IRgbColor);
  143.                     break;
  144.                 case esriSymbologyStyleClass.esriStyleClassLineSymbols:
  145.                     color = this.ConvertIRgbColorToColor(((ILineSymbol)pStyleGalleryItem.Item).Color as IRgbColor);
  146.                     break;
  147.                 case esriSymbologyStyleClass.esriStyleClassFillSymbols:
  148.                     color=this.ConvertIRgbColorToColor(((IFillSymbol)pStyleGalleryItem.Item).Color as IRgbColor);
  149.                     this.btnOutlineColor.BackColor = this.ConvertIRgbColorToColor(((IFillSymbol)pStyleGalleryItem.Item).Outline.Color as IRgbColor);
  150.                     break;
  151.                 default:
  152.                     color = Color.Black;
  153.                     break;
  154.             }
  155.             this.btnColor.BackColor = color;
  156.             this.PreviewImage();
  157.         }
  158.         /// <summary>
  159.         /// 把选中并设置好的符号在picturebox中预览
  160.         /// </summary>
  161.         private void PreviewImage()
  162.         {
  163.             stdole.IPictureDisp picture = this.axSymbologyControl.GetStyleClass(this.axSymbologyControl.StyleClass).PreviewItem(pStyleGalleryItem, this.ptbPreview.Width, this.ptbPreview.Height);
  164.             System.Drawing.Image image = System.Drawing.Image.FromHbitmap(new System.IntPtr(picture.Handle));
  165.             this.ptbPreview.Image = image;
  166.         }
  167.         private void btnOK_Click(object sender, EventArgs e)
  168.         {
  169.             //pLegendClass.Symbol = (ISymbol)pStyleGalleryItem.Item;
  170.             this.pSymbol = (ISymbol)pStyleGalleryItem.Item;
  171.             this.pSymbolImage = this.ptbPreview.Image;
  172.             this.Close();
  173.         }
  174.         private void nudSize_ValueChanged(object sender, EventArgs e)
  175.         {
  176.             ((IMarkerSymbol)this.pStyleGalleryItem.Item).Size = (double)this.nudSize.Value;
  177.             this.PreviewImage();
  178.         }
  179.         private void nudAngle_ValueChanged(object sender, EventArgs e)
  180.         {
  181.             ((IMarkerSymbol)this.pStyleGalleryItem.Item).Angle = (double)this.nudAngle.Value;
  182.             this.PreviewImage();
  183.         }
  184.         /// <summary>
  185.         /// 将ArcGIS Engine中的IRgbColor接口转换至.NET中的Color结构
  186.         /// </summary>
  187.         /// <param name="pRgbColor">IRgbColor</param>
  188.         /// <returns>.NET中的System.Drawing.Color结构表示ARGB颜色</returns>
  189.         public Color ConvertIRgbColorToColor(IRgbColor pRgbColor)
  190.         {
  191.             return ColorTranslator.FromOle(pRgbColor.RGB);
  192.         }
  193.         /// <summary>
  194.         /// 将.NET中的Color结构转换至于ArcGIS Engine中的IColor接口
  195.         /// </summary>
  196.         /// <param name="color">.NET中的System.Drawing.Color结构表示ARGB颜色</param>
  197.         /// <returns>IColor</returns>
  198.         public IColor ConvertColorToIColor(Color color)
  199.         {
  200.             IColor pColor = new RgbColorClass();
  201.             pColor.RGB = color.B * 65536 + color.G * 256 + color.R;
  202.             return pColor;
  203.         }
  204.         /// <summary>
  205.         /// 将.NET中的Color结构转换至于ArcGIS Engine中的IRgbColor接口
  206.         /// </summary>
  207.         /// <param name="color">.NET中的System.Drawing.Color结构表示ARGB颜色</param>
  208.         /// <returns>IRgbColor</returns>
  209.         public IRgbColor ConvertColorToIRgbColor(Color color)
  210.         {
  211.             IRgbColor pRgbColor = new RgbColorClass();
  212.             pRgbColor.RGB = color.B * 65536 + color.G * 256 + color.R;
  213.             return pRgbColor;
  214.         }
  215.         private void btnColor_Click(object sender, EventArgs e)
  216.         {
  217.             if (this.colorDialog.ShowDialog() == DialogResult.OK)
  218.             {
  219.                 this.btnColor.BackColor = this.colorDialog.Color;
  220.                 switch (this.axSymbologyControl.StyleClass)
  221.                 {
  222.                     case esriSymbologyStyleClass.esriStyleClassMarkerSymbols:
  223.                         ((IMarkerSymbol)this.pStyleGalleryItem.Item).Color = this.ConvertColorToIColor(this.colorDialog.Color);
  224.                         break;
  225.                     case esriSymbologyStyleClass.esriStyleClassLineSymbols:
  226.                         ((ILineSymbol)this.pStyleGalleryItem.Item).Color = this.ConvertColorToIColor(this.colorDialog.Color);
  227.                         break;
  228.                     case esriSymbologyStyleClass.esriStyleClassFillSymbols:
  229.                         ((IFillSymbol)this.pStyleGalleryItem.Item).Color=this.ConvertColorToIColor(this.colorDialog.Color);
  230.                         break;
  231.                 }
  232.                 this.PreviewImage();
  233.             }
  234.         }
  235.         private void axSymbologyControl_OnDoubleClick(object sender, ISymbologyControlEvents_OnDoubleClickEvent e)
  236.         {
  237.             this.btnOK.PerformClick();
  238.         }
  239.         private void axSymbologyControl_OnStyleClassChanged(object sender, ISymbologyControlEvents_OnStyleClassChangedEvent e)
  240.         {
  241.             switch ((esriSymbologyStyleClass)(e.symbologyStyleClass))
  242.             {
  243.                 case esriSymbologyStyleClass.esriStyleClassMarkerSymbols:
  244.                     this.lblAngle.Visible = true;
  245.                     this.nudAngle.Visible = true;
  246.                     this.lblSize.Visible = true;
  247.                     this.nudSize.Visible = true;
  248.                     this.lblWidth.Visible = false;
  249.                     this.nudWidth.Visible = false;
  250.                     this.lblOutlineColor.Visible = false;
  251.                     this.btnOutlineColor.Visible = false;
  252.                     break;
  253.                 case esriSymbologyStyleClass.esriStyleClassLineSymbols:
  254.                     this.lblAngle.Visible = false;
  255.                     this.nudAngle.Visible = false;
  256.                     this.lblSize.Visible = false;
  257.                     this.nudSize.Visible = false;
  258.                     this.lblWidth.Visible = true;
  259.                     this.nudWidth.Visible = true;
  260.                     this.lblOutlineColor.Visible = false;
  261.                     this.btnOutlineColor.Visible = false;
  262.                     break;
  263.                 case esriSymbologyStyleClass.esriStyleClassFillSymbols:
  264.                     this.lblAngle.Visible = false;
  265.                     this.nudAngle.Visible = false;
  266.                     this.lblSize.Visible = false;
  267.                     this.nudSize.Visible = false;
  268.                     this.lblWidth.Visible = true;
  269.                     this.nudWidth.Visible = true;
  270.                     this.lblOutlineColor.Visible = true;
  271.                     this.btnOutlineColor.Visible = true;
  272.                     break;
  273.             }
  274.         }
  275.         private void nudWidth_ValueChanged(object sender, EventArgs e)
  276.         {
  277.             switch (this.axSymbologyControl.StyleClass)
  278.             {
  279.                 case esriSymbologyStyleClass.esriStyleClassLineSymbols:
  280.                     ((ILineSymbol)this.pStyleGalleryItem.Item).Width = Convert.ToDouble(this.nudWidth.Value);
  281.                     break;
  282.                 case esriSymbologyStyleClass.esriStyleClassFillSymbols:
  283.                     ILineSymbol pLineSymbol = ((IFillSymbol)this.pStyleGalleryItem.Item).Outline;
  284.                     pLineSymbol.Width = Convert.ToDouble(this.nudWidth.Value);
  285.                     ((IFillSymbol)this.pStyleGalleryItem.Item).Outline = pLineSymbol;
  286.                     break;
  287.             }
  288.             this.PreviewImage();
  289.         }
  290.         private void btnOutlineColor_Click(object sender, EventArgs e)
  291.         {
  292.             if (this.colorDialog.ShowDialog() == DialogResult.OK)
  293.             {
  294.                 ILineSymbol pLineSymbol = ((IFillSymbol)this.pStyleGalleryItem.Item).Outline;
  295.                 pLineSymbol.Color = this.ConvertColorToIColor(this.colorDialog.Color);
  296.                 ((IFillSymbol)this.pStyleGalleryItem.Item).Outline = pLineSymbol;
  297.                 this.btnOutlineColor.BackColor = this.colorDialog.Color;
  298.                 this.PreviewImage();
  299.             }
  300.         }
  301.         bool contextMenuMoreSymbolInitiated = false;
  302.         private void btnMoreSymbols_Click(object sender, EventArgs e)
  303.         {
  304.             if (this.contextMenuMoreSymbolInitiated == false)
  305.             {
  306.                 string sInstall = ReadRegistry("SOFTWARE\ESRI\CoreRuntime");
  307.                 string path = System.IO.Path.Combine(sInstall, "Styles");
  308.                 string[] styleNames = System.IO.Directory.GetFiles(path, "*.ServerStyle");
  309.                 ToolStripMenuItem[] symbolContextMenuItem = new ToolStripMenuItem[styleNames.Length + 1];
  310.                 for (int i = 0; i < styleNames.Length; i++)
  311.                 {
  312.                     symbolContextMenuItem[i] = new ToolStripMenuItem();
  313.                     symbolContextMenuItem[i].CheckOnClick = true;
  314.                     symbolContextMenuItem[i].Text = System.IO.Path.GetFileNameWithoutExtension(styleNames[i]);
  315.                     if (symbolContextMenuItem[i].Text == "ESRI")
  316.                     {
  317.                         symbolContextMenuItem[i].Checked = true;
  318.                     }
  319.                     symbolContextMenuItem[i].Name = styleNames[i];
  320.                     symbolContextMenuItem[i].Click += new EventHandler(symbolContextMenuItem_Click);
  321.                 }
  322.                 symbolContextMenuItem[styleNames.Length] = new ToolStripMenuItem();
  323.                 symbolContextMenuItem[styleNames.Length].Text = "更多符号";
  324.                 symbolContextMenuItem[styleNames.Length].Click += new EventHandler(symbolContextMenuItemMoreSymbols_Click);
  325.                 this.contextMenuStripMoreSymbol.Items.AddRange(symbolContextMenuItem);
  326.                 this.contextMenuMoreSymbolInitiated = true;
  327.             }
  328.             this.contextMenuStripMoreSymbol.Show(this.btnMoreSymbols.Location);
  329.         }
  330.         void symbolContextMenuItemMoreSymbols_Click(object sender, EventArgs e)
  331.         {
  332.             if (this.openFileDialog.ShowDialog() == DialogResult.OK)
  333.             {
  334.                 this.axSymbologyControl.LoadStyleFile(this.openFileDialog.FileName);
  335.                 this.axSymbologyControl.Refresh();
  336.             }
  337.         }
  338.         private void symbolContextMenuItem_Click(object sender, EventArgs e)
  339.         {
  340.             ToolStripMenuItem pToolStripMenuItem = ((ToolStripMenuItem)sender);
  341.             //Load the style file into the SymbologyControl
  342.             if (pToolStripMenuItem.Checked == true)
  343.             {
  344.                 this.axSymbologyControl.LoadStyleFile(pToolStripMenuItem.Name);
  345.                 this.axSymbologyControl.Refresh();
  346.             }
  347.             else
  348.             {
  349.                 this.axSymbologyControl.RemoveFile(pToolStripMenuItem.Name);
  350.                 this.axSymbologyControl.Refresh();
  351.             }
  352.         }
  353.     }
  354. }