frmLegend.vb

 Imports System.Math

Friend Class frmLegend
    Inherits System.Windows.Forms.Form

#Region "Declarations"
    Private m_ParentControl As agLegend
    Private m_Location As Point
    Private Const LG_SIZE As Integer = 10
    Private CaptionRect As Rectangle
    Private CloseRect As Rectangle
#End Region

#Region "API declarations"
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
    Private Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As IntPtr
    Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    Private Const HTCAPTION As Integer = 2
    Private Const WM_ACTIVATE As Integer = &H6
    Private Const WA_INACTIVE As Integer = 0
#End Region

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub
    Public Sub New(ByVal lgLegendPoint As Point)
        MyClass.New()
        m_Location = lgLegendPoint
    End Sub
    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'frmLegend
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.BackColor = System.Drawing.SystemColors.Window
        Me.ClientSize = New System.Drawing.Size(8, 8)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
        Me.Name = "frmLegend"
        Me.ShowInTaskbar = False
        Me.Text = "frmLegend"

    End Sub

#End Region

#Region "Methods"
    Friend Sub SetItems(ByVal ParentControl As agLegend)
        m_ParentControl = ParentControl
        Me.BackColor = m_ParentControl.BackColor
    End Sub
#End Region

#Region "Private helpers"
    Private Function GetMaxSize(ByVal e As System.Windows.Forms.PaintEventArgs) As Size
        Dim MaxWidth As Integer
        Dim MaxHeight As Integer
        Dim CurrHeight As Integer
        Dim CurrWidth As Integer
        Dim XX As Integer
        Dim pntF As PointF
        Dim szF As SizeF

        Try
            With m_ParentControl
                For XX = 0 To UBound(.Items)
                    If Not .Items(XX).LegendColor.Equals(Color.Empty) Then
                        pntF = New PointF(3 * LG_SIZE, (XX + 1 + 2 * Abs(CShort(.Moveable))) * LG_SIZE)
                    Else
                        pntF = New PointF(LG_SIZE, (XX + 1 + 2 * Abs(CShort(.Moveable))) * LG_SIZE)
                    End If
                    If .FixedWidth <= 0 Then
                        MaxWidth = Max(CInt(e.Graphics.MeasureString(.Items(XX).LegendText, .Items(XX).LegendFont).Width + pntF.X), MaxWidth)
                        CurrWidth = MaxWidth - CInt(pntF.X) + LG_SIZE
                        szF = New SizeF(CurrWidth, e.Graphics.MeasureString(.Items(XX).LegendText, .Items(XX).LegendFont).Height)
                    Else
                        MaxWidth = .FixedWidth
                        CurrWidth = MaxWidth - CInt(pntF.X) + LG_SIZE
                        szF = New SizeF(CSng(CurrWidth), e.Graphics.MeasureString(.Items(XX).LegendText, .Items(XX).LegendFont, CurrWidth).Height)
                    End If
                    If XX <> 0 Then
                        pntF = New PointF(pntF.X, .Items(XX - 1).LegendRect.Bottom + LG_SIZE)
                    End If
                    .Items(XX).LegendRect = New RectangleF(pntF, szF)
                    CurrHeight += CInt(.Items(XX).LegendRect.Height)
                Next
                MaxHeight = (XX + 1 + 2 * Abs(CShort(.Moveable))) * LG_SIZE + CurrHeight
                If Not .Shadowed Then
                    Return New Size(MaxWidth + LG_SIZE, MaxHeight)
                Else
                    Return New Size(MaxWidth + 3 * CInt(LG_SIZE / 2), MaxHeight + CInt(LG_SIZE / 2))
                End If
            End With
        Catch excp As Exception
            Throw excp
        End Try
    End Function


    Private Function BuildShadowed(ByVal e As System.Windows.Forms.PaintEventArgs) As Rectangle
        Dim XX As Byte
        Dim pnts(7) As Point
        Dim tps(7) As Byte
        Dim rcRight As Rectangle
        Dim rcBottom As Rectangle
        Dim pth As Drawing2D.GraphicsPath
        Dim rgn As Region

        Try
            pnts(0).X = 0
            pnts(0).Y = 0
            pnts(1).X = CInt(Me.Width - CInt(LG_SIZE / 2))
            pnts(1).Y = 0
            pnts(2).X = CInt(Me.Width - CInt(LG_SIZE / 2))
            pnts(2).Y = CInt(LG_SIZE / 2)
            pnts(3).X = Me.Width
            pnts(3).Y = CInt(LG_SIZE / 2)
            pnts(4).X = Me.Width
            pnts(4).Y = Me.Height
            pnts(5).X = CInt(LG_SIZE / 2)
            pnts(5).Y = Me.Height
            pnts(6).X = CInt(LG_SIZE / 2)
            pnts(6).Y = Me.Height - CInt(LG_SIZE / 2)
            pnts(7).X = 0
            pnts(7).Y = Me.Height - CInt(LG_SIZE / 2)
            rcRight = New Rectangle(pnts(2), New Size(CInt(LG_SIZE / 2), Me.Height - CInt(LG_SIZE / 2)))
            rcBottom = New Rectangle(pnts(6), New Size(Me.Width - CInt(LG_SIZE / 2), CInt(LG_SIZE / 2)))

            tps(0) = CByte(Drawing.Drawing2D.PathPointType.Start)
            For XX = 1 To 7
                tps(XX) = CByte(Drawing.Drawing2D.PathPointType.Line)
            Next

            pth = New Drawing2D.GraphicsPath(pnts, tps)
            rgn = New Region(pth)
            Me.Region = rgn
            e.Graphics.FillRectangle(Brushes.DimGray, rcRight)
            e.Graphics.FillRectangle(Brushes.DimGray, rcBottom)

            Return New Rectangle(0, 0, Me.Width - CInt(LG_SIZE / 2) - 1, Me.Height - CInt(LG_SIZE / 2) - 1)
        Catch excp As Exception
            Throw excp
        Finally
            pth.Dispose()
            rgn.Dispose()
        End Try
    End Function
#End Region

#Region "Form events"
    Private Sub frmLegend_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub frmLegend_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Location = m_Location
    End Sub

    Private Sub frmLegend_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If m_ParentControl.Moveable Then
            With CaptionRect
                If .Contains(e.X, e.Y) Then
                    If Not CloseRect.Contains(e.X, e.Y) Then
                        Me.Cursor = Cursors.SizeAll
                        If e.Button = MouseButtons.Left Then
                            ReleaseCapture()
                            SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)
                        End If
                    Else
                        Me.Cursor = Cursors.Default
                    End If
                Else
                    Me.Cursor = Cursors.Default
                End If
            End With
        End If
    End Sub

    Private Sub frmLegend_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        If m_ParentControl.Moveable Then
            If CloseRect.Contains(e.X, e.Y) AndAlso e.Button = MouseButtons.Left Then
                Me.Close()
            End If
        End If
    End Sub
#End Region

#Region "Overrides"
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_ACTIVATE
                If m.WParam.ToInt32 = WA_INACTIVE Then
                    Me.Close()
                Else
                    MyBase.WndProc(m)
                End If
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim XX As Integer
        Dim rc As Rectangle
        Dim brRc As SolidBrush
        Dim br As SolidBrush
        Dim pn As Pen
        Dim CurrTop As Integer
        Dim rcShape As Rectangle

        Try
            MyBase.OnPaint(e)
            Me.Size = GetMaxSize(e)
            If m_ParentControl.Shadowed Then
                rcShape = BuildShadowed(e)
            Else
                rcShape = New Rectangle(0, 0, Me.Width - 1, Me.Height - 1)
            End If
            With e.Graphics
                With m_ParentControl
                    CurrTop = (1 + 2 * Abs(CShort(.Moveable))) * LG_SIZE
                    If .Moveable Then
                        CaptionRect = New Rectangle(2, 2, rcShape.Width - 4, 2 * LG_SIZE - 3)
                        If .CaptionBackStyle = agLegend.AGLegendCaptionBackStyle.agOpaque Then
                            e.Graphics.FillRectangle(New SolidBrush(.CaptionBackColor), CaptionRect)
                        End If
                        e.Graphics.DrawRectangle(Pens.Black, CaptionRect)
                        If .CaptionText <> String.Empty Then
                            Dim rcf As New RectangleF(CaptionRect.Left, CaptionRect.Top, CaptionRect.Right, CaptionRect.Bottom)
                            br = New SolidBrush(.CaptionForeColor)
                            e.Graphics.DrawString(.CaptionText, .CaptionFont, br, rcf)
                            br.Dispose()
                        End If
                        With CaptionRect
                            CloseRect = New Rectangle(.Right - LG_SIZE - 5, .Top + 2, LG_SIZE + 3, LG_SIZE + 3)
                        End With
                        pn = New Pen(.CaptionForeColor)
                        e.Graphics.DrawRectangle(pn, CloseRect)
                        With CloseRect
                            e.Graphics.DrawLine(pn, .Left + 2, .Bottom - 2, .Right - 2, .Top + 2)
                            e.Graphics.DrawLine(pn, .Left + 2, .Top + 2, .Right - 2, .Bottom - 2)
                        End With
                        pn.Dispose()
                    End If
                    For XX = 0 To UBound(.Items)
                        If Not .Items(XX).LegendColor.Equals(Color.Empty) Then
                            rc = New Rectangle(LG_SIZE, CInt(.Items(XX).LegendRect.Top), LG_SIZE, LG_SIZE) 'CurrTop, LG_SIZE, LG_SIZE)
                            brRc = New SolidBrush(.Items(XX).LegendColor)
                            Select Case .ShapesStyle
                                Case agLegend.AGLegendShapesStyleConstants.agShapedRectangle
                                    e.Graphics.FillRectangle(brRc, rc)
                                    e.Graphics.DrawRectangle(Pens.Black, rc)
                                Case agLegend.AGLegendShapesStyleConstants.agShapedCircle
                                    e.Graphics.FillEllipse(brRc, rc)
                                    e.Graphics.DrawEllipse(Pens.Black, rc)
                                Case agLegend.AGLegendShapesStyleConstants.agRectangle
                                    e.Graphics.FillRectangle(brRc, rc)
                                Case agLegend.AGLegendShapesStyleConstants.agCircle
                                    e.Graphics.FillEllipse(brRc, rc)
                            End Select
                            brRc.Dispose()
                        End If

                        br = New SolidBrush(.Items(XX).LegendFColor)
                        e.Graphics.DrawString(.Items(XX).LegendText, .Items(XX).LegendFont, br, .Items(XX).LegendRect)
                        br.Dispose()
                        CurrTop += CInt(.Items(XX).LegendRect.Height) + LG_SIZE
                    Next
                End With
                .DrawRectangle(Pens.Black, rcShape)
            End With
        Catch excp As Exception
            Throw excp
        Finally
            e.Graphics.Dispose()
            e.Dispose()
        End Try
    End Sub
#End Region

End Class

Project Homepage: