当前位置:主页   - 电脑 - 网站开发 - ASP.Net
学习ArcEngine(1)-放大工具
来源:网络   作者:liuyh208   更新时间:2011-10-12
收藏此页】    【字号    】    【打印】    【关闭

  地图操作的工具,大部分ArcEngine都有现成的,但是这些工具不太好用,比如提示和名称都是英文,地图操作中没有右键,无法和vs提供的工具条控件或第三方工具条控件集成等,还是自己实现比较灵活方便。

  我们就从最简单的放大工具开始来实现地图操作常用的工具吧!

  废话少说,先上代码

using System;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.ADF.CATIDs;
using System.Runtime.InteropServices;

namespace TechFront.TPIS2.GisCommFun
{
    public class ZoomIn : ICommand, ITool
    {
      
        [DllImport("gdi32.dll")]
        static extern bool DeleteObject(IntPtr hObject);

        private System.Drawing.Bitmap m_bitmap;
        private IntPtr m_hBitmap;
        private IHookHelper m_pHookHelper;
        private INewEnvelopeFeedback m_feedBack;
        private IPoint m_point;
        private Boolean m_isMouseDown;
        private System.Windows.Forms.Cursor m_zoomInCur;
        private System.Windows.Forms.Cursor m_moveZoomInCur;

        public ZoomIn()
        {
            //Load resources
            string[] res = GetType().Assembly.GetManifestResourceNames();
            if (res.GetLength(0) > 0)
            {
                m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), "Bmp.ZoomIn.bmp"));
                if (m_bitmap != null)
                {
                    m_bitmap.MakeTransparent(m_bitmap.GetPixel(1, 1));
                    m_hBitmap = m_bitmap.GetHbitmap();
                }
            }
            m_pHookHelper = new HookHelperClass();
        }

        ~ZoomIn()
        {
            if (m_hBitmap.ToInt32() != 0)
                DeleteObject(m_hBitmap);

            m_pHookHelper = null;
            m_zoomInCur = null;
            m_moveZoomInCur = null;
        }

        #region ICommand Members

        public void OnClick()
        {
        }

        public string Message
        {
            get
            {
                return "放大地图";
            }
        }

        public int Bitmap
        {
            get
            {
                return m_hBitmap.ToInt32();
            }
        }

        public void OnCreate(object hook)
        {
            m_pHookHelper.Hook = hook;
            m_zoomInCur = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream(GetType(), "Cur.ZoomIn.cur"));
            m_moveZoomInCur = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream(GetType(), "Cur.MoveZoomIn.cur"));
        }

        public string Caption
        {
            get
            {
                return "放大";
            }
        }

        public string Tooltip
        {
            get
            {
                return "放大";
"           }
        }

        public int HelpContextID
        {
            get
            {
                // TODO:  Add ZoomIn.HelpContextID getter implementation
                return 0;
            }
        }

        public string Name
        {
            get
            {
                return "ZoomIn";
            }
        }

        public bool Checked
        {
            get
            {
                return false;
            }
        }

        public bool Enabled
        {
            get
            {
                if (m_pHookHelper.FocusMap == null) return false;

                return true;
            }
        }

        public string HelpFile
        {
            get
            {
                // TODO:  Add ZoomIn.HelpFile getter implementation
                return null;
            }
        }

        public string Category
        {
            get
            {
                return "GisCommFun";
            }
        }

        #endregion

        #region ITool Members

        public void OnMouseDown(int button, int shift, int x, int y)
        {
            if (m_pHookHelper.ActiveView == null) return;

            //If the active view is a page layout
            if (m_pHookHelper.ActiveView is IPageLayout)
            {
                //Create a point in map coordinates
                IPoint pPoint = (IPoint)m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);

                //Get the map if the point is within a data frame
                IMap pMap = m_pHookHelper.ActiveView.HitTestMap(pPoint);

                if (pMap == null) return;

                //Set the map to be the page layout's focus map
                if (pMap != m_pHookHelper.FocusMap)
                {
                    m_pHookHelper.ActiveView.FocusMap = pMap;
                    m_pHookHelper.ActiveView.PartialRefresh_PB_PB_PB_PB_PB_PB_PB(esriViewDrawPhase.esriViewGraphics, null, null);
                }
            }
            //Create a point in map coordinates
            IActiveView pActiveView = (IActiveView)m_pHookHelper.FocusMap;
            m_point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y);

            m_isMouseDown = true;

        }

        public void OnMouseMove(int button, int shift, int x, int y)
        {
            if (!m_isMouseDown) return;

            //Get the focus map
            IActiveView pActiveView = (IActiveView)m_pHookHelper.FocusMap;

            //Start an envelope feedback
            if (m_feedBack == null)
            {
                m_feedBack = new NewEnvelopeFeedbackClass();
                m_feedBack.Display = pActiveView.ScreenDisplay;
                m_feedBack.Start(m_point);
            }

            //Move the envelope feedback
            m_feedBack.MoveTo(pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y));
        }

        public void OnMouseUp(int button, int shift, int x, int y)
        {
            if (!m_isMouseDown) return;

            //Get the focus map
            IActiveView pActiveView = (IActiveView)m_pHookHelper.FocusMap;

            //If an envelope has not been tracked
            IEnvelope pEnvelope;

            if (m_feedBack == null)
            {
                //Zoom in from mouse click
                pEnvelope = pActiveView.Extent;
                pEnvelope.Expand(0.5, 0.5, true);
                pEnvelope.CenterAt(m_point);
            }
            else
            {
                //Stop the envelope feedback
                pEnvelope = m_feedBack.Stop();

                //Exit if the envelope height or width is 0
                if (pEnvelope.Width == 0

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明