ArcObjects Library Reference  

TriangleElementTool

About the Triangle graphic element Sample

[C#]

TriangleElementTool.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.Connection.Local;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;

using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;


namespace TriangleElement
{
  /// <summary>
  /// Summary description for TriangleElementTool.
  /// </summary>
  [Guid("2d0c353b-2a0e-4fdf-90ae-fc8e1314a989")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("TriangleElement.TriangleElementTool")]
  public sealed class TriangleElementTool : BaseTool
  {
    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType);

      //
      // TODO: Add any COM registration code here
      //
    }

    [ComUnregisterFunction()]
    [ComVisible(false)]
    static void UnregisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryUnregistration(registerType);

      //
      // TODO: Add any COM unregistration code here
      //
    }

    #region ArcGIS Component Category Registrar generated code
    /// <summary>
    /// Required method for ArcGIS Component Category registration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryRegistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Register(regKey);
      ControlsCommands.Register(regKey);
    }
    /// <summary>
    /// Required method for ArcGIS Component Category unregistration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryUnregistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Unregister(regKey);
      ControlsCommands.Unregister(regKey);
    }

    #endregion
    #endregion

    private IHookHelper m_hookHelper = null;

    public TriangleElementTool()
    {
      base.m_category = ".NET Samples";
      base.m_caption = "Triangle Element";
      base.m_message = "Add Triangle Element";
      base.m_toolTip = "Add Triangle Element";
      base.m_name = "TriangleElement_TriangleElementTool";  
      try
      {
        string bitmapResourceName = GetType().Name + ".bmp";
        base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
        base.m_cursor = new Cursor(GetType(), GetType().Name + ".cur");
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
      }
    }

    #region Overriden Class Methods

    /// <summary>
    /// Occurs when this tool is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      try
      {
        m_hookHelper = new HookHelperClass();
        m_hookHelper.Hook = hook;
        if (m_hookHelper.ActiveView == null)
        {
          m_hookHelper = null;
        }
      }
      catch
      {
        m_hookHelper = null;
      }

      if (m_hookHelper == null)
        base.m_enabled = false;
      else
        base.m_enabled = true;
    }

    /// <summary>
    /// Occurs when this tool is clicked
    /// </summary>
    public override void OnClick()
    {
      // TODO: Add TriangleElementTool.OnClick implementation
    }

    public override void OnMouseDown(int Button, int Shift, int X, int Y)
    {
      //set the color for the symbol's outline
      IColor color = (IColor)ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.Black);

      //create the triangle outline symbol
      ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
      lineSymbol.Color = color;
      lineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
      lineSymbol.Width = 2.0;

      //create the triangle's fill symbol
      color = (IColor)Converter.ToRGBColor(Color.Gold);
      color.Transparency = 50;
      ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
      simpleFillSymbol.Color = color;
      simpleFillSymbol.Outline = (ILineSymbol)lineSymbol;
      simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross;

      //create a new triangle element
      ITriangleElement triangleElement = new TriangleElementClass();
      triangleElement.Angle = 40.0;
      triangleElement.Size = 25;
      triangleElement.FillSymbol = simpleFillSymbol;

      //set the geometry of the element
      IPoint point = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
      IElement element = (IElement)triangleElement;
      element.Geometry = (IGeometry)point;

      //add the graphics element to the map
      IGraphicsContainer graphicsContainer = (IGraphicsContainer)m_hookHelper.FocusMap;
      graphicsContainer.AddElement(element, 0);
      m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
    }

    public override void OnMouseMove(int Button, int Shift, int X, int Y)
    {
      // TODO:  Add TriangleElementTool.OnMouseMove implementation
    }

    public override void OnMouseUp(int Button, int Shift, int X, int Y)
    {
      // TODO:  Add TriangleElementTool.OnMouseUp implementation
    }
    #endregion
  }
}

[Visual Basic .NET]

