Skip to main content
added 348 characters in body
Source Link
Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75

Note that the tool cursor is only valid while the mouse cursor is over the ArcMap canvas and then reverts to the current windows cursor as soon as the mouse cursor leaves the window. this.Cursor is a property of ESRI.ArcGIS.Desktop.AddIns.Tool not so for ESRI.ArcGIS.Desktop.AddIns.Button.

Note that the tool cursor is only valid while the mouse cursor is over the ArcMap canvas and then reverts to the current windows cursor as soon as the mouse cursor leaves the window.

Note that the tool cursor is only valid while the mouse cursor is over the ArcMap canvas and then reverts to the current windows cursor as soon as the mouse cursor leaves the window. this.Cursor is a property of ESRI.ArcGIS.Desktop.AddIns.Tool not so for ESRI.ArcGIS.Desktop.AddIns.Button.

Source Link
Michael Stimson
  • 25.7k
  • 2
  • 35
  • 75

Barbarosa, here's a sub from one of my tools that sets the cursor using a 'in memory' drawn bitmap though you could just as easily load a bitmap or cursor file from resources or known location (System.Reflection.Assembly.GetExecutingAssembly().Location perhaps). It's probably a bit too verbose but there's bound to be one or two gems that might help with your sticking point(s).

Note that the tool cursor is only valid while the mouse cursor is over the ArcMap canvas and then reverts to the current windows cursor as soon as the mouse cursor leaves the window.

System.Windows.Forms.Cursor gFirstSelectionCursor;
System.Windows.Forms.Cursor gSecondSelectionCursor;
System.Windows.Forms.Cursor gSnappingCursor;

public void SetCursor()
        {
            gDispTran = gDoc.ActiveView.ScreenDisplay.DisplayTransformation;
            int pHalfCursorSize = (int)cCursorSize / 2;
            int pQuarterCursorSize = (int)pHalfCursorSize / 2;

            System.Drawing.Point UL = new System.Drawing.Point(pQuarterCursorSize, pQuarterCursorSize);
            System.Drawing.Point UR = new System.Drawing.Point(cCursorSize - pQuarterCursorSize, pQuarterCursorSize);
            System.Drawing.Point LL = new System.Drawing.Point(pQuarterCursorSize, cCursorSize - pQuarterCursorSize);
            System.Drawing.Point LR = new System.Drawing.Point(cCursorSize - pQuarterCursorSize, cCursorSize - pQuarterCursorSize);

            Bitmap pBitmap = new Bitmap(cCursorSize, cCursorSize, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics pGraphics = Graphics.FromImage(pBitmap);
            pGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            Rectangle pRect = new Rectangle(0, 0, cCursorSize, cCursorSize);
            int pHalfSelection = (int)gSelectionTolerance / 2;
            pRect = new Rectangle(pHalfCursorSize - pHalfSelection, pHalfCursorSize - pHalfSelection, gSelectionTolerance, gSelectionTolerance);
            Color pCol = Color.Transparent;

            Color pCircleColour;

            if (gCursorColour == gCursorEnum.Black)
                pCircleColour = Color.Black;
            else if (gCursorColour == gCursorEnum.Red)
                pCircleColour = Color.Red;
            else if (gCursorColour == gCursorEnum.White)
                pCircleColour = Color.White;
            else if (gCursorColour == gCursorEnum.Yellow)
                pCircleColour = Color.Yellow;
            else
                pCircleColour = Color.Black;

            // set the draw lines to the same colour as the
            // selection circle
            ILineSymbol pLineSym = new SimpleLineSymbol();
            pLineSym.Color.RGB = ((pCircleColour.R | pCircleColour.G << 8) | pCircleColour.B << 0x10);

            //pLineSym.Color = (IColor)ESRI.ArcGIS.ADF.Converter.ToRGBColor(pCircleColour);
            pLineSym.Width = 1;
            gSym = (ISymbol)pLineSym;

            Brush pBrush = new SolidBrush(pCol);
            float[] pDashValues = { 3, 1 };
            Pen pPen = new Pen(pCircleColour, 1);
            pPen.DashPattern = pDashValues;
            pGraphics.FillRectangle(pBrush, pRect);
            pGraphics.DrawEllipse(pPen, pRect);
            gFirstSelectionCursor = new System.Windows.Forms.Cursor(pBitmap.GetHicon());
            // second cursor
            pBitmap = new Bitmap(cCursorSize, cCursorSize, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            pGraphics = Graphics.FromImage(pBitmap);
            pGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            pGraphics.DrawLine(pPen, UL, LR);
            pGraphics.DrawLine(pPen, LL, UR);
            gSecondSelectionCursor = new System.Windows.Forms.Cursor(pBitmap.GetHicon());
            // third cursor
            pBitmap = new Bitmap(cCursorSize, cCursorSize, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            pGraphics = Graphics.FromImage(pBitmap);
            pGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
            pGraphics.DrawLine(pPen, UL, LR);
            pGraphics.DrawLine(pPen, LL, UR);
            pRect = new Rectangle((int)pHalfCursorSize / 2, (int)pHalfCursorSize / 2, pHalfCursorSize, pHalfCursorSize);
            pGraphics.DrawEllipse(pPen, pRect);
            gSnappingCursor = new System.Windows.Forms.Cursor(pBitmap.GetHicon());

            if (gSelectionStarted)
                this.Cursor = gSecondSelectionCursor;
            else
                this.Cursor = gFirstSelectionCursor;
        }

This defines the cursor as a circle of cCursorSize when selecting and changes to a large X when something is selected. Note that the actual cursors are global which allows for fast switching during the OnMouseDown event; this sub is only called on tool startup and when the cCursorSize is changed via a form.