Creating map grid labels


Summary This topic contains code examples for creating different types of map grid labels.

In this topic


Populate IGridLabel

The following code example shows how to populate the properties exposed by IGridLabel for a new GridLabel:
[VB.NET]
Public Sub PopulateIGridLabelProperties()
    
    ' Set font and color.
    Dim standardFont As StdFont = New StdFont
    Dim fontDisp As IFontDisp = TryCast(standardFont, IFontDisp)
    fontDisp.Name = "Arial"
    fontDisp.Size = 24
    
    ' Create grid label.
    Dim gridLabel As IGridLabel = New DMSGridLabelClass
    gridLabel.Font = fontDisp
    gridLabel.Color = BuildRGB(0, 0, 0)
    
    'Specify vertical labels.
    gridLabel.LabelAlignment(esriGridAxisEnum.esriGridAxisLeft) = False
    gridLabel.LabelAlignment(esriGridAxisEnum.esriGridAxisRight) = False
    gridLabel.LabelOffset = 6
    
    'Create the grid.
    'Set the properties specific to the type of grid label you are creating.
    'Associate the created grid label to the grid using the grid's
    'IMapGrid.LabelFormat property:
    Dim mapGrid As IMapGrid = New GraticuleClass
    mapGrid.Name = "Map Grid"
    mapGrid.LabelFormat = gridLabel
End Sub

Public Function BuildRGB(ByVal red As Int32, ByVal green As Int32, ByVal blue As Int32) As IColor
    
    Dim rgbColor As IRgbColor = New RgbColorClass
    With rgbColor
        .Red = red
        .Green = green
        .Blue = blue
        .UseWindowsDithering = True
    End With
    Return rgbColor
    
End Function
[C#]
public void PopulateIGridLabelProperties()
{
    // Set font and color.
    StdFont standardFont = new StdFont();
    IFontDisp fontDisp = standardFont as IFontDisp;
    fontDisp.Name = "Arial";
    fontDisp.Size = 24;

    // Create grid label.
    IGridLabel gridLabel = new DMSGridLabelClass();
    gridLabel.Font = fontDisp;
    gridLabel.Color = BuildRGB(0, 0, 0);

    //Specify vertical labels.
    gridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisLeft, false);
    gridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisRight, false);
    gridLabel.LabelOffset = 6;

    //Create the grid.
    //Set the properties specific to the type of grid label you are creating. 
    //Associate the created grid label to the grid using the grid's 
    //IMapGrid.LabelFormat property:
    IMapGrid mapGrid = new GraticuleClass();
    mapGrid.Name = "Map Grid";
    mapGrid.LabelFormat = gridLabel;
}

public IColor BuildRGB(Int32 red, Int32 green, Int32 blue)
{
    IRgbColor rgbColor = new RgbColorClass();
    rgbColor.Red = red;
    rgbColor.Green = green;
    rgbColor.Blue = blue;
    rgbColor.UseWindowsDithering = true;
    return rgbColor;
}

DMS grid label

The following code example shows how to create a degrees/minutes/seconds (DMS) grid label:
[VB.NET]
Public Sub CreateDMSGridLabel()
    
    'Create a DMS grid label.
    Dim dmsGridLabel As IDMSGridLabel = New DMSGridLabelClass
    
    'Set IDMSGridLabel properties.
    dmsGridLabel.LabelType = esriDMSGridLabelType.esriDMSGridLabelStandard
    dmsGridLabel.ShowZeroMinutes = True
    dmsGridLabel.ShowZeroSeconds = True
    
    Dim latLonFormat As ILatLonFormat = New LatLonFormatClass
    latLonFormat.ShowDirections = True
    dmsGridLabel.LatLonFormat = latLonFormat
    
    Dim standardFont As StdFont = New StdFont
    Dim fontDisp As IFontDisp = TryCast(standardFont, IFontDisp)
    fontDisp.Bold = False
    fontDisp.Name = "Arial"
    fontDisp.Italic = False
    fontDisp.Underline = False
    fontDisp.Size = 8
    
    dmsGridLabel.MinutesFont = fontDisp
    dmsGridLabel.MinutesColor = BuildRGB(0, 0, 0)
    dmsGridLabel.SecondsFont = fontDisp
    dmsGridLabel.SecondsColor = BuildRGB(0, 0, 0)
    