TriangleElementTool.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.Controls
Imports System.Windows.Forms

Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Carto


  ''' <summary>
  ''' Summary description for TriangleElementTool.
  ''' </summary>
<Guid("2d0c353b-2a0e-4fdf-90ae-fc8e1314a989"), ClassInterface(ClassInterfaceType.None), ProgId("TriangleElement.TriangleElementTool")> _
Public NotInheritable Class TriangleElementTool
  Inherits BaseTool
#Region "COM Registration Function(s)"
  <ComRegisterFunction(), ComVisible(False)> _
  Private Shared Sub RegisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryRegistration(registerType)

    '
    ' TODO: Add any COM registration code here
    '
  End Sub

  <ComUnregisterFunction(), ComVisible(False)> _
  Private Shared Sub UnregisterFunction(ByVal registerType As Type)
    ' Required for ArcGIS Component Category Registrar support
    ArcGISCategoryUnregistration(registerType)

    '
    ' TODO: Add any COM unregistration code here
    '
  End Sub

#Region "ArcGIS Component Category Registrar generated code"
  ''' <summary>
  ''' Required method for ArcGIS Component Category registration -
  ''' Do not modify the contents of this method with the code editor.
  ''' </summary>
  Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    MxCommands.Register(regKey)
    ControlsCommands.Register(regKey)
  End Sub
  ''' <summary>
  ''' Required method for ArcGIS Component Category unregistration -
  ''' Do not modify the contents of this method with the code editor.
  ''' </summary>
  Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
    Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
    MxCommands.Unregister(regKey)
    ControlsCommands.Unregister(regKey)
  End Sub

#End Region
#End Region

  Private m_hookHelper As IHookHelper = Nothing

  Public Sub New()
    MyBase.m_category = ".NET Samples"
    MyBase.m_caption = "Triangle Element"
    MyBase.m_message = "Add Triangle Element"
    MyBase.m_toolTip = "Add Triangle Element"
    MyBase.m_name = "TriangleElement_TriangleElementTool"
    Try
      Dim bitmapResourceName As String = Me.GetType().Name & ".bmp"
      MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
      MyBase.m_cursor = New Cursor(Me.GetType(), Me.GetType().Name & ".cur")
    Catch ex As Exception
      System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
    End Try
  End Sub

#Region "Overridden Class Methods"

    ''' <summary>
    ''' Occurs when this tool is created
    ''' </summary>
    ''' <param name="hook">Instance of the application</param>
    Public Overrides Sub OnCreate(ByVal hook As Object)
        Try
            m_hookHelper = New HookHelperClass()
            m_hookHelper.Hook = hook
            If m_hookHelper.ActiveView Is Nothing Then
                m_hookHelper = Nothing
            End If
        Catch
            m_hookHelper = Nothing
        End Try

        If m_hookHelper Is Nothing Then
            MyBase.m_enabled = False
        Else
            MyBase.m_enabled = True
        End If
    End Sub

    ''' <summary>
    ''' Occurs when this tool is clicked
    ''' </summary>
    Public Overrides Sub OnClick()
        ' TODO: Add TriangleElementTool.OnClick implementation
    End Sub

    Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
        Dim c As IColor = CType(ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.Black), IColor)

        Dim lineSymbol As ISimpleLineSymbol = New SimpleLineSymbolClass()
        lineSymbol.Color = c
        lineSymbol.Style = esriSimpleLineStyle.esriSLSSolid
        lineSymbol.Width = 2.0

        c = CType(ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(Color.Gold), IColor)
        Dim simpleFillSymbol As ISimpleFillSymbol = New SimpleFillSymbolClass()
        simpleFillSymbol.Color = c
        simpleFillSymbol.Outline = CType(lineSymbol, ILineSymbol)
        simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSDiagonalCross

        Dim triangleElement As ITriangleElement = New TriangleElementClass()
        triangleElement.Angle = 40.0
        triangleElement.Size = 25
        triangleElement.FillSymbol = simpleFillSymbol

        Dim point As IPoint = m_hookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)

        Dim element As IElement = CType(triangleElement, IElement)
        element.Geometry = CType(point, IGeometry)

        Dim graphicsContainer As IGraphicsContainer = CType(m_hookHelper.FocusMap, IGraphicsContainer)
        graphicsContainer.AddElement(element, 0)
        m_hookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
    End Sub

    Public Overrides Sub OnMouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
        ' TODO:  Add TriangleElementTool.OnMouseMove implementation
    End Sub

    Public Overrides Sub OnMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
        ' TODO:  Add TriangleElementTool.OnMouseUp implementation
    End Sub
#End Region
End Class