ArcObjects Library Reference  

Pan

About the Custom map navigation commands Sample

[C#]

Pan.cs

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 PanZoom
{
	[ClassInterface(ClassInterfaceType.None)]
	[Guid("4BD2DDAE-AA6F-4718-AC2E-F39C5618895C")]

	public class Pan :  ICommand, ITool
	{
        #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);
            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);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

		[DllImport("gdi32.dll")]
		static extern bool DeleteObject(IntPtr hObject);

		private System.Drawing.Bitmap m_bitmap;
		private IntPtr m_hBitmap;
		private IHookHelper m_pHookHelper;
		private bool m_enabled;
		private IScreenDisplay m_focusScreenDisplay;
		private bool m_PanOperation;
		private bool m_check;
		private System.Windows.Forms.Cursor m_cursor;
		private System.Windows.Forms.Cursor m_cursorMove;
				
		public Pan()
		{
			//Load resources
			string[] res = GetType().Assembly.GetManifestResourceNames();
			if(res.GetLength(0) > 0)
			{
				m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), "Pan.bmp"));
				if(m_bitmap != null)
				{
					m_bitmap.MakeTransparent(m_bitmap.GetPixel(1,1));
					m_hBitmap = m_bitmap.GetHbitmap();
				}
			}
			m_pHookHelper = new HookHelperClass ();
		}

		~Pan()
		{
			if(m_hBitmap.ToInt32() != 0)
				DeleteObject(m_hBitmap);
		}
	   
		#region ICommand Members

		public void OnClick()
		{
		}

		public string Message
		{
			get
			{
				return "Pans the Display by Grabbing";
			}
		}

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

		public void OnCreate(object hook)
		{
			m_pHookHelper.Hook = hook;
            m_enabled = true;
			m_check = false;
			m_cursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream (GetType(), "Hand.cur"));
			m_cursorMove = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream(GetType(), "MoveHand.cur"));
		}

		public string Caption
		{
			get
			{
				return "Pan";
			}
		}

		public string Tooltip
		{
			get
			{
				return "Pan by Grab";
			}
		}

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

		public string Name
		{
			get
			{
				return "Sample_Pan/Zoom_Pan";
			}
		}

		public bool Checked
		{
			get
			{
				return m_check;
			}
		}

		public bool Enabled
		{
			get
			{
				return m_enabled;
			}
		}

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

		public string Category
		{
			get
			{
				return "Sample_Pan/Zoom";
			}
		}

		#endregion
	
		#region ITool Members

		public void OnMouseDown(int button, int shift, int x, int y)
		{
			if (button != 1) return;

			IActiveView activeView = m_pHookHelper.ActiveView;
			IPoint point;

			//If in PageLayout view, find the closest map
			if(!(activeView is IMap))
			{
				point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(x,y);
				IMap hitMap = activeView.HitTestMap(point);

				//Exit if no map found
				if(hitMap == null)
					return;
	
				if(activeView != m_pHookHelper.FocusMap)
					activeView.FocusMap = hitMap;
			}

			//Start the pan operation
			m_focusScreenDisplay = getFocusDisplay();
			point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(x,y);
			m_focusScreenDisplay.PanStart(point);
			m_PanOperation = true;
		}

        private IScreenDisplay getFocusDisplay()
        {
            //Get the ScreenDisplay that has focus
            IActiveView activeView = m_pHookHelper.ActiveView;

            activeView = m_pHookHelper.ActiveView.FocusMap as IActiveView; //layout view
            return activeView.ScreenDisplay;

        }

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

			if(! m_PanOperation) return;

			IPoint point = m_focusScreenDisplay.DisplayTransformation.ToMapPoint(x,y);
			m_focusScreenDisplay.PanMoveTo(point);
		}
		
		public void OnMouseUp(int button, int shift, int x, int y)
		{
			if(button != 1) return;

			if(! m_PanOperation) return;

			IEnvelope extent = m_focusScreenDisplay.PanStop();

			//Check if user dragged a rectangle or just clicked.
			//If a rectangle was dragged, m_ipFeedback in non-NULL
			if(extent != null)
			{
				m_focusScreenDisplay.DisplayTransformation.VisibleBounds = extent;
				m_focusScreenDisplay.Invalidate(null, true, (short)esriScreenCache.esriAllScreenCaches);
			}

			m_PanOperation = false;
		}

		public void OnKeyDown(int keyCode, int shift)
		{
			// TODO:  Add Pan.OnKeyDown implementation
		}

		public void OnKeyUp(int keyCode, int shift)
		{
			// TODO:  Add Pan.OnKeyUp implementation
		}

		public int Cursor
		{
			get
			{
				if(m_PanOperation)
                    return m_cursorMove.Handle.ToInt32();
				else
					return m_cursor.Handle.ToInt32();
			}
		}

		public bool OnContextMenu(int x, int y)
		{
			// TODO:  Add Pan.OnContextMenu implementation
			return false;
		}

		public bool Deactivate()
		{
			// TODO:  Add Pan.Deactivate implementation
			return true;
		}

		public void Refresh(int hdc)
		{
			// TODO:  Add Pan.Refresh implementation
		}

		public void OnDblClick()
		{
			// TODO:  Add Pan.OnDblClick implementation
		}

		#endregion
	}
}

[Visual Basic .NET]

Pan.vb

Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.GeomeTry
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports System.Runtime.InteropServices

<ComClass(Pan.ClassId, Pan.InterfaceId, Pan.EventsId)> _
Public NotInheritable Class Pan
    Inherits BaseTool

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class 
    ' and its COM interfaces. If you change them, existing 
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "64653147-697B-4B0B-B728-9529BA8A2AAF"
    Public Const InterfaceId As String = "1B28A647-9A8B-4F92-A0C8-AFE121C847E1"
    Public Const EventsId As String = "E35275E7-89D3-43E1-96B8-C2088CE7C7AE"
#End Region
#Region "COM Registration Function(s)"
    <ComRegisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub RegisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryRegistration(registerType)

        'Add any COM registration code after the ArcGISCategoryRegistration() call

    End Sub

    <ComUnregisterFunction(), ComVisibleAttribute(False)> _
    Public Shared Sub UnregisterFunction(ByVal registerType As Type)
        ' Required for ArcGIS Component Category Registrar support
        ArcGISCategoryUnregistration(registerType)

        'Add any COM unregistration code after the ArcGISCategoryUnregistration() call

    End Sub

#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 Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        ControlsCommands.Register(regKey)

    End Sub
    ''' <summary>
    ''' Required method for ArcGIS Component Category unregistration -
    ''' Do not modify the contents of this method with the code editor.
    ''' </summary>
    Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
        Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
        ControlsCommands.Unregister(regKey)

    End Sub

#End Region
#End Region

    Private m_pHookHelper As IHookHelper
    Private m_pEnabled As Boolean
    Private m_focusScreenDisplay As IScreenDisplay
    Private m_PanOperation As Boolean
    Private m_check As Boolean
    Private m_cursorStart As System.Windows.Forms.Cursor
    Private m_cursorMove As System.Windows.Forms.Cursor

    ' A creatable COM class must have a Public Sub New() 
    ' with no parameters, otherwise, the class will not be 
    ' registered in the COM registry and cannot be created 
    ' via CreateObject.
    Public Sub New()
        MyBase.New()

        MyBase.m_category = "Sample_Pan_VBNET/Zoom"
        MyBase.m_caption = "Pan"
        MyBase.m_message = "Pans The Display by Grabbing"
        MyBase.m_toolTip = "Pan by Grab"
        MyBase.m_name = "Sample_Pan/Zoom_Pan"

        Dim res() As String = GetType(Pan).Assembly.GetManifestResourceNames()
        If res.GetLength(0) > 0 Then
            MyBase.m_bitmap = New System.Drawing.Bitmap(GetType(Pan).Assembly.GetManifestResourceStream("PanZoomVBNET.Pan.bmp"))
        End If
        m_pHookHelper = New HookHelperClass
    End Sub

    Public Overrides Sub OnCreate(ByVal hook As Object)
        m_pHookHelper.Hook = hook
        m_pEnabled = True
        m_check = False
        m_cursorStart = New System.Windows.Forms.Cursor(GetType(Pan).Assembly.GetManifestResourceStream("PanZoomVBNET.Hand.cur"))
        m_cursorMove = New System.Windows.Forms.Cursor(GetType(Pan).Assembly.GetManifestResourceStream("PanZoomVBNET.MoveHand.cur"))
    End Sub

    Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)

        If Button <> 1 Then
            Return
        End If

        Dim activeView As IActiveView = m_pHookHelper.ActiveView
        Dim point As IPoint
        'If in PageLayout view, find the closest map
        If Not TypeOf activeView Is IMap Then
            point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)
            Dim hitMap As IMap = activeView.HitTestMap(point)

            'Exit if no map found
            If hitMap Is Nothing Then
                Return
            End If

            If Not activeView Is m_pHookHelper.FocusMap Then
                activeView.FocusMap = hitMap
            End If

        End If
        'Start the pan operation
        point = m_pHookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)
        m_focusScreenDisplay = getFocusDisplay()
        m_focusScreenDisplay.PanStart(point)

        m_PanOperation = True
    End Sub

    Private Function getFocusDisplay() As IScreenDisplay
        'Get the ScreenDisplay that has focus
        Dim activeView As IActiveView = m_pHookHelper.ActiveView

        activeView = CType(m_pHookHelper.ActiveView.FocusMap, IActiveView)  'layout view
        Return activeView.ScreenDisplay

    End Function

    Public Overrides Sub OnMouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)

        If Button <> 1 Then
            Return
        End If

        If Not m_PanOperation Then
            Return
        End If

        Dim point As IPoint = m_focusScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)
        m_focusScreenDisplay.PanMoveTo(point)
    End Sub

    Public Overrides Sub OnMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)

        If Button <> 1 Then
            Return
        End If

        If Not m_PanOperation Then
            Return
        End If

        Dim extent As IEnvelope = m_focusScreenDisplay.PanStop()

        'Check if user dragged a rectangle or just clicked.
        'If a rectangle was dragged, m_ipFeedback in non-NULL
        If Not extent Is Nothing Then
            m_focusScreenDisplay.DisplayTransformation.VisibleBounds = extent
            m_focusScreenDisplay.Invalidate(Nothing, True, CType(esriScreenCache.esriAllScreenCaches, Short))
        End If

        m_PanOperation = False
    End Sub

    Public Overrides Function Deactivate() As Boolean
        Return True
    End Function

    Public Overrides ReadOnly Property Cursor() As Integer
        Get
            If (m_PanOperation) Then
                Return m_cursorMove.Handle.ToInt32()
            Else
                Return m_cursorStart.Handle.ToInt32()
            End If
        End Get
    End Property
End Class