打开地图
#region 打开Mxd文件
if (this.axMapControl1.LayerCount > 0)
{
DialogResult result = MessageBox.Show("是否保存当前地图,", "警告",MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (result == DialogResult.Cancel)
{
return;
}
if (result == DialogResult.Yes)
{
// btnSaveMxd_Click(null,null ); //调用保存文档方法
}
}
OpenFileDialog openFileDialog = new OpenFileDialog();
string filename = string.Empty;
openFileDialog.Title = "Open Map Document";
openFileDialog.Filter = "Map Documents (*.mxd;*.pmf)|*.mxd;*.pmf";
openFileDialog.InitialDirectory = @"C:\Users\TJ\Desktop"; //默认路径
openFileDialog.ShowHelp = true; //现实帮助按钮 有对应的事件“HelpRequest”
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filename))
{
axMapControl1.LoadMxFile(filename);
// 设置MapControl显示范围至数据的全局范围
axMapControl1.Extent = axMapControl1.FullExtent;
}
}
#endregion
自定义工具类,实现添加图名,比例尺,指北针
-------------------------添加图名------------------------------
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.ADF.BaseClasses; using stdole;
//自定义工具类,实现添加图名功能
namespace WindowsApplication1
{
class addPageLayoutName:BaseTool
{
public Form1 formTemp;
TextBox textbox;
AxPageLayoutControl axLayoutControl;
IPoint pPoint;
//double xMap, yMap;
public static double xMap;
public static double yMap;
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
if (Button==1)
{
pPoint =
formTemp.returnPageLayoutControl().ActiveView.ScreenDisplay.DisplayTransformation.T
oMapPoint(X, Y);
xMap = pPoint.X;
yMap = pPoint.Y;
formTemp.returnTextbox1().Location = new System.Drawing.Point(X,Y);
formTemp.returnTextbox1().Visible = true;
formTemp.returnTextbox1().Focus();
formTemp.returnTextbox1().Text = "请在此输入图名";
}
}
public override void OnCreate(object hook)
{
axLayoutControl = hook as AxPageLayoutControl;
}
public void AddTextElement(AxPageLayoutControl PageLayoutControl,double
x,double y,string textName)
{
IPageLayout pPageLayout;
IActiveView pAV;
IGraphicsContainer pGraphicsContainer;
IPoint pPoint;
ITextElement pTextElement;
IElement pElement;
ITextSymbol pTextSymbol;
IRgbColor pColor;
pPageLayout = PageLayoutControl.PageLayout;
pAV = (IActiveView)pPageLayout;
pGraphicsContainer = (IGraphicsContainer)pPageLayout;
pTextElement = new TextElementClass();
IFontDisp pFont = new StdFontClass() as IFontDisp;
pFont.Bold = true;
pFont.Name = "宋体";
pFont.Size = 13;
pColor = new RgbColorClass();
pColor.Red = 255;
pTextSymbol = new TextSymbolClass();
pTextSymbol.Color = (IColor)pColor;
pTextSymbol.Font = pFont;
pTextElement.Text = textName;
pTextElement.Symbol = pTextSymbol;
pPoint = new PointClass();
pPoint.X = x;
pPoint.Y = y;
pElement = (IElement)pTextElement;
pElement.Geometry = (IGeometry)pPoint;
pGraphicsContainer.AddElement(pElement, 0);
pAV.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
}
另外附加其它(添加比例尺,指北针的代码,,,也是通过自定义工具类实现的,,,,下列只给出自定义工具类的代码~)
---------------------------------------添加指北针-------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.ADF.BaseClasses;
namespace WindowsApplication1
{
sealed class addNorthArrow : BaseTool
{
AxPageLayoutControl axPageLayout=null;
IPoint pPoint;
bool bInuse;
INewEnvelopeFeedback pNewEnvelopeFeedback = null;
public addNorthArrow()
{
base.m_caption= "添加指北针";
base.m_toolTip = "添加指北针";
base.m_category = "customCommands";
base.m_message = "添加指北针";
base.m_deactivate = true;
}
public override void OnCreate(object hook)
{
axPageLayout = (AxPageLayoutControl)hook;
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
pPoint =
axPageLayout.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
bInuse = true;
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (bInuse==false)
{
return;
}
if (pNewEnvelopeFeedback==null)
{
pNewEnvelopeFeedback = new NewEnvelopeFeedbackClass();
pNewEnvelopeFeedback.Display = axPageLayout.ActiveView.ScreenDisplay;
pNewEnvelopeFeedback.Start(pPoint);
}
pNewEnvelopeFeedback.MoveTo(axPageLayout.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (bInuse==false)
{
return;
}
if (pNewEnvelopeFeedback==null)
{
pNewEnvelopeFeedback = null;
bInuse = false;
return;
}
IEnvelope pEnvelope=pNewEnvelopeFeedback.Stop();
if((pEnvelope.IsEmpty)||(pEnvelope.Width==0)||(pEnvelope.Height==0))
{
pNewEnvelopeFeedback = null;
bInuse = false;
return;
}
addNorthArrowForm northArrow = new addNorthArrowForm();
IStyleGalleryItem pStyleGalleryItemTemp = Form1.pStyleGalleryItem;
if (pStyleGalleryItemTemp==null)
{
return;
}
IMapFrame pMapframe =
axPageLayout.ActiveView.GraphicsContainer.FindFrame(axPageLayout.ActiveView.FocusMap)as IMapFrame;
IMapSurroundFrame pMapSurroundFrame = new MapSurroundFrameClass();
pMapSurroundFrame.MapFrame = pMapframe;
pMapSurroundFrame.MapSurround = (IMapSurround)pStyleGalleryItemTemp.Item;
//在pageLayout中根据名称查要Element,找到之后删除已经存在的指北针
IElement pElement = axPageLayout.FindElementByName("NorthArrows");
if (pElement!=null)
{
axPageLayout.ActiveView.GraphicsContainer.DeleteElement(pElement); //删
除已经存在的指北针
}
pElement = (IElement)pMapSurroundFrame;
pElement.Geometry = (IGeometry)pEnvelope;
axPageLayout.ActiveView.GraphicsContainer.AddElement(pElement, 0);
axPageLayout.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
pNewEnvelopeFeedback = null;
bInuse = false;
}
}
}
----------------------------------------添加比例尺(Scale Bars)----------------------------------- using System;
using System.Collections.Generic;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.ADF.BaseClasses;
namespace WindowsApplication1
{
sealed class addScaleBar:BaseTool
{
//----------------------------------------------------------
//发现的重大问题,如果用IHookHelper,就会出现问题,用AxPageLayoutControl
则不会出现问题,以后注意
//private IHookHelper pHookHelper=null;
private AxPageLayoutControl axPagelayoutControl = null;
private IPoint pPoint;
private INewEnvelopeFeedback pNewEnvelopeFeedback;
private bool bInuse;
public addScaleBar()
{
base.m_caption = "ScaleBar";
base.m_category = "myCustomCommands(C#)";
base.m_message = "Add a scale bar map surround";
base.m_name = "myCustomCommands(C#)_ScaleBar";
base.m_toolTip = "Add a scale bar";
base.m_deactivate = true;
}
public override void OnCreate(object hook)
{
//pHookHelper.Hook = hook;
axPagelayoutControl = hook as AxPageLayoutControl;
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
pPoint =
axPagelayoutControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
bInuse = true;
}
public override void OnMouseMove(int Button, int Shift, int X, int Y)
{
if (bInuse==false)
{
return;
}
if (pNewEnvelopeFeedback==null)
{
pNewEnvelopeFeedback = new NewEnvelopeFeedbackClass();
pNewEnvelopeFeedback.Display =
axPagelayoutControl.ActiveView.ScreenDisplay;
pNewEnvelopeFeedback.Start(pPoint);
}
pNewEnvelopeFeedback.MoveTo(axPagelayoutControl.ActiveView.ScreenDispla
y.DisplayTransformation.ToMapPoint(X, Y));
}
public override void OnMouseUp(int Button, int Shift, int X, int Y)
{
if (bInuse==false)
{
return;
}
if (pNewEnvelopeFeedback==null)
{
pNewEnvelopeFeedback = null;
bInuse = false;
return;
}
IEnvelope pEnvelope = pNewEnvelopeFeedback.Stop();
if ((pEnvelope.IsEmpty)||(pEnvelope.Width==0)||(pEnvelope.Height==0))
{
pNewEnvelopeFeedback = null;
bInuse = false;
return;
}
AddScaleBarForm scaleBarForm = new AddScaleBarForm();
//scaleBarForm.Show();
IStyleGalleryItem pStyleItem = Form1.pStyleGalleryItem;
if (pStyleItem == null)
{
return;
}
IMapFrame pMapframe =
axPagelayoutControl.ActiveView.GraphicsContainer.FindFrame(axPagelayoutControl.ActiveView.FocusMap) as IMapFrame;
IMapSurroundFrame pSurroundFrame = new MapSurroundFrameClass();
pSurroundFrame.MapFrame = pMapframe;
pSurroundFrame.MapSurround = (IMapSurround)pStyleItem.Item;
//在pageLayout中根据名称查要Element,找到之后删除已经存在的比例尺
IElement pelement = axPagelayoutControl.FindElementByName("ScaleBars");
if (pelement != null)
{
axPagelayoutControl.ActiveView.GraphicsContainer.DeleteElement(pelement); //删除已经存在的指北针
}
pelement = (IElement)pSurroundFrame;
pelement.Geometry = (IGeometry)pEnvelope;
axPagelayoutControl.ActiveView.GraphicsContainer.AddElement(pelement, 0);
axPagelayoutControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
pNewEnvelopeFeedback = null;
bInuse = false;
}
}
}
在地图屏幕上绘制点、线、面符号
上一篇 / 下一篇 2010-06-30 16:02:04 / 个人分类:ArcEngine
查看( 883 ) / 评论( 1 ) / 评分( 0 / 0 ) 有一种方法在地图屏幕上绘制点、线、面符号,当地图刷新一次后就消失了。 用下面的方法不会出现这种情况:
1、创建一个GraphicsLayer,做为绘制符号的容器
2、设置符号属性,并绘制。
下面是绘制点,线以及清除的实例代码:
///
/// 读取或创建指定名称的GraphicsLayer
///
///
当前地图
///
图层名称唯一
///
IGraphicsLayer对象
public static IGraphicsLayer AddOrGetSubGraphicsLayer(IMap map, string subg
raphicsLayername)
{
ICompositeGraphicsLayer pCompositeGLayer = map.BasicGraphicsLayer as IC
ompositeGraphicsLayer;
IGraphicsLayer pGLayer = null;
try
{
//查找是否已存在,如果不存在,跳转到catch内容.
//如果查到不到,说明集合中并没有指定名称的graphicslayer
pGLayer = pCompositeGLayer.FindLayer(subgraphicsLayername);
}
catch
{
//若不存在,则添加一个指定名称的GraphicsLayer
pGLayer = pCompositeGLayer.AddLayer(subgraphicsLayername, null);
}
return pGLayer;
}
///
/// 在当前的地图绘制点
///
///
地图控件
///
点的几何形状
///
点符号
///
点所在的GraphicLayer的名字
///
是否自动刷新
public static void drawPointToGraphicLayer(AxMapControl mapControl, IPoint pGeom, IMarkerSymbol pSym, string GraphicLayerName, bool autoRefesh)
{
try
{
//生成GraphicsLayer-"GX.jpnet_Junctions"
IGraphicsLayer pGraphicsLayer = AddOrGetSubGraphicsLayer(mapControl.Map, GraphicLayerName);
IGraphicsContainer iGC = pGraphicsLayer as IGraphicsContainer;
IElement pEle;
IMarkerElement pLE;
if (pGeom != null)
{
pEle = new MarkerElementClass() as IElement;
pLE = pEle as IMarkerElement;
pLE.Symbol = pSym;
pEle.Geometry = pGeom;
iGC.AddElement(pEle, 0);
}
if (autoRefesh)
mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriView
Graphics, Type.Missing, mapControl.ActiveView.Extent);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
///
/// 在当前的地图绘制点多义线
///
///
地图控件
///
多义线几何形状
///
GraphicLayer的名称
///
是否自动刷新
public static void drawPolylineToGraphicLayer(AxMapControl mapControl, IPol
yline pGeom, string GraphicLayerName,bool autoRefesh)
{
try
{
//生成GraphicsLayer
IGraphicsLayer pGraphicsLayer = AddOrGetSubGraphicsLayer(mapControl.
Map, GraphicLayerName);
IGraphicsContainer iGC = pGraphicsLayer as IGraphicsContainer;
ILineSymbol ipLineSymbol = new CartographicLineSymbolClass();
ipLineSymbol.Width = 2;
IRgbColor pRgbColor = new RgbColorClass();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
ipLineSymbol.Color = pRgbColor as IColor;
IElement pEle;
ILineElement pLE;
if (pGeom != null)
{
pEle = new LineElementClass() as IElement;
pLE = pEle as ILineElement;
pLE.Symbol = ipLineSymbol;
pEle.Geometry = pGeom;
iGC.AddElement(pEle, 0);
}
if(autoRefesh)
mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriView
Graphics, Type.Missing, mapControl.ActiveView.Extent);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
///
/// 清除指定图层上绘制的符号
///
///
public static void ClearDrawingAtGLayer(AxMapControl pMapControl, IGraphicsLayer pGLayer)
{
IGraphicsContainer iGC = pGLayer as IGraphicsContainer;
iGC.DeleteAllElements();
pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Type.Missing, pMapControl.ActiveView.Extent);
pMapControl.ActiveView.Refresh();
}
添加指北针的代码,看到有人问,贴出来大家分享下,呵呵~
比较简单的添加方式,欢迎指教
private void button2_Click(object sender, EventArgs e)
{
UID pUID = new UIDClass();
pUID.Value = "esriCarto.MarkerNorthArrow";
IMarkerSymbol pMarkerSymbol = new ArrowMarkerSymbolClass();
pMarkerSymbol.Size = 20;
IPoint pPoint =
axPageLayoutControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(0, 300);
//IPoint pPoint = new PointClass();
//pPoint.PutCoords(10, 10);
IEnvelope pEnvelope = new EnvelopeClass();
pEnvelope.PutCoords(pPoint.X, pPoint.Y, pPoint.X + pMarkerSymbol.Size, pPoint.Y + pMarkerSymbol.Size);
IMapSurround pMapSurround = CreateSurround(pUID, pEnvelope, null);
IMarkerNorthArrow pMarkerNorthArrow = pMapSurround as IMarkerNorthArrow;
pMarkerNorthArrow.MarkerSymbol = pMarkerSymbol;
INorthArrow pNorthArrow = pMarkerNorthArrow as INorthArrow;
axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
public IMapSurround CreateSurround(UID pUID, IEnvelope pEnvelope, IBorder pMapSurroundFrameBorder)
{
IActiveView pActiveView = axPageLayoutControl1.ActiveView as IActiveView;
IGraphicsContainer pGraphicsContainer = axPageLayoutControl1.PageLayout as IGraphicsContainer;
IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveView.FocusMap) as IMapFrame;
IMapSurroundFrame pMapSurroundFrame = pMapFrame.CreateSurroundFrame(pUID, null);
if (pMapSurroundFrameBorder != null)
{
pMapSurroundFrame.Border = pMapSurroundFrameBorder;
}
IElement pElement = pMapSurroundFrame as IElement;
pElement.Geometry = pEnvelope;
//这两句将要素显示在屏幕上
pElement.Activate(pActiveView.ScreenDisplay);
pElement.Draw(pActiveView.ScreenDisplay, new CancelTrackerClass());
pGraphicsContainer.AddElement(pElement, 0);
IMarkerElement pMkElement = new MarkerElementClass();
pMkElement = pElement as IMarkerElement;
axPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
return pMapSurroundFrame.MapSurround;
}