IPageLayout 添加 网格

向AxPageLayoutControl的PageLayout中添加格网分为两种情况: 添加经纬网   和   添加公里网。

两种情况的很多代码都是相同的,不过区别的地方也是很关键的。

创建经纬网时,是将IMapGrid的对象赋值给IMeasuredGrid对象。

创建公里网时,是将IMeasuredGrid对象的对象赋值给IMapGrid的对象,同时还要为IMeasuredGrid的对象设置投影属性(IProjectedGrid)。

具体代码:

1、创建经纬网。

         /// <summary>
        /// 为PageLayout对象添加经纬网格
        /// </summary>
        /// <param name="pPageLayout"></param>
        ///
        public  void CreateGraticuleMapGrid(IPageLayout pPageLayout)
        {
            //获取MapFrame对象
            IActiveView pActiveView = pPageLayout as IActiveView;
            IMap pMap = pActiveView.FocusMap;
            IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer;
            IMapFrame. pMapFrame. = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
            IMapGrids pMapGrids = pMapFrame. as IMapGrids;      

            //创建网格对象
            IMapGrid  pMapGrid = new GraticuleClass();// (IGraticule)pServerContext.CreateObject("esriCarto.Graticule");
            pMapGrid.Name = "Map Grid";
         

            //设置网格线的符号样式
            ICartographicLineSymbol pLineSymbol;
            pLineSymbol = new CartographicLineSymbolClass();          
            pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
            pLineSymbol.Width = 1; 
            IRgbColor rgbColor = new RgbColor();
            rgbColor.Red = 166;
            rgbColor.Green = 187;
            rgbColor.Blue = 208;
            IColor color = rgbColor as IColor;
            pLineSymbol.Color = color;
            pMapGrid.LineSymbol = pLineSymbol;

            //设置网格的边框样式,为简单边框样式           
            ISimpleMapGridBorder simpleMapGridBorder = new SimpleMapGridBorderClass();          
            ISimpleLineSymbol simpleLineSymbol = new SimpleLineSymbolClass();
            simpleLineSymbol.Style. = esriSimpleLineStyle.esriSLSSolid;
            simpleLineSymbol.Color = BuildRGB(0, 0, 0);
            simpleLineSymbol.Width = 2;
            simpleMapGridBorder.LineSymbol = simpleLineSymbol as ILineSymbol;         
            pMapGrid.Border = simpleMapGridBorder as IMapGridBorder ;
            pMapGrid.SetTickVisibility(true, true, true, true);

          
            //设置网格的主刻度的样式和可见性
            pMapGrid.TickLength = 15;
            pLineSymbol = new CartographicLineSymbolClass();         
            pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
            pLineSymbol.Width = 1;
            pLineSymbol.Color = color;
            pMapGrid.TickMarkSymbol = null; 
            pMapGrid.TickLineSymbol = pLineSymbol;
            pMapGrid.SetTickVisibility(true, true, true, true);

            //设置网格的次一级刻度的样式和可见性
            pMapGrid.SubTickCount = 5;
            pMapGrid.SubTickLength = 10;
            pLineSymbol = new CartographicLineSymbolClass();         
            pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
            pLineSymbol.Width = 0.1;
            pLineSymbol.Color = color;
            pMapGrid.SubTickLineSymbol = pLineSymbol;       
            pMapGrid.SetSubTickVisibility(true, true, true, true);
        
            //设置网格的标签的样式和可见性
            IGridLabel pGridLabel;
            pGridLabel = pMapGrid.LabelFormat;
            pGridLabel.LabelOffset = 15;

           

            stdole.StdFont pFont = new stdole.StdFont();
            pFont.Name = "Arial";
            pFont.Size = 16;

           pMapGrid.LabelFormat.Font = pFont as stdole.IFontDisp;

 


             //设置网格的可见性
            pMapGrid.Visible = true;

            //创建IMeasuredGrid对象
            IMeasuredGrid pMeasuredGrid;
            pMeasuredGrid = pMapGrid as IMeasuredGrid;
            pMeasuredGrid.FixedOrigin = true;
            pMeasuredGrid.XIntervalSize = 0.5; //meridian interval
            pMeasuredGrid.XOrigin = 109; //shift grid 5
            pMeasuredGrid.YIntervalSize = 0.5; //parallel interval
            pMeasuredGrid.YOrigin = 40; //shift grid 5
          
            //将网格对象添加到地图控件中    
            pMapGrids.AddMapGrid(pMapGrid);

            //刷新地图
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, null);
        }

2、创建公里网。

    public void CreateMeasuredGrid(IPageLayout pPageLayout)
        {
            try
            {
                //获取MapFrame对象
                IActiveView pActiveView = pPageLayout as IActiveView;
                IMap pMap = pActiveView.FocusMap;
                IGraphicsContainer pGraphicsContainer = pActiveView as IGraphicsContainer;
                IMapFrame. pMapFrame. = pGraphicsContainer.FindFrame(pMap) as IMapFrame;
                IMapGrids pMapGrids = pMapFrame. as IMapGrids; 


                //创建MeasuredGrid对象            
                IMeasuredGrid pMeasuredGrid = new MeasuredGrid() as IMeasuredGrid;


                //设置pMeasuredGrid的投影属性
                IProjectedGrid pProjectedGrid = (IProjectedGrid)pMeasuredGrid;
                pProjectedGrid.SpatialReference = Map.SpatialReference;
                double MaxX,MaxY,MinX,MinY;//动态获取坐标系中的坐标范围,以便设置网格的起始点和间隔
                pProjectedGrid.SpatialReference.GetDomain(out MinX, out MaxX , out MinY , out MaxY );
                pMeasuredGrid.FixedOrigin = true;
                pMeasuredGrid.Units = Map.MapUnits;
                pMeasuredGrid.XIntervalSize = (MaxX-MinX)/20;
                pMeasuredGrid.XOrigin = MinX;
                pMeasuredGrid.YIntervalSize = (MaxY - MinY)/20 ;
                pMeasuredGrid.YOrigin = MinY;

                //将pMeasuredGrid赋值个IMapGrid对象
                IMapGrid pMapGrid = (IMapGrid)pMeasuredGrid;
                pMapGrid.Name = "Measureed Grid";


                //设置网格线的符号样式
                ICartographicLineSymbol pLineSymbol;
                pLineSymbol = new CartographicLineSymbolClass();
                pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
                pLineSymbol.Width = 1;
                IRgbColor rgbColor = new RgbColor();
                rgbColor.Red = 166;
                rgbColor.Green = 187;
                rgbColor.Blue = 208;
                IColor color = rgbColor as IColor;
                pLineSymbol.Color = color;
                pMapGrid.LineSymbol = pLineSymbol;


                //设置ICalibratedMapGridBorder的属性
                ICalibratedMapGridBorder pCalibratedBorder = (ICalibratedMapGridBorder)new CalibratedMapGridBorder();
                IRgbColor gCol = new RgbColor();
                gCol.Red = 255;
                gCol.Blue = 255;
                gCol.Green = 255;
                pCalibratedBorder.BackgroundColor = gCol;
                IRgbColor ForeColor = new RgbColor();
                ForeColor.Red = 0;
                ForeColor.Blue = 0;
                ForeColor.Green = 0;
                pCalibratedBorder.ForegroundColor = ForeColor;
                pCalibratedBorder.BorderWidth = 2;
                pCalibratedBorder.Interval = 50;
                pCalibratedBorder.Alternating = true;
                pMapGrid.Border = (IMapGridBorder)pCalibratedBorder;


                //设置网格的主刻度的样式和可见性
                pMapGrid.TickLength = 15;
                pLineSymbol = new CartographicLineSymbolClass();
                pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
                pLineSymbol.Width = 1;
                pLineSymbol.Color = color;
                pMapGrid.TickMarkSymbol = null;
                pMapGrid.TickLineSymbol = pLineSymbol;
                pMapGrid.SetTickVisibility(true, true, true, true);

                //设置网格的次一级刻度的样式和可见性
                pMapGrid.SubTickCount = 5;
                pMapGrid.SubTickLength = 10;
                pLineSymbol = new CartographicLineSymbolClass();
                pLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
                pLineSymbol.Width = 0.1;
                pLineSymbol.Color = color;
                pMapGrid.SubTickLineSymbol = pLineSymbol;
                pMapGrid.SetSubTickVisibility(true, true, true, true);

                //标签格式设置IGridLabel
                IGridLabel pGridLabel;
                pGridLabel = pMapGrid.LabelFormat;
                pGridLabel.LabelOffset = 15;

                stdole.StdFont pFont = new stdole.StdFont();
                pFont.Name = "Arial";
                pFont.Size = 16;
                pMapGrid.LabelFormat.Font = pFont as stdole.IFontDisp;
     
                //设置网格的可见性
                pMapGrid.Visible = true;

                //添加网格对象到地图控件中      
                pMapGrids.AddMapGrid(pMapGrid);

                //刷新地图
                pActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, null);
            }
            catch (Exception EX)
            {
                throw EX;
            }
 
        }

 

posted @ 2010-09-21 13:49  淡茶gis  阅读(1014)  评论(1编辑  收藏  举报