End Sub
[C#]
public void CreateDMSGridLabel()
{
    //Create a DMS grid label.
    IDMSGridLabel dmsGridLabel = new DMSGridLabelClass();

    //Set IDMSGridLabel properties.
    dmsGridLabel.LabelType = esriDMSGridLabelType.esriDMSGridLabelStandard;
    dmsGridLabel.ShowZeroMinutes = true;
    dmsGridLabel.ShowZeroSeconds = true;

    ILatLonFormat latLonFormat = new LatLonFormatClass();
    latLonFormat.ShowDirections = true;
    dmsGridLabel.LatLonFormat = latLonFormat;

    StdFont standardFont = new StdFont();
    IFontDisp fontDisp = standardFont as IFontDisp;
    fontDisp.Bold = false;
    fontDisp.Name = "Arial";
    fontDisp.Italic = false;
    fontDisp.Underline = false;
    fontDisp.Size = 8;

    dmsGridLabel.MinutesFont = fontDisp;
    dmsGridLabel.MinutesColor = BuildRGB(0, 0, 0);
    dmsGridLabel.SecondsFont = fontDisp;
    dmsGridLabel.SecondsColor = BuildRGB(0, 0, 0);
}

Formatted grid label

The following code example shows how to create a formatted grid label:
[VB.NET]
Public Sub CreateFormattedGridLabel()
    
    Dim numericFormat As INumericFormat = New NumericFormatClass
    numericFormat.AlignmentOption = esriNumericAlignmentEnum.esriAlignRight
    numericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfDecimals
    numericFormat.RoundingValue = 2
    numericFormat.ShowPlusSign = False
    numericFormat.UseSeparator = True
    numericFormat.ZeroPad = True
    
    'Create the label and set IFormattedGridLabel properties.
    Dim formattedGridLabel As IFormattedGridLabel = New FormattedGridLabelClass
    formattedGridLabel.Format = TryCast(numericFormat, INumberFormat)
    
End Sub
[C#]
public void CreateFormattedGridLabel()
{
    INumericFormat numericFormat = new NumericFormatClass();
    numericFormat.AlignmentOption = esriNumericAlignmentEnum.esriAlignRight;
    numericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfDecimals;
    numericFormat.RoundingValue = 2;
    numericFormat.ShowPlusSign = false;
    numericFormat.UseSeparator = true;
    numericFormat.ZeroPad = true;

    //Create the label and set IFormattedGridLabel properties.
    IFormattedGridLabel formattedGridLabel = new FormattedGridLabelClass();
    formattedGridLabel.Format = numericFormat as INumberFormat;
}

Mixed font grid label

The following code example shows how to create a mixed font grid label:
[VB.NET]
Public Sub CreateMixedFontGridLabel()
    
    Dim standardFont As StdFont = New StdFont
    Dim fontDisp As IFontDisp = TryCast(standardFont, IFontDisp)
    fontDisp.Name = "Arial"
    fontDisp.Size = 12
    
    'Create the label and set IMixedFontGridLabel properties.
    Dim mixedFontGridLabel As IMixedFontGridLabel = New MixedFontGridLabelClass
    mixedFontGridLabel.SecondaryFont = fontDisp
    mixedFontGridLabel.SecondaryColor = BuildRGB(0, 0, 0)
    mixedFontGridLabel.NumGroupedDigits = 6 '–1 if not being used.
    
    Dim numericFormat As INumericFormat = New NumericFormatClass
    numericFormat.AlignmentOption = esriNumericAlignmentEnum.esriAlignRight
    numericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfDecimals
    numericFormat.RoundingValue = 2
    numericFormat.ShowPlusSign = True
    numericFormat.UseSeparator = False
    numericFormat.ZeroPad = True
    
    'Set IFormattedGridLabel properties.
    Dim formattedGridLabel As IFormattedGridLabel = TryCast(mixedFontGridLabel, IFormattedGridLabel)
    formattedGridLabel.Format = CType(numericFormat, INumberFormat)
    
End Sub
[C#]
public void CreateMixedFontGridLabel()
{
    StdFont standardFont = new StdFont();
    IFontDisp fontDisp = standardFont as IFontDisp;
    fontDisp.Name = "Arial";
    fontDisp.Size = 12;

    //Create the label and set IMixedFontGridLabel properties.
    IMixedFontGridLabel mixedFontGridLabel = new MixedFontGridLabelClass();
    mixedFontGridLabel.SecondaryFont = fontDisp;
    mixedFontGridLabel.SecondaryColor = BuildRGB(0, 0, 0);
    mixedFontGridLabel.NumGroupedDigits = 6; //–1 if not being used.

    INumericFormat numericFormat = new NumericFormatClass();
    numericFormat.AlignmentOption = esriNumericAlignmentEnum.esriAlignRight;
    numericFormat.RoundingOption = esriRoundingOptionEnum.esriRoundNumberOfDecimals;
    numericFormat.RoundingValue = 2;
    numericFormat.ShowPlusSign = true;
    numericFormat.UseSeparator = false;
    numericFormat.ZeroPad = true;

    //Set IFormattedGridLabel properties.
    IFormattedGridLabel formattedGridLabel = mixedFontGridLabel as
        IFormattedGridLabel;
    formattedGridLabel.Format = (INumberFormat)numericFormat;
}

Populate IIndexGridTabStyle

The following code example shows how to populate the properties exposed by the IIndexGridTabStyle interface after you create the label:
[VB.NET]
Public Sub PopulateIIndexGridTabStyle()
    
    ' Create colors.
    Dim foregroundColor As IColor = New RgbColorClass
    foregroundColor.RGB = &HECDEFE ' -> Pink.
    Dim outlineColor As IColor = New RgbColorClass
    outlineColor.RGB = &HBBBBBB ' -> Gray.
    
    'Set IIndexGridTabStyle properties.
    Dim indexGridTabStyle As IIndexGridTabStyle = New BackgroundTabStyleClass
    indexGridTabStyle.ForegroundColor = foregroundColor
    indexGridTabStyle.OutlineColor = outlineColor
    indexGridTabStyle.Thickness = 20
    
End Sub
[C#]
public void PopulateIIndexGridTabStyle()
{
    // Create colors.
    IColor foregroundColor = new RgbColorClass();
    foregroundColor.RGB = 0XECDEFE; // -> Pink.
    IColor outlineColor = new RgbColorClass();
    outlineColor.RGB = 0XBBBBBB; // -> Gray.

    //Set IIndexGridTabStyle properties.
    IIndexGridTabStyle indexGridTabStyle = new BackgroundTabStyleClass();
    indexGridTabStyle.ForegroundColor = foregroundColor;
    indexGridTabStyle.OutlineColor = outlineColor;
    indexGridTabStyle.Thickness = 20;
}

Button tab style grid label

Button tab style labels are rectangular buttons, each the width of the grid cell that it borders. The following code example shows how to create a button tab style grid label:
[VB.NET]
Public Sub CreateButtonTabStyleGridLabel()
    
    'Create the label.
    Dim indexGridTabStyle As IIndexGridTabStyle = New ButtonTabStyleClass
    
End Sub
[C#]
public void CreateButtonTabStyleGridLabel()
{
    //Create the label.
    IIndexGridTabStyle indexGridTabStyle = new ButtonTabStyleClass();
}

Continuous tab style grid label

Continuous tab style labels form a continuous band around the map grid. The following code example shows how to create this type of label:
[VB.NET]
Public Sub CreateContinuousTabStyleGridLabel()
    
    Dim indexGridTabStyle As IIndexGridTabStyle = New ContinuousTabStyleClass
    
End Sub
[C#]
public void CreateContinuousTabStyleGridLabel()
{
    IIndexGridTabStyle indexGridTabStyle = new ContinuousTabStyleClass();
}

Rounded tab style grid label

Rounded tab style labels are rounded rectangles, each the width of the grid cell that it borders. Use the following code example to create a rounded tab style grid label:
[VB.NET]
Public Sub CreateRoundedTabStyleGridLabel()
    
    Dim indexGridTabStyle As IIndexGridTabStyle = New RoundedTabStyleClass
    
End Sub
[C#]
public void CreateRoundedTabStyleGridLabel()
{
    IIndexGridTabStyle indexGridTabStyle = new RoundedTabStyleClass();
}

Round box style grid label

The following code example shows how to create a background tab style label that uses round boxes to label a map grid:
[VB.NET]
Public Sub CreateRoundBoxTabStyleGridLabel()
    
    Dim indexGridTabStyle As IIndexGridTabStyle = New BackgroundTabStyleClass
    
    'Set IBackgroundTabStyle properties.
    Dim backgroundTabStyle As IBackgroundTabStyle = TryCast(indexGridTabStyle, IBackgroundTabStyle)
    backgroundTabStyle.BackgroundType = esriBackgroundTabType.esriBackgroundTabRound
    
End Sub
[C#]
public void CreateRoundBoxTabStyleGridLabel()
{
    IIndexGridTabStyle indexGridTabStyle = new BackgroundTabStyleClass();

    //Set IBackgroundTabStyle properties.
    IBackgroundTabStyle backgroundTabStyle = indexGridTabStyle as
        IBackgroundTabStyle;
    backgroundTabStyle.BackgroundType = esriBackgroundTabType.esriBackgroundTabRound;
}


See Also:

How to create map grids
IMapGrid interface
Map grids




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime