ArcObjects Library Reference  

ZoomOut

About the Custom scene navigation commands Sample

[C#]

ZoomOut.cs

using System;
using System.Drawing;
using ESRI.ArcGIS.Analyst3D;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;

namespace sceneTools
{
	[ClassInterface(ClassInterfaceType.None)]
	[Guid("3D3C8C68-7B65-4441-B614-792D5606FF8D")]

	public sealed class ZoomOut : BaseTool
	{
		[DllImport("user32")] public static extern int ReleaseCapture(int hwnd); 
		[DllImport("user32")] public static extern int SetCapture(int hwnd); 
		[DllImport("user32")] public static extern int GetCapture(int fuFlags);
		[DllImport("user32")] public static extern int GetWindowRect(int hwnd, ref Rectangle lpRect);

        #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

		private System.Windows.Forms.Cursor m_pCursor;
		private ISceneHookHelper m_pSceneHookHelper;
		private long m_lMouseX, m_lMouseY;
		private bool m_bInUse;
        private INewEnvelopeFeedback m_feedBack;
        private IPoint m_point;

		public ZoomOut()
		{
			base.m_category = "Sample_SceneControl(C#)";
			base.m_caption = "Zoom Out";
			base.m_toolTip = "Zoom Out";
			base.m_name = "Sample_SceneControl(C#)/Zoom Out";
			base.m_message = "Zooms in out the scene";

			//Load resources
			string[] res = GetType().Assembly.GetManifestResourceNames();
			if(res.GetLength(0) > 0)
			{
				base.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream("sceneTools.zoomout.bmp"));
			}
			m_pCursor = new System.Windows.Forms.Cursor(GetType().Assembly.GetManifestResourceStream("sceneTools.ZOOMOUT.CUR"));
		
			m_pSceneHookHelper = new SceneHookHelperClass ();
		}

		public override bool Enabled
		{
			get
			{
				if(m_pSceneHookHelper.Scene == null)
					return false;
				else
					return true;
			}
		}

		public override void OnCreate(object hook)
		{
			m_pSceneHookHelper.Hook = hook;
		}

		public override int Cursor
		{
			get
			{
				return m_pCursor.Handle.ToInt32();
			}
		}

		public override bool Deactivate()
		{
			return true;
		}

		public override void OnKeyDown(int keyCode, int Shift)
		{
			if(m_bInUse == true)
			{
				if(keyCode == 27) //If ESC was pressed 
				{
					//Redraw the scene viewer
					ISceneViewer pSceneViewer = (ISceneViewer) m_pSceneHookHelper.ActiveViewer;
					pSceneViewer.Redraw(true);

					ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd);

					m_bInUse = false;
				}
			}
		}

		public override void OnMouseDown(int Button, int Shift, int X, int Y)
		{
			//Initialize mouse coordinates
			m_bInUse = true;
			m_lMouseX = X;
			m_lMouseY = Y;
            
			//Get the scene viewer
			ISceneViewer pSceneViewer = (ISceneViewer) m_pSceneHookHelper.ActiveViewer;

			SetCapture(m_pSceneHookHelper.ActiveViewer.hWnd);

            //Create a point in map coordinates
            IActiveView pActiveView = (IActiveView)m_pSceneHookHelper.Scene;
            m_point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);

		}

		public override void OnMouseMove(int Button, int Shift, int X, int Y)
		{
			if(!m_bInUse) return;

            //Get the focus map
            IActiveView pActiveView = (IActiveView)m_pSceneHookHelper.Scene;

            //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 override void OnMouseUp(int Button, int Shift, int X, int Y)
		{
			if(!m_bInUse) return;

            try
            {
                if (GetCapture(m_pSceneHookHelper.ActiveViewer.hWnd) != 0)
                    ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd);

                //Get the scene viewer's camera
                ICamera pCamera = (ICamera)m_pSceneHookHelper.Camera;

                //Get the scene graph
                ISceneGraph pSceneGraph = (ISceneGraph)m_pSceneHookHelper.SceneGraph;

                //Create envelope
                IEnvelope pEnvelope;
                CreateEnvelope(X, Y, out pEnvelope);

                IPoint pPoint;
                object pOwner, pObject;

                if (pEnvelope.Width == 0 || pEnvelope.Height == 0)
                {
                    //Translate screen coordinates into a 3D point
                    pSceneGraph.Locate(pSceneGraph.ActiveViewer, X, Y, esriScenePickMode.esriScenePickAll, true,
                        out pPoint, out pOwner, out pObject);

                    //Set camera target and zoom in
                    pCamera.Target = pPoint;
                    pCamera.Zoom(1.3333333333333);
                }
                else
                {
                    //Get dimension of the scene viewer window
                    Rectangle rect;
                    rect = new Rectangle();

                    if (GetWindowRect(m_pSceneHookHelper.ActiveViewer.hWnd, ref rect) == 0) return;

                    //If perspective (3D) view
                    if (pCamera.ProjectionType == esri3DProjectionType.esriPerspectiveProjection)
                    {
                        double dWidth, dHeight;

                        dWidth = Math.Abs(rect.Right - rect.Left) * (Math.Abs(rect.Right - rect.Left) / pEnvelope.Width);
                        dHeight = Math.Abs(rect.Top - rect.Bottom) * (Math.Abs(rect.Top - rect.Bottom) / pEnvelope.Height);

                        pPoint = new PointClass();
                        pPoint.PutCoords(pEnvelope.XMin + (pEnvelope.Width / 2), pEnvelope.YMin + (pEnvelope.Height / 2));

                        //Redimension envelope based on scene viewer dimensions
                        pEnvelope.XMin = pPoint.X - (dWidth / 2);
                        pEnvelope.YMin = pPoint.Y - (dHeight / 2);
                        pEnvelope.Width = dWidth;
                        pEnvelope.Height = dHeight;

                        //Zoom camera to the envelope
                        pCamera.ZoomToRect(pEnvelope);
                    }
                    else
                    {
                        //Translate screen coordinates into a 3D point
                        pSceneGraph.Locate(pSceneGraph.ActiveViewer, (int)(pEnvelope.XMin + (pEnvelope.Width / 2)),
                            (int)(pEnvelope.YMin + (pEnvelope.Height / 2)), esriScenePickMode.esriScenePickAll, true,
                            out pPoint, out pOwner, out pObject);

                        //Set camera target
                        pCamera.Target = pPoint;

                        double dx, dy;
                        dx = pEnvelope.Width;
                        dy = pEnvelope.Height;

                        //Determine zoom factor
                        if (dx > 0 && dy > 0)
                        {
                            dx = Math.Abs(rect.Right - rect.Left) / dx;
                            dy = Math.Abs(rect.Top - rect.Bottom) / dy;

                            if (dx < dy)
                                pCamera.Zoom(dx);
                            else
                                pCamera.Zoom(dy);
                        }
                        else
                            pCamera.Zoom(1.3333333333333);
                    }
                }
            }
            finally
            {
                //Redraw the scene viewer
                ISceneViewer pSceneViewer = (ISceneViewer)m_pSceneHookHelper.ActiveViewer;
                pSceneViewer.Redraw(true);


                m_bInUse = false;
            }
		}

		public void CreateEnvelope(int X, int Y, out IEnvelope pEnvelope)
		{
			//Create envelope based upon the initial
			//and current mouse coordinates
			pEnvelope = new EnvelopeClass();
			if((double)m_lMouseX <= (double)X)
			{
				pEnvelope.XMin = (double) m_lMouseX;
				pEnvelope.XMax = (double) X;
			}
			else
			{
				pEnvelope.XMin = (double) X;
				pEnvelope.XMax = (double) m_lMouseX;
			}

			if((double) m_lMouseY <= (double) Y)
			{
				pEnvelope.YMin = (double) m_lMouseY;
				pEnvelope.YMax = (double) Y;
			}
			else
			{
				pEnvelope.YMin = (double) Y;
				pEnvelope.YMax = (double) m_lMouseY;
			}
		}

		public void DrawRectangle(IEnvelope pEnvelope)
		{
			//Get the scene viewer
			ISceneViewer pSceneViewer = (ISceneViewer) m_pSceneHookHelper.ActiveViewer;

            using (Graphics myGraphics = Graphics.FromHdc((IntPtr)m_pSceneHookHelper.ActiveViewer.hDC))
            {
                using (Brush brush = new SolidBrush(Color.Transparent))//hollow brush
                {
                    //GDI+ call to fill a rectangle with a hollow brush
                    myGraphics.FillRectangle(brush, (int)pEnvelope.XMin, (int)pEnvelope.YMin,
                        (int)pEnvelope.Width, (int)pEnvelope.Height);
                }

                using (Pen pen = new System.Drawing.Pen(Color.Black, 2)) //A solid, width of 2 black pen
                {
                    //GDI+ call to draw a rectangle with a specified pen 
                    myGraphics.DrawRectangle(pen, (int)pEnvelope.XMin, (int)pEnvelope.YMin,
                        (int)pEnvelope.Width, (int)pEnvelope.Height);
                }
            }

		}
	}
}

[Visual Basic .NET]

ZoomOut.vb

Imports System.Drawing
Imports ESRI.ArcGIS.Analyst3D
Imports ESRI.ArcGIS.Controls
Imports ESRI.ArcGIS.GeomeTry
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports System.Runtime.InteropServices

<ComClass(ZoomOut.ClassId, ZoomOut.InterfaceId, ZoomOut.EventsId)> _
Public NotInheritable Class ZoomOut
    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 = "5D7FE858-DDFD-4E6E-ABBD-7FE29EF5F654"
    Public Const InterfaceId As String = "67151CED-047B-436C-9306-06C05C621047"
    Public Const EventsId As String = "AF100AC6-F570-4C3E-86BA-278BA8F9E02B"
#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

    Declare Function GetCapture Lib "user32" (ByVal fuFlags As Integer) As Integer
    Declare Function SetCapture Lib "user32" (ByVal hwnd As Integer) As Integer
    Declare Function ReleaseCapture Lib "user32" (ByVal hwnd As Integer) As Integer
    Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Integer, ByRef lpRect As Rectangle) As Integer

    Private m_pCursor As System.Windows.Forms.Cursor
    Private m_pSceneHookHelper As ISceneHookHelper
    Private m_lMouseX, m_lMouseY As Long
    Private m_bInUse As Boolean
    Private m_pen As Pen
    Private m_brush As Brush
    Private myGraphics As Graphics

  ' 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_SceneControl(VB.NET)"
        MyBase.m_caption = "Zoom Out"
        MyBase.m_toolTip = "Zoom Out"
        MyBase.m_name = "Sample_SceneControl(VB.NET)/Zoom Out"
        MyBase.m_message = "Zooms in out the scene"

        'Load resources
        Dim res() As String = GetType(ZoomOut).Assembly.GetManifestResourceNames()
        If res.GetLength(0) > 0 Then
            MyBase.m_bitmap = New System.Drawing.Bitmap(GetType(ZoomOut).Assembly.GetManifestResourceStream("SceneToolsVB.zoomout.bmp"))
        End If
        m_pCursor = New System.Windows.Forms.Cursor(GetType(ZoomOut).Assembly.GetManifestResourceStream("SceneToolsVB.ZOOMOUT.CUR"))

        m_pSceneHookHelper = New SceneHookHelperClass
    End Sub

    Public Overrides Sub OnCreate(ByVal hook As Object)
        m_pSceneHookHelper.Hook = hook
        Dim ptrHDC As IntPtr
        ptrHDC = New IntPtr(m_pSceneHookHelper.ActiveViewer.hDC)
        myGraphics = Graphics.FromHdc(ptrHDC)
        m_brush = New SolidBrush(Color.Transparent) 'hollow brush
        m_pen = New System.Drawing.Pen(Color.Black, 2)  'A solid, width of 2 black pen
    End Sub

    Public Overrides ReadOnly Property Enabled() As Boolean
        Get
            If (m_pSceneHookHelper.Scene Is Nothing) Then
                Return False
            Else
                Return True
            End If
        End Get
    End Property

    Public Overrides ReadOnly Property Cursor() As Integer
        Get
            Return m_pCursor.Handle.ToInt32()
        End Get
    End Property

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

    Public Overrides Sub OnKeyDown(ByVal keyCode As Integer, ByVal Shift As Integer)
        If (m_bInUse = True) Then
            If (keyCode = 27) Then 'If ESC was pressed 
                'Redraw the scene viewer
                Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer)

                pSceneViewer.Redraw(True)
                ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd)

                m_bInUse = False
            End If
        End If
    End Sub

    Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
        'Initialize mouse coordinates
        m_bInUse = True
        m_lMouseX = X
        m_lMouseY = Y

        Dim pEnvelope As IEnvelope = Nothing

        'Initialize(Envelope)
        CreateEnvelope(X, Y, pEnvelope)

        'Get the scene viewer
        Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer)

        SetCapture(m_pSceneHookHelper.ActiveViewer.hWnd)
    End Sub

    Public Overrides Sub OnMouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
        If Not m_bInUse Then
            Return
        End If

        Dim pEnvelope As IEnvelope = Nothing

        'Draw rectangle on the device
        CreateEnvelope(X, Y, pEnvelope)
        DrawRectangle(pEnvelope)
    End Sub

    Public Overrides Sub OnMouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
        If Not m_bInUse Then
            Return
        End If

        If (GetCapture(m_pSceneHookHelper.ActiveViewer.hWnd) <> 0) Then
            ReleaseCapture(m_pSceneHookHelper.ActiveViewer.hWnd)
        End If

        'Get the scene viewer's camera
        Dim pCamera As ICamera = CType(m_pSceneHookHelper.Camera, ICamera)

        'Get the scene graph
        Dim pSceneGraph As ISceneGraph = CType(m_pSceneHookHelper.SceneGraph, ISceneGraph)

        'Create(Envelope)
        Dim pEnvelope As IEnvelope = Nothing
        CreateEnvelope(X, Y, pEnvelope)

        Dim pPoint As IPoint = Nothing
        Dim pOwner As Object = Nothing, pObject As Object = Nothing

        If (pEnvelope.Width = 0 Or pEnvelope.Height = 0) Then
            'Translate screen coordinates into a 3D point
            pSceneGraph.Locate(pSceneGraph.ActiveViewer, X, Y, esriScenePickMode.esriScenePickAll, True, pPoint, pOwner, pObject)

            'Set camera target and zoom in
            pCamera.Target = pPoint
            pCamera.Zoom(1.3333333333333)
        Else
            'Get dimension of the scene viewer window
            Dim rect As Rectangle
            rect = New Rectangle

            If (GetWindowRect(m_pSceneHookHelper.ActiveViewer.hWnd, rect) = 0) Then
                Return
            End If

            'If perspective (3D) view
            If (pCamera.ProjectionType = esri3DProjectionType.esriPerspectiveProjection) Then
                Dim dWidth, dHeight As Double

                dWidth = Math.Abs(rect.Right - rect.Left) * (Math.Abs(rect.Right - rect.Left) / pEnvelope.Width)
                dHeight = Math.Abs(rect.Top - rect.Bottom) * (Math.Abs(rect.Top - rect.Bottom) / pEnvelope.Height)

                pPoint = New PointClass
                pPoint.PutCoords(pEnvelope.XMin + (pEnvelope.Width / 2), pEnvelope.YMin + (pEnvelope.Height / 2))

                'Redimension envelope based on scene viewer dimensions
                pEnvelope.XMin = pPoint.X - (dWidth / 2)
                pEnvelope.YMin = pPoint.Y - (dHeight / 2)
                pEnvelope.Width = dWidth
                pEnvelope.Height = dHeight

                'Zoom camera to the envelope
                pCamera.ZoomToRect(pEnvelope)
            Else
                'Translate screen coordinates into a 3D point
                pSceneGraph.Locate(pSceneGraph.ActiveViewer, pEnvelope.XMin + (pEnvelope.Width / 2), _
                    pEnvelope.YMin + (pEnvelope.Height / 2), esriScenePickMode.esriScenePickAll, True, _
                    pPoint, pOwner, pObject)

                'Set camera target
                pCamera.Target = pPoint

                Dim dx, dy As Double
                dx = pEnvelope.Width
                dy = pEnvelope.Height

                'Determine zoom factor
                If (dx > 0 And dy > 0) Then
                    dx = Math.Abs(rect.Right - rect.Left) / dx
                    dy = Math.Abs(rect.Top - rect.Bottom) / dy

                    If (dx < dy) Then
                        pCamera.Zoom(dx)
                    Else
                        pCamera.Zoom(dy)
                    End If
                Else
                    pCamera.Zoom(1.3333333333333)
                End If
            End If
        End If

        'Redraw the scene viewer
        Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer)
        pSceneViewer.Redraw(True)
        m_bInUse = False
    End Sub

    Public Sub CreateEnvelope(ByVal X As Integer, ByVal Y As Integer, ByRef pEnvelope As IEnvelope)
        'Create envelope based upon the initial
        'and current mouse coordinates
        pEnvelope = New EnvelopeClass

        If (CType(m_lMouseX, Double) <= CType(X, Double)) Then
            pEnvelope.XMin = CType(m_lMouseX, Double)
            pEnvelope.XMax = CType(X, Double)
        Else
            pEnvelope.XMin = CType(X, Double)
            pEnvelope.XMax = CType(m_lMouseX, Double)
        End If

        If (CType(m_lMouseY, Double) <= CType(Y, Double)) Then

            pEnvelope.YMin = CType(m_lMouseY, Double)
            pEnvelope.YMax = CType(Y, Double)
        Else
            pEnvelope.YMin = CType(Y, Double)
            pEnvelope.YMax = CType(m_lMouseY, Double)
        End If

    End Sub

    Public Sub DrawRectangle(ByRef pEnvelope As IEnvelope)
        'Get the scene viewer
        Dim pSceneViewer As ISceneViewer = CType(m_pSceneHookHelper.ActiveViewer, ISceneViewer)

        'Redraw the rectangle
        pSceneViewer.Redraw(True)

        'GDI+ call to fill a rectangle with a hollow brush
        myGraphics.FillRectangle(m_brush, CType(pEnvelope.XMin, Integer), CType(pEnvelope.YMin, Integer), _
          CType(pEnvelope.Width, Integer), CType(pEnvelope.Height, Integer))

        'GDI+ call to draw a rectangle with a specified pen 
        myGraphics.DrawRectangle(m_pen, CType(pEnvelope.XMin, Integer), CType(pEnvelope.YMin, Integer), _
          CType(pEnvelope.Width, Integer), CType(pEnvelope.Height, Integer))
    End Sub

End Class