/ Copyright 2010 ESRI// // All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.// // You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.// // See the use restrictions at <your ArcGIS install location>/DeveloperKit10.0/userestrictions.txt.// using System;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using System.Runtime.InteropServices;
namespace AddMapSurrounds
{
[ClassInterface(ClassInterfaceType.None)]
[Guid("5175B831-F18E-4cff-A016-146A7923681D")]
publicsealedclass CreateNorthArrow : BaseTool
{
private IHookHelper m_HookHelper;
private INewEnvelopeFeedback m_Feedback;
private IPoint m_Point;
privatebool m_InUse;
//Windows API functions to capture mouse and keyboard//input to a window when the mouse is outside the window
[DllImport("User32", CharSet=CharSet.Auto)]
privatestaticexternint SetCapture(int hWnd);
[DllImport("User32", CharSet=CharSet.Auto)]
privatestaticexternint GetCapture();
[DllImport("User32", CharSet=CharSet.Auto)]
privatestaticexternint ReleaseCapture();
#region Component Category Registration
[ComRegisterFunction()]
[ComVisible(false)]
staticvoid RegisterFunction(String sKey)
{
ControlsCommands.Register(sKey);
}
[ComUnregisterFunction()]
[ComVisible(false)]
staticvoid UnregisterFunction(String sKey)
{
ControlsCommands.Unregister(sKey);
}
#endregionpublic CreateNorthArrow()
{
//Create an IHookHelper object
m_HookHelper = new HookHelperClass();
//Set the tool propertiesbase.m_bitmap = new System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(GetType(), "NorthArrow.bmp"));
base.m_caption = "NorthArrow";
base.m_category = "myCustomCommands(C#)";
base.m_message = "Add a north arrow map surround";
base.m_name = "myCustomCommands(C#)_NorthArrow";
base.m_toolTip = "Add a north arrow";
base.m_deactivate = true;
}
publicoverridevoid OnCreate(object hook)
{
m_HookHelper.Hook = hook;
}
publicoverridevoid OnMouseDown(int Button, int Shift, int X, int Y)
{
//Create a point in map coordinates
m_Point = m_HookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
//Start capturing mouse events
SetCapture(m_HookHelper.ActiveView.ScreenDisplay.hWnd);
m_InUse = true;
}
publicoverridevoid OnMouseMove(int Button, int Shift, int X, int Y)
{
if (m_InUse == false) return;
//Start an envelope feedbackif (m_Feedback == null )
{
m_Feedback = new NewEnvelopeFeedbackClass();
m_Feedback.Display = m_HookHelper.ActiveView.ScreenDisplay;
m_Feedback.Start(m_Point);
}
//Move the envelope feedback
m_Feedback.MoveTo(m_HookHelper.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
}
publicoverridevoid OnMouseUp(int Button, int Shift, int X, int Y)
{
if (m_InUse == false) return;
//Stop capturing mouse eventsif (GetCapture() == m_HookHelper.ActiveView.ScreenDisplay.hWnd)
ReleaseCapture();
//If an envelope has not been tracked or its height/width is 0if (m_Feedback == null)
{
m_Feedback = null;
m_InUse = false;
return;
}
IEnvelope envelope = m_Feedback.Stop();
if ((envelope.IsEmpty) || (envelope.Width == 0) || (envelope.Height == 0))
{
m_Feedback = null;
m_InUse = false;
return;
}
//Create the form with the SymbologyControl
SymbolForm symbolForm = new SymbolForm();
//Get the IStyleGalleryItem
IStyleGalleryItem styleGalleryItem = symbolForm.GetItem(esriSymbologyStyleClass.esriStyleClassNorthArrows);
//Release the form
symbolForm.Dispose();
if (styleGalleryItem == null) return;
//Get the map frame of the focus map
IMapFrame mapFrame = (IMapFrame) m_HookHelper.ActiveView.GraphicsContainer.FindFrame(m_HookHelper.ActiveView.FocusMap);
//Create a map surround frame
IMapSurroundFrame mapSurroundFrame = new MapSurroundFrameClass();
//Set its map frame and map surround
mapSurroundFrame.MapFrame = mapFrame;
mapSurroundFrame.MapSurround = (IMapSurround) styleGalleryItem.Item;
//QI to IElement and set its geometry
IElement element = (IElement) mapSurroundFrame;
element.Geometry = envelope;
//Add the element to the graphics container
m_HookHelper.ActiveView.GraphicsContainer.AddElement((IElement)mapSurroundFrame, 0);
//Refresh
m_HookHelper.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, mapSurroundFrame, null);
m_Feedback = null;
m_InUse = false;
}
}
}