English

How To: Create a Custom Symbol in C#

Summary

Instructions provided describe how to create a custom marker symbol for MapObjects-Windows in C#.

Procedure

The following steps are based on Visual Studio .NET 2003.

  1. Click the New Project button to create a new Visual Studio project. Select Visual C# Project folder from the Project Type box and Class Library from the templates box. Name the project CustomSymbol and click OK.
  2. Click on the class1.cs file from the Solution Explorer menu on the right. Change the file name in the bottom right Properties menu to Marker.cs. Next, change the class name and class constructor from Class1 to Marker in the main code window. Now a Marker class in the CustomSymbol namespace should be present.
  3. Right-click references in the Solution Explorer and select Add Reference. In the dialog box, select the .NET tab and find the MapObjects2 Custom Interop Assembly in the list. Double-click this assembly to make it appear in the Selected Components list box. Click on OK.

    Note:
    If the assembly is not visible, click the Browse button and navigate to the MapObjects2/Dotnet/Assemblies folder and select the ESRI.MapObjects2.Custom.dll.

  4. Add the following at the top of Marker.cs:

    Code:
    using ESRI.MapObjects2.Custom;

    Make the Marker class derive from ICustomMarker by adding ICustomMarker as the base class of Marker as follows:

    Code:
    public class Marker : ICustomMarker
    {
    ...
    }

  5. Select the Class View menu on the right. Navigate to the Bases and Interfaces, right-click ICustomMarker and select Add->Implement Interface.
    [O-Image] classview
    Note:
    The code should now look similar to the following:

    using System;
    using ESRI.MapObjects2.Custom;

    namespace CustomSymbol
    {
    public class Marker : ICustomMarker
    {
    public Marker()
    {

    }

    #region ICustomMarker Members

    public void ResetDC(int hDC)
    {
    // TODO: Add Marker.ResetDC implementation
    }

    public void SetupDC(int hDC, double dpi, object pBaseSym)
    {
    // TODO: Add Marker.SetupDC implementation
    }

    public void Draw(int hDC, int x, int y)
    {
    // TODO: Add Marker.Draw implementation
    }

    #endregion
    }
    }


  6. Add the System.Drawing.dll reference in the same manner as Step 3. Then copy the following code onto the Marker.cs code view:

    Code:
    using System;
    using ESRI.MapObjects2.Custom;
    using System.Drawing;

    namespace CustomSymbol
    {
    public class Marker : ICustomMarker
    {
    private IntPtr m_hdc;
    private double m_dpi;
    private object m_BaseSym;
    private Graphics m_graphics;
    private Brush m_brush;

    public Marker()
    {
    }

    #region ICustomMarker Members

    public void ResetDC(int hDC)
    {
    //Cleans up and re-establishes the original device context
    if (m_graphics != null)
    {
    m_graphics.ReleaseHdc(m_hdc);
    m_graphics.Dispose();
    m_graphics = null;

    }
    if (m_brush != null)
    {
    m_brush.Dispose();
    m_brush = null;
    }
    }

    public void SetupDC(int hDC, double dpi, object pBaseSym)
    {
    //establishes the device context and sets up symbol characteristics
    m_hdc = new IntPtr(hDC);
    m_dpi = dpi;
    m_BaseSym = pBaseSym;

    //Get the graphics drawing surface
    m_graphics = Graphics.FromHdc(m_hdc);
    //Create a brush (used to fill marker symbol)
    Color clr = Color.FromArgb(200,0,0);
    m_brush = new SolidBrush(clr);
    }

    public void Draw(int hDC, int x, int y)
    {
    //calls drawing primitve to draw the symbol
    m_graphics.FillRectangle(m_brush,x,y,4,4);
    }

    #endregion
    }
    }


    Note:
    Paste the new parts of code, or simply paste the entire code over the original Marker.cs.

  7. In the Solution Explorer view, right-click Solution 'CustomSymbol' and select Add > New Project. Select a Visual C# Project Type and a Windows Application template. Name the new project MarkerTest and click OK.
  8. In the Solution Explorer view, right-click MarkerTest and select Set as Startup Project.
  9. Click on the Form1.cs[Design] tab to switch to the design canvas. Add a map control (ESRI.MapObjects2.Core.AxMap) to Form1 and add a shapefile containing point features to the map control, axMap1.
  10. In the Solution Explorer, right-click References under the MarkerTest project and select Add Reference. Click on Browse and navigate to the bin/Debug directory of the CustomSymbol project to select CustomSymbol.dll. Repeat the procedure in Step 3 to add the MapObjects2 Custom Interop Assembly to the MarkerTest project.
  11. In the Form1.cs code view, add the following code to Form1():

    Code:
    CustomSymbol.Marker marker = new CustomSymbol.Marker();
    ESRI.MapObjects2.Core.MapLayer mapLayer = (ESRI.MapObjects2.Core.MapLayer)axMap1.Layers.Item(0);
    mapLayer.Symbol.Custom = marker;
    axMap1.CtlRefresh();

  12. Compile and run the MarkerTest project. The custom symbols should appear on the map.