eddixon85

Custom tool how to implemet double click event

Discussion created by eddixon85 on Aug 16, 2011
Latest reply on Dec 9, 2013 by margaral
I have a tool written in C# for draw pictures using mid points; but i don't know how to finish the sketch using the OnDblClick() method overriden by my tool,  because when I make a double click that method is never invoked.
Here's the code below:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;

namespace Editor_Tools
{
    /// <summary>
    /// Summary description for Midpoint.
    /// </summary>
    [Guid("567745d3-b672-4ec3-9f51-635a06b6dea1")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Editor_Tools.Midpoint")]
    public sealed class Midpoint : BaseTool
    {
        #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
        
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Register(regKey);

        }
        
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            ControlsCommands.Unregister(regKey);

        }

        #endregion
        #endregion

        #region private members
        private IHookHelper m_hookHelper;
        private IEngineEditor engineEditor = new EngineEditorClass();
        private IEngineEditSketch engineEditSketch;
        private IMapControlDefault mapControl;
        #endregion

        public Midpoint()
        {
            base.m_category = "Editor_Tools"; //localizable text 
            base.m_caption = "Mid Point";  //localizable text 
            base.m_message = "Mid Point";  //localizable text
            base.m_toolTip = "Mid Point";  //localizable text
            base.m_name = "Editor_Tools_Midpoint";   //unique id, non-localizable (e.g. "MyCategory_MyTool")
            try
            {
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
                base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods
 
  public override void OnCreate(object hook)
        {
            if (m_hookHelper == null)
                m_hookHelper = new HookHelperClass();

            m_hookHelper.Hook = hook;

            if (hook is IToolbarControl)
            {
                IToolbarControlDefault tbar = hook as IToolbarControlDefault;
                mapControl = tbar.Buddy as IMapControlDefault;
            }
            else if (hook is MapControl)
            {
                mapControl = hook as IMapControlDefault;
            }
        }

        public override void OnClick()
        {
            // TODO: Add Midpoint.OnClick implementation
            engineEditSketch = engineEditor as IEngineEditSketch;
            
        }

        public override void OnMouseDown(int Button, int Shift, int X, int Y)
        {
            //constructLine();
            IGeometry glinea = constructLine();
            IPolyline linea = glinea as IPolyline;
            IPoint to = linea.ToPoint;
            IPoint from = linea.FromPoint;
            IPoint mid = getMidPoint(from, to);
            engineEditSketch.AddPoint(mid, true);
            mapControl.ActiveView.Clear();
        }


        public override void OnDblClick()
        {
            base.OnDblClick();
            MessageBox.Show("buu");
        }

        public override void OnMouseMove(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add Midpoint.OnMouseMove implementation
        }

        public override void OnMouseUp(int Button, int Shift, int X, int Y)
        {
            // TODO:  Add Midpoint.OnMouseUp implementation
        }
        
        public override bool Enabled
        {
            get
            {
                if (!(engineEditor.EditState != esriEngineEditState.esriEngineStateNotEditing))
                {
                    return base.Enabled == false;
                }
                return base.Enabled == true;
            }
        }
        
        #endregion

        private IGeometry constructLine()
        {
            if (mapControl.ActiveView == null)
            {
                //return;
                return null;
            }
            ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = mapControl.ActiveView.ScreenDisplay;

            // Constant
            screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast
            ColorSelection.SetColor(0, 0, 0);

            ESRI.ArcGIS.Display.IColor color = ColorSelection.GetColor(); // Implicit Cast
            ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
            simpleLineSymbol.Color = color;

            ESRI.ArcGIS.Display.ISymbol symbol = (ESRI.ArcGIS.Display.ISymbol)simpleLineSymbol; // Explicit Cast
            ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberLineClass();
            ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);
            screenDisplay.SetSymbol(symbol);
            screenDisplay.DrawPolyline(geometry);
            screenDisplay.FinishDrawing();

            return geometry;
        }
        
        private IPoint getMidPoint(IPoint from, IPoint to)
        {
            double x1, x2, y1, y2;
            x1 = from.X;
            y1 = from.Y;
            x2 = to.X;
            y2 = to.Y;

            IPoint mid = new PointClass();
            mid.X = (x1 + x2) / 2;
            mid.Y = (y1 + y2) / 2;

            return mid;
        }
    
    }
}

Outcomes