ArcObjects:添加图例代码

 

Private Sub showLegend()
        If Not pGroupElement Is Nothing Then
            pGroupElement.ClearElements()
        End If
        Dim graphicsContainer As IGraphicsContainer
        graphicsContainer = frmMain.AxPageLayoutCtl.GraphicsContainer
        Dim pLegend As ILegend
        Dim pLegendItem As ILegendItem
        Dim mapSurround As IMapSurround

        Dim mapFrame As IMapFrame
        mapFrame = graphicsContainer.FindFrame(frmMain.AxPageLayoutCtl.ActiveView.FocusMap)
        If mapFrame Is Nothing Then Exit Sub

        Dim uID As UID = New UIDClass
        uID.Value = "esriCarto.Legend"

        Dim mapSurroundFrame As IMapSurroundFrame
        mapSurroundFrame = mapFrame.CreateSurroundFrame(uID, Nothing)
        If mapSurroundFrame Is Nothing Then Return
        If mapSurroundFrame.MapSurround Is Nothing Then Return

        mapSurroundFrame.MapSurround.Name = "Legend"
        mapSurround = mapSurroundFrame.MapSurround
        pLegend = mapSurround
        pLegend.Title = txtLegendTitle.Text

        Dim pLForm As ILegendFormat
        pLForm = New LegendFormat
        If Not Me.mAreaStyleItem Is Nothing Then
            pLForm.DefaultAreaPatch = Me.mAreaStyleItem.Item
        End If
        If Not Me.mLineStyleItem Is Nothing Then
            pLForm.DefaultLinePatch = Me.mLineStyleItem.Item
        End If
        With pLForm
            .DefaultPatchWidth = CDbl(txtWidth3.Text)
            .DefaultPatchHeight = CDbl(txtHeight3.Text)
            .HeadingGap = CDbl(Me.txtHeadingGap.Text)
            .TitleGap = CDbl(Me.txtTitleGap.Text)
            .TextGap = CDbl(Me.txtTextGap.Text)
            .VerticalPatchGap = CDbl(Me.txtPatch.Text)
            .VerticalItemGap = CDbl(Me.txtVerticalItemGap.Text)
            .HorizontalItemGap = CDbl(Me.txtColumn.Text)
            .HorizontalPatchGap = CDbl(Me.txtPatchLabel.Text)
            If Me.rbtLeft.Checked = True Then
                .TitlePosition = esriRectanglePosition.esriLeftSide
            ElseIf Me.rbtRight.Checked = True Then
                .TitlePosition = esriRectanglePosition.esriRightSide
            End If
        End With
      

        Dim pTextSym As ITextSymbol
        pTextSym = New TextSymbol

        Dim pColor As IRgbColor
        pColor = New RgbColor
        With txtLegendTitle.ForeColor
            pColor.Red = .R
            pColor.Green = .G
            pColor.Blue = .B
        End With
        pTextSym.Color = pColor

        pTextSym.Font = ESRI.ArcGIS.ADF.COMSupport.OLE.GetIFontDispFromFont(txtLegendTitle.Font)
        pLForm.TitleSymbol = pTextSym
        pLegend.Format = pLForm
        pLegend.ClearItems()

        Dim i As Integer
        For i = 0 To lbxLayerLegend.Items.Count - 1
            pLegendItem = New HorizontalLegendItem
            With pLegendItem
                .Columns = Me.nudColumnNum.Value
                Dim temp As String
                temp = lbxLayerLegend.GetItemText(lbxLayerLegend.Items.Item(i))
                Dim j As Integer
                Dim pFeatlyr As IFeatureLayer
                For j = 0 To frmMain.AxPageLayoutCtl.ActiveView.FocusMap.LayerCount - 1
                    pFeatlyr = frmMain.AxPageLayoutCtl.ActiveView.FocusMap.Layer(j)
                    If pFeatlyr.Name = temp Then
                        Exit For
                    End If
                Next
                .Layer = frmMain.AxPageLayoutCtl.ActiveView.FocusMap.Layer(j)
                .ShowDescriptions = True
                .ShowHeading = True
                .ShowLabels = True
                .ShowLayerName = True
            End With
            pLegend.AddItem(pLegendItem)
        Next

        Dim pFrameProp As IFrameProperties
        pFrameProp = mapSurroundFrame
        If Not Me.mFrameStyleItem Is Nothing Then
            pFrameProp.Border = Me.mFrameStyleItem.Item
        End If
        If Not Me.mBackColorStyleItem Is Nothing Then
            pFrameProp.Background = Me.mBackColorStyleItem.Item
        End If
        If Not Me.mShadowStyleItem Is Nothing Then
            pFrameProp.Shadow = Me.mShadowStyleItem.Item
        End If
        Dim envelope As IEnvelope = New EnvelopeClass
        envelope.PutCoords(1, 1, 3.4, 2.4)
        Dim element As IElement
        element = mapSurroundFrame
        element.Geometry = envelope
        pGroupElement.AddElement(element)

        frmMain.AxPageLayoutCtl.AddElement(pGroupElement, Type.Missing, Type.Missing, "Legend", 0)
        frmMain.AxPageLayoutCtl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)

    End Sub

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值