[ArcEngine地图制图系列]—添加网格

//经纬度网格和索引网格

  //经纬度网格 

   public void CreateGraticuleMapGrid(IActiveView pActiveView, IPageLayout pPageLayout)

        {
            IMap pMap = pActiveView.FocusMap;
            IGraticule pGraticule = new GraticuleClass();
            pGraticule.Name = "MapGrid";

            //设置网格线的符号样式
            ICartographicLineSymbol pLineSymbol;
            
            //pLineSymbol = new CartographicLineSymbolClass();
            //pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
            //pLineSymbol.Width = 1;
            //pLineSymbol.Color = ColorUtilty.GetRgbColor(166, 187, 208);
            pGraticule.LineSymbol = null;

            //设置网格的边框样式           
            ISimpleMapGridBorder simpleMapGridBorder = new SimpleMapGridBorderClass();
            ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
            simpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
            simpleLineSymbol.Color = ColorUtilty.GetRgbColor(100, 255, 0);//亮黄色
            //simpleLineSymbol.Color = ColorUtilty.GetRgbColor(50, 50, 50); //浅黑色
            simpleLineSymbol.Width = 1;
            simpleMapGridBorder.LineSymbol = simpleLineSymbol as ILineSymbol;
            pGraticule.Border = simpleMapGridBorder as IMapGridBorder;
            pGraticule.SetTickVisibility(true, true, true, true);

            //设置网格的主刻度的样式和可见性
            pGraticule.TickLength = 10;
            pLineSymbol = new CartographicLineSymbolClass();
            pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
            pLineSymbol.Width = 1;
            pLineSymbol.Color = ColorUtilty.GetRgbColor(255, 187, 208); //填充颜色
            //pLineSymbol.Color = pageOperater.GetRgbColor(100, 179, 240); //填充颜色
            pGraticule.TickMarkSymbol = null;
            pGraticule.TickLineSymbol = pLineSymbol;
            pGraticule.SetTickVisibility(true, true, true, true);

            //设置网格的次级刻度的样式和可见性
            pGraticule.SubTickCount = 2;
            pGraticule.SubTickLength = 5;
            pLineSymbol = new CartographicLineSymbolClass();
            pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
            pLineSymbol.Width = 0.1;
            pLineSymbol.Color = ColorUtilty.GetRgbColor(166, 187, 208);
            pGraticule.SubTickLineSymbol = pLineSymbol;
            pGraticule.SetSubTickVisibility(true, true, true, true);
            //设置网格的标签的样式和可见性
            IGridLabel pGridLabel;
            pGridLabel = pGraticule.LabelFormat;
            pGridLabel.LabelOffset = 5;
            pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisLeft, false);
            pGridLabel.set_LabelAlignment(esriGridAxisEnum.esriGridAxisRight, false);

            stdole.StdFont pFont = new stdole.StdFont();
            pFont.Name = "Arial";
            pFont.Size = 16;
            pGraticule.LabelFormat.Font = pFont as stdole.IFontDisp;
            pGraticule.Visible = true;
            //创建IMeasuredGrid对象
            IMeasuredGrid pMeasuredGrid = new MeasuredGridClass();
            IProjectedGrid pProjectedGrid = pMeasuredGrid as IProjectedGrid;
            pProjectedGrid.SpatialReference = pMap.SpatialReference;
            pMeasuredGrid = pGraticule as IMeasuredGrid;
            //获取坐标范围,设置网格的起始点和间隔
            double MaxX, MaxY, MinX, MinY;
            pProjectedGrid.SpatialReference.GetDomain(out MinX, out MaxX, out MinY, out MaxY);
            pMeasuredGrid.FixedOrigin = true;
            pMeasuredGrid.Units = pMap.MapUnits;
            pMeasuredGrid.XIntervalSize = (MaxX - MinX) / 8000;//纬度间隔
            pMeasuredGrid.XOrigin = MinX;
            pMeasuredGrid.YIntervalSize = (MaxY - MinY) / 8000;//经度间隔.
            pMeasuredGrid.YOrigin = MinY;
            //将网格对象添加到地图控件中                              
            IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer;
            IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
            IMapGrids pMapGrids = pMapFrame as IMapGrids;

            pMapGrids.AddMapGrid(pGraticule);
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, null);

        }

//索引网格

       public void CreateIndexGrid(IActiveView pActiveView, IPageLayout pPageLayout)
        {
            IIndexGrid pIndexGrid = new IndexGridClass();

            //设置格网样式
            ICartographicLineSymbol pLineSymbol;
            //pLineSymbol = new CartographicLineSymbolClass();
            //pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
            //pLineSymbol.Width = 1;
            //pLineSymbol.Color = ColorUtilty.GetRgbColor(166, 187, 208);
            pIndexGrid.LineSymbol = null ;
            //设置Index属性
            pIndexGrid.ColumnCount = 5;
            pIndexGrid.RowCount = 5;
            String[] indexnum = { "A", "B", "C", "D", "E" };
            //设置IndexLabel
            int i = 0;
            for (i = 0; i <= (pIndexGrid.ColumnCount - 1); i++)
            {
                pIndexGrid.set_XLabel(i, indexnum[i]);
            }
            for (i = 0; i <= (pIndexGrid.RowCount - 1); i++)
            {
                pIndexGrid.set_YLabel(i, (i+1).ToString());
            }
            //设置GridLabel格式
            IGridLabel pGridLabel = new RoundedTabStyleClass();
            StdFont myFont = new stdole.StdFontClass();
            myFont.Name = "宋体";
            myFont.Size = 18;
            pGridLabel.Font = myFont as IFontDisp;
            IMapGrid pmapGrid = new IndexGridClass();
            pmapGrid.Name = "MapGrid";
            pmapGrid = pIndexGrid as IMapGrid;
            pmapGrid.LabelFormat = pGridLabel;

            //添加IndexGrid         
            IMapGrid pMapGrid = pIndexGrid;
            IMap pMap = pActiveView.FocusMap;
            IGraphicsContainer graphicsContainer = pPageLayout as IGraphicsContainer;
            IFrameElement frameElement = graphicsContainer.FindFrame(pMap);
            IMapFrame mapFrame = frameElement as IMapFrame;
            IMapGrids mapGrids = null;
            mapGrids = mapFrame as IMapGrids;
            mapGrids.AddMapGrid(pMapGrid);
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }


你可能感兴趣的:(ArcGISEngine)