Custom tool how to implemet double click event

905
4
08-16-2011 08:12 AM
eddixoncastillo
New Contributor
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;
        }
    
    }
}

0 Kudos
4 Replies
AlexanderGray
Occasional Contributor III
You could call FinishSketch on the IeditSketch interface.
0 Kudos
EdgarBejarano
Occasional Contributor
Does OnDblClick() appear under the members menu on top of the code editor in your C# project?  I have a custom tool (class that implements BaseTool) like yours and my breakpoint inside my OnDblClick method is reached, and its message box is displayed with every double-click I do?  Mine works with or without base.OnDblClick() statement.

public sealed class clsTool1 : BaseTool
{
     ...
     ...
     ...

     #region Overridden Class Methods

     public override void OnDblClick()
     {
            base.OnDblClick();
            System.Windows.Forms.MessageBox.Show("Double-clicked!")
     }

     ...
     ...
     ...
} //end of class
0 Kudos
SebastianKrings
Occasional Contributor
Just a thought, but maybe onDoubleClick will just be invoked when double clicking on your tools icon like onClick works.

Then you would have to make use of on down, read the time until on mouse down(on mouse up as well) is activated twice. And when the mesured time is under 1 second, then a double click was performed.
0 Kudos
MarAlcaraz
New Contributor II
I have just coped with this issue. The solution is implementing ITool in your custom tool. Add this line of code after Inherits BaseTool:


   [INDENT] Inherits BaseTool
    Implements ITool
    Implements ICommand[/INDENT]


Then, add a new method, like this:


    [INDENT]Public Overrides Sub OnDblClick()
       [INDENT][INDENT]MyBase.OnDblClick()

        Dim newLineGeometry As IGeometry = _lineDrawFeedback.Stop()
        Dim rgbColor As IRgbColor = GetRGBColor(255, 0, 0)
        AddGraphicToMap(m_hookHelper.ActiveView, newLineGeometry, rgbColor, rgbColor)

        _lineDrawFeedback = Nothing
        _duringLineDrawing = False[/INDENT][/INDENT]

    End Sub[/INDENT]



Good Luck!!!
0 Kudos