终于搞得像个样子了.
实现功能:
功能1:加载、保存地图文档
功能2:鹰眼
功能3:添加、删除、移动图层
功能4:专题图
功能5:属性表查看
功能6:地图输出
功能7:设置图层符号
功能8:状态栏信息的添加和实现
开发环境:
Vs2012+arcgisEngine10.2
演示效果:
其中在地图导出的代码,这部分搞了很久:
using System; using System.Drawing; using System.Runtime.InteropServices; using ESRI.ArcGIS.ADF.BaseClasses; using ESRI.ArcGIS.ADF.CATIDs; using ESRI.ArcGIS.Controls; using System.Windows.Forms; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Output; using ESRI.ArcGIS.Display; using System.IO; namespace Cb123456 { /// <summary> /// Command that works in ArcMap/Map/PageLayout /// </summary> [Guid("cc52bbd2-a5a1-4674-86dc-889a15e7878b")] [ClassInterface(ClassInterfaceType.None)] [ProgId("Cb123456.SaveMapPictureCommand")] public sealed class SaveMapPictureCommand : BaseCommand { #region COM Registration Function(s) [ComRegisterFunction()] [ComVisible(false)] static void RegisterFunction(Type registerType) { // Required for ArcGIS Component Category Registrar support ArcGISCategoryRegistration(registerType); // // TODO: Add any COM registration code here // } [ComUnregisterFunction()] [ComVisible(false)] static void UnregisterFunction(Type registerType) { // Required for ArcGIS Component Category Registrar support ArcGISCategoryUnregistration(registerType); // // TODO: Add any COM unregistration code here // } #region ArcGIS Component Category Registrar generated code /// <summary> /// Required method for ArcGIS Component Category registration - /// Do not modify the contents of this method with the code editor. /// </summary> private static void ArcGISCategoryRegistration(Type registerType) { string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID); MxCommands.Register(regKey); ControlsCommands.Register(regKey); } /// <summary> /// Required method for ArcGIS Component Category unregistration - /// Do not modify the contents of this method with the code editor. /// </summary> private static void ArcGISCategoryUnregistration(Type registerType) { string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID); MxCommands.Unregister(regKey); ControlsCommands.Unregister(regKey); } #endregion #endregion private IHookHelper m_hookHelper ; private IMapControl3 m_mapControl; private AxMapControl axMapControl1; public SaveMapPictureCommand(AxMapControl axMapControl1) { // TODO: Complete member initialization this.axMapControl1 = axMapControl1; base.m_category = "导出地图图片"; //localizable text base.m_caption = "导出地图图片"; //localizable text base.m_message = "导出地图图片"; //localizable text base.m_toolTip = "导出地图图片"; //localizable text base.m_name = "SaveMapPictureCommand"; //unique id, non-localizable (e.g. "MyCategory_MyCommand") try { // // TODO: change bitmap name if necessary // string bitmapResourceName = GetType().Name + ".bmp"; base.m_bitmap = new Bitmap(GetType(), bitmapResourceName); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap"); } } #region Overridden Class Methods /// <summary> /// Occurs when this command is created /// </summary> /// <param name="hook">Instance of the application</param> public override void OnCreate(object hook) { if (hook == null) return; try { m_hookHelper = new HookHelperClass(); m_hookHelper.Hook = this.axMapControl1.Object; if (m_hookHelper.ActiveView == null) m_hookHelper = null; } catch { m_hookHelper = null; } if (m_hookHelper == null) base.m_enabled = false; else base.m_enabled = true; // TODO: Add other initialization code if (m_hookHelper == null) return; //get the mapControl hook object hookMap = null; if (m_hookHelper.Hook is IToolbarControl2) hookMap = (m_hookHelper.Hook as IToolbarControl2).Buddy; else hookMap = m_hookHelper.Hook; if (hookMap is IMapControl3) m_mapControl = hookMap as IMapControl3; } /// <summary> /// Occurs when this command is clicked /// </summary> public override void OnClick() { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "jpg文件(*.jpg)|*.jpg|png文件(*.png)|*.png|bmp文件(*.bmp)|*.bmp|pdf文件(*.pfd)|*.pdf|tiff文件(*.tiff)|*.tiff|emf文件(*.emf)|*.emf|gif文件(*.gif)|*.gif|eps文件(*.eps)|*.eps|ai文件(*.ai)|*.ai|svg文件(*.svg)|*.svg|所有文件(*.*)|*.*"; dlg.Title = "导出图片文件"; if (dlg.ShowDialog() == DialogResult.OK) { try { IActiveView pActiveView = m_mapControl.ActiveView; FileInfo fileInfo = new FileInfo(dlg.FileName); string sOutputDir = fileInfo.DirectoryName; string strImageFileName = fileInfo.Name.Split('.')[0]; string ExportType = fileInfo.Extension.Split('.')[1].ToUpper(); IExport docExport = ExportFileType(ExportType); docExport.ExportFileName = sOutputDir + "\\" + strImageFileName + "." + docExport.Filter.Split('.')[1].Split('|')[0].Split(')')[0].ToUpper(); // check if export is vector or raster if (docExport is IOutputRasterSettings) { IOutputRasterSettings RasterSettings; RasterSettings = (IOutputRasterSettings)docExport; RasterSettings.ResampleRatio = 1; } IPrintAndExport docPrintExport = new PrintAndExport(); docPrintExport.Export(pActiveView, docExport, 200, false, null); MessageBox.Show("地图输出成功"); } catch (Exception e) { throw new Exception(Environment.NewLine + "地图输出过程出现错误:" + e.Message); } } } #endregion private static IExport ExportFileType(string ExportType) { IExport docExport; if (ExportType == "PDF") docExport = new ExportPDF() as IExport;// new ExportPDFClass(); else if (ExportType == "EPS") docExport = new ExportPS() as IExport; else if (ExportType == "AI") docExport = new ExportAI() as IExport; else if (ExportType == "BMP") docExport = new ExportBMP() as IExport;// new ExportBMPClass(); else if (ExportType == "TIFF") docExport = new ExportTIFF() as IExport; else if (ExportType == "SVG") docExport = new ExportSVG() as IExport; else if (ExportType == "PNG") docExport = new ExportPNG() as IExport; else if (ExportType == "GIF") docExport = new ExportGIF() as IExport; else if (ExportType == "EMF") docExport = new ExportEMF() as IExport; else { ExportType = "JPG"; docExport = new ExportJPEG() as IExport; } return docExport; } } }
在以上代码中,比较关键的两句:
m_hookHelper = new HookHelperClass();
m_hookHelper.Hook = this.axMapControl1.Object;
之前在测试的时候,出现"未将对象引用设置到对象的实例"的问题,最后用设置断点的方法,发现了这个错误,解决了.
其他的代码见附件:cb123456.rar
参考及引用:
http://www.gissky.net/Article/1554.html
总结:
本次的课程设计终于可以结束了,也学到很多.