Common Custom renderers
Common_CustomRenderers_VBNet\App_Code\LabelPointRenderer.vb
' Copyright 2010 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Namespace ESRI.ADF.Samples.Renderers

  ''' <summary>
  ''' Creates a label over a point with a background box like this:
  ''' ---------------------
  ''' |Attribute goes here|
  ''' |________  _________|
  '''          \/
  ''' </summary>
  Public Class LabelPointRenderer
    Inherits ESRI.ADF.Samples.Renderers.RendererBase
    #Region "Public Properties"

    Private labelColumn_Renamed As String = "Name"

    ''' <summary>
    ''' Name of the column containing text to use on the label
    ''' </summary>
    <System.ComponentModel.Description("Name of the column containing text to use on the label")> _
    Public Property LabelColumn() As String
      Get
        Return labelColumn_Renamed
      End Get
      Set
        labelColumn_Renamed = Value
      End Set
    End Property

    Private backgroundColor_Renamed As System.Drawing.Color = System.Drawing.Color.White
    ''' <summary>
    ''' The fill color of the label's callout
    ''' </summary>
    <System.ComponentModel.Description("The fill color of the label's callout")> _
    Public Property BackgroundColor() As System.Drawing.Color
      Get
        Return backgroundColor_Renamed
      End Get
      Set
        backgroundColor_Renamed = Value
      End Set
    End Property

    Private outlineColor_Renamed As System.Drawing.Color = System.Drawing.Color.Black
    ''' <summary>
    ''' The outline color around the label's callout
    ''' </summary>
    <System.ComponentModel.Description("The outline color around the label's callout")> _
    Public Property OutlineColor() As System.Drawing.Color
      Get
        Return outlineColor_Renamed
      End Get
      Set
        outlineColor_Renamed = Value
      End Set
    End Property

    Private fontColor_Renamed As System.Drawing.Color = System.Drawing.Color.Black
    ' The font color used on the label
    <System.ComponentModel.Description("The font color used on the label")> _
    Public Property FontColor() As System.Drawing.Color
      Get
        Return fontColor_Renamed
      End Get
      Set
        fontColor_Renamed = Value
      End Set
    End Property

    Private font_Renamed As System.Drawing.Font
    ' The font used on the label
    <System.ComponentModel.Description("The font used on the label")> _
    Public Property Font() As System.Drawing.Font
      Get
        Return font_Renamed
      End Get
      Set
        font_Renamed = Value
      End Set
    End Property

    #End Region

    ' Use the constructor to set a default font
    Public Sub New()
      MyBase.New()
      Me.Font = New System.Drawing.Font("Arial", 8f)
    End Sub

    #Region "IRenderer Members"

    ''' <summary>
    ''' Main part of the IRenderer interface, within which a feature encapsulating the specified DataRow is to be 
    ''' rendered on the specified graphics surface. The geometry instance has already been transformed to screen 
    ''' coordinate, so we don't have to worry about that here.
    ''' </summary>
    ''' <param name="row">row containing the feature's data</param>
    ''' <param name="graphics">GDI+ surface on which to render the feature</param>
    ''' <param name="geometryColumn">column containing the feature's geometry</param>
    Public Overrides Sub Render(ByVal row As System.Data.DataRow, ByVal graphics As System.Drawing.Graphics, ByVal geometryColumn As System.Data.DataColumn)
      ' Validate method input
      If row Is Nothing OrElse graphics Is Nothing OrElse geometryColumn Is Nothing Then
        Return
      End If

      ' Validate input geometry.  This renderer will only work on points.
      Dim geometry As ESRI.ArcGIS.ADF.Web.Geometry.Geometry = TryCast(row(geometryColumn), ESRI.ArcGIS.ADF.Web.Geometry.Geometry)
      If geometry Is Nothing OrElse Not(TypeOf geometry Is ESRI.ArcGIS.ADF.Web.Geometry.Point) Then
        Return
      End If

      ' Get the input point
      Dim adfPoint As ESRI.ArcGIS.ADF.Web.Geometry.Point = TryCast(geometry, ESRI.ArcGIS.ADF.Web.Geometry.Point)

      ' Make sure the input feature has the renderer's specified label column 
      If row.Table.Columns.Contains(Me.LabelColumn) Then 'Only if column exists
        ' Get the label's text
        Dim labelText As String = row(Me.LabelColumn).ToString()

        ' Get the size of the rendered label text
        Dim size As System.Drawing.SizeF = graphics.MeasureString(labelText, Me.Font)

        ' Label parameters
        Dim margin As Integer = 2 ' Margin around text
        Dim arrowSize As Integer = 5 ' size of leader arrow pointing to our point
        Dim width As Integer = CInt(Fix(size.Width)) + margin * 2
        Dim height As Integer = CInt(Fix(size.Height)) + margin * 2

        ' Create polygon for text background such that the label will look like:
        ' ---------------------
        ' |Attribute goes here|
        ' |________  _________|
        '          \/
        '         (x,y)

        ' Use the input point's coordinates as the start point of the callout
        Dim x As Integer = CInt(Fix(adfPoint.X))
        Dim y As Integer = CInt(Fix(adfPoint.Y))

        ' Populate an array with the callout polygon's points
        Dim calloutPoints As System.Drawing.Point() = New System.Drawing.Point(7){}
        calloutPoints(0) = New System.Drawing.Point(x, y) ' Start point - tip of leader arrow
        calloutPoints(1) = New System.Drawing.Point(x - arrowSize, y - arrowSize) ' Top left of leader arrow
        calloutPoints(2) = New System.Drawing.Point(x - width / 2, calloutPoints(1).Y) ' Lower left of box
        calloutPoints(3) = New System.Drawing.Point(calloutPoints(2).X, calloutPoints(2).Y - height) ' Upper left of box
        calloutPoints(4) = New System.Drawing.Point(calloutPoints(3).X + width, calloutPoints(3).Y) ' Upper right of box
        calloutPoints(5) = New System.Drawing.Point(calloutPoints(4).X, calloutPoints(4).Y + height) ' Lower right of box
        calloutPoints(6) = New System.Drawing.Point(x + arrowSize, y - arrowSize) ' Top right of leader arrow
        calloutPoints(7) = New System.Drawing.Point(x, y) ' End point

        ' Add the callout polygon to a GDI+ graphics path
        Dim graphicsPath As System.Drawing.Drawing2D.GraphicsPath = New System.Drawing.Drawing2D.GraphicsPath()
        graphicsPath.AddPolygon(calloutPoints)

        ' Draw the callout polygon's fill (i.e. background)
        graphics.FillPath(New System.Drawing.SolidBrush(Me.BackgroundColor), graphicsPath)

        ' Draw the callout polygon's outline
        graphics.DrawPath(New System.Drawing.Pen(Me.OutlineColor), graphicsPath)

        ' Draw the label text
        graphics.DrawString(labelText, Me.Font, New System.Drawing.SolidBrush(Me.FontColor), New System.Drawing.PointF(x - width / 2 + margin, y - height - arrowSize + margin))
      End If
    End Sub

    #End Region
  End Class
End Namespace