【028】◀▶ 自定义命令 & 工具
---------------------------------------------------------------------------------------------------------
●·● 目录:
A1 ………… ICommand 接口
A2 ………… ITool 接口
A3 ………… 三种添加命令、工具的方法
一、自己写命令:
第一步:右键》添加项,如下图所示:
第二步:在生成的代码中进行修改,修改的部分很少,只是两行线之间,如下所示:
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;
namespace ToolbarControl
{
/// <summary>
/// Summary description for OpenMxdCommand.
/// </summary>
[Guid("b5b6817b-f990-4e95-a2a9-888090cf4bd2")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("ToolbarControl.OpenMxdCommand")]
public sealed class OpenMxdCommand : BaseCommand
{
#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 IHookHelper m_hookHelper;
IMapControl2 pMapControl;
public OpenMxdCommand()
{
//
// TODO: Define values for the public properties
//
//---------------------------------------------------------------------------------------------------------------
//下面的部分将引号内部填上汉字,就算是修改了!!!
base.m_category = "ToolbarControl"; //一般写命名空间的名称
base.m_caption = "打开地图文档"; //localizable text,中文解释,名称,以下类似!
base.m_message = "打开地图文档"; //localizable text
base.m_toolTip = "打开地图文档"; //localizable text
base.m_name = "OpenMxdCommand"; //unique id, non-localizable (e.g. "MyCategory_MyCommand")
//一般写工具的Name也行的!
//---------------------------------------------------------------------------------------------------------------
try
{
//
// TODO: change bitmap name if necessary
//
string bitmapResourceName = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
#region Overridden Class Methods
/// <summary>
/// Occurs when this command is created
/// </summary>
/// <param name="hook">Instance of the application</param>
public override void OnCreate(object hook)
{
if (hook == null)
return;
//---------------------------------------------------------------------------------------------------------------
//下面进行修改,主要建立关联
if (hook is IToolbarControl)
{
IToolbarControl pToolbar = hook as IToolbarControl;
pMapControl = pToolbar.Buddy as IMapControl2;
}
else if (hook is IMapControl2)
{
pMapControl = hook as IMapControl2;
}
//---------------------------------------------------------------------------------------------------------------
if (m_hookHelper == null)
m_hookHelper = new HookHelper();
m_hookHelper.Hook = hook;
// TODO: Add other initialization code
}
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add OpenMxdCommand.OnClick implementation
//---------------------------------------------------------------------------------------------------------------
//下面的内容就是点击命令所执行的内容了!
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "map document|*.mxd";
ofd.ShowDialog();
try
{
pMapControl.LoadMxFile(ofd.FileName);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//---------------------------------------------------------------------------------------------------------------
#endregion
}
}
第三步:在load事件中添加命令,代码如下:
(1)在 axToolbarControl 中调用:
OpenMxdCommand pMxdCommand = new OpenMxdCommand();
axToolbarControl1.AddItem(pMxdCommand, -1,-1,false,0, esriCommandStyles.esriCommandStyleIconOnly);
(2)在菜单中调用:
ICommand pMxdCommand = new OpenMxdCommand();
pMxdCommand.OnCreate(axMapControl1.Object);
pMxdCommand.OnClick; //操作点击方法
若调用的是工具,则要用如下代码:
ICommand pPagePan = new ControlsMapPanTool();
pPagePan.OnCreate(axPageLayoutControl1.Object);
axPageLayoutControl1.CurrentTool = pPagePan as ESRI.ArcGIS.SystemUI.ITool; //当前工具为此工具
二、通过系统自带的自定义工具来添加命令
ICustomizeDialog m_CustomizeDialog = new CustomizeDialog(); //自定义一个自定义窗口
private void Form1_Load(object sender, EventArgs e)
{
CreateCustomizeDialog();
}
private void CreateCustomizeDialog()
{
((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnStartDialog +=
new ICustomizeDialogEvents_OnStartDialogEventHandler(OnStartDialog); //添加开始事件
((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnCloseDialog +=
new ICustomizeDialogEvents_OnCloseDialogEventHandler(OnCloseDialog); //添加关闭事件
//CustomizeDialog本身没有实现的事件,但是ICustomizeDialogEvents_Event有两个可以实现的事件,强制类型转换触发事件!
m_CustomizeDialog.DialogTitle = "Customize ToolbarControl Items!!!";
m_CustomizeDialog.SetDoubleClickDestination(axToolbarControl1);
}
private void OnStartDialog() //当点击显示对话框的时候触发
{
axToolbarControl1.Customize = true;
}
private void OnCloseDialog() //当关闭显示对话框的时候触发
{
axToolbarControl1.Customize = false;
checkBox1.Checked = false;
}
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A1个 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● ICommand 接口:
1. Provides access to members that define a COM command.
Members
| Description | ||
|---|---|---|
![]() |
Bitmap | The bitmap that is used as the icon on this command. |
![]() |
Caption | The caption of this command. |
![]() |
Category | The name of the category with which this command is associated. |
![]() |
Checked | Indicates if this command is checked. |
![]() |
Enabled | Indicates if this command is enabled. |
![]() |
HelpContextID | The help context ID associated with this command. |
![]() |
HelpFile | The name of the help file associated with this command. |
![]() |
Message | The statusbar message for this command. |
![]() |
Name | The name of this commmand. |
![]() |
OnClick | Occurs when this command is clicked. |
![]() |
OnCreate | Occurs when this command is created. |
![]() |
Tooltip | The tooltip for this command. |
CoClasses that implement ICommand
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A2个 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● ITool 接口:
1. Provides access to members that define a tool.
Members
| Description | ||
|---|---|---|
![]() |
Cursor | The mouse pointer for this tool. |
![]() |
Deactivate | Causes the tool to no longer be the active tool. |
![]() |
OnContextMenu | Context menu event occured at the given xy location. |
![]() |
OnDblClick | Occurs when a mouse button is double clicked when this tool is active. |
![]() |
OnKeyDown | Occurs when a key on the keyboard is pressed when this tool is active. |
![]() |
OnKeyUp | Occurs when a key on the keyboard is released when this tool is active. |
![]() |
OnMouseDown | Occurs when a mouse button is pressed when this tool is active. |
![]() |
OnMouseMove | Occurs when the mouse is moved when this tool is active. |
![]() |
OnMouseUp | Occurs when a mouse button is released when this tool is active. |
![]() |
Refresh | Occurs when a screen display in the application is refreshed. |
CoClasses that implement ITool
| CoClasses and Classes | Description |
|---|---|
| AutoCompletePolygonFeatureTool (esriEditor) | Tool that uses the AutoCompletePolygon Task to create a new Polygon from a Line sketch geometry. |
| Controls3DAnalystContourTool (esriControls) | Generates the contour that passes through a query point. |
| Controls3DAnalystSteepestPathTool (esriControls) | Generates the steepest path down from a point. |
| ControlsEditingEditTool (esriControls) | Edits features and their geometries. |
| ControlsEditingSketchTool (esriControls) | Adds points to the edit sketch. |
| ControlsGenericGetPositionTool (esriControls) | Tools that can be used to retrieve the cursor coordinates as the user clicks on the map or globe. |
| ControlsGlobeFixedLineOfSightTool (esriControls) | Rotates the observer around the target. |
| ControlsGlobeFlyTool (esriControls) | Flies over the globe. |
| ControlsGlobeHyperlinkTool (esriControls) | Hyperlinks to features on a globe, if more than one hyperlink is under the cursor a dialog is shown allowing the user to select which hyperlink to jump to. |
| ControlsGlobeIdentifyTool (esriControls) | Finds features on a globe, launches a modeless dialog to search fields in globe layers. |
| ControlsGlobeLookAroundTool (esriControls) | Rotates the observer to look around. |
| ControlsGlobeMeasureTool (esriControls) | Measures features on a globe, a floating tooltip is used to show the result. The message property returns a string for the status bar. |
| ControlsGlobeNavigateTool (esriControls) | Navigates the globe. |
| ControlsGlobeOrbitalFlyTool (esriControls) | Flies in orbital trajectories over the globe. |
| ControlsGlobePanDragTool (esriControls) | Trackball style pan tool. |
| ControlsGlobePanTool (esriControls) | Pans the globe. |
| ControlsGlobeSelectFeaturesTool (esriControls) | Selects features by clicking. |
| ControlsGlobeSwipeTool (esriControls) | Interactively reveals layers on a globe. |
| ControlsGlobeTargetCenterTool (esriControls) | Centers view at selected target. |
| ControlsGlobeTargetPanTool (esriControls) | Pans to selected target. |
| ControlsGlobeTargetZoomTool (esriControls) | Zooms to selected target. |
| ControlsGlobeWalkTool (esriControls) | Walks on the globe surface. |
| ControlsGlobeZoomInOutTool (esriControls) | Dynamically zooms in or out the globle. |
| ControlsInkEraserTool (esriControls) | Erases ink from a map or layout. |
| ControlsInkGenericDrawTool (esriControls) | A generic ink drawing tool. |
| ControlsInkHighlightTool (esriControls) | Draws semi-transparent ink on a map or layout. |
| ControlsInkPenTool (esriControls) | Draws colored ink on a map or layout. |
| ControlsMapHyperlinkTool (esriControls) | Hyperlinks to features on a map, if more than one hyperlink is under the cursor a dialog is shown allowing the user to select which hyperlink to jump to. |
| ControlsMapIdentifyTool (esriControls) | Identifies features on a map, launches a modeless identify dialog containing the results. |
| ControlsMapMeasureTool (esriControls) | Measures features on a map, a floating tooltip is used to show the result. The message property returns a string for the status bar. |
| ControlsMapPanTool (esriControls) | Pans the map. |
| ControlsMapRoamTool (esriControls) | Click the mouse left button to start or finish roaming, move the mouse to change roaming direction and speed. |
| ControlsMapRotateTool (esriControls) | Rotates the focus data frame. |
| ControlsMapSwipeTool (esriControls) | Interactively reveals layers on a map. |
| ControlsMapZoomInTool (esriControls) | Zooms in by clicking a point or dragging a box. |
| ControlsMapZoomOutTool (esriControls) | Zooms out by clicking a point or dragging a box. |
| ControlsMapZoomPanTool (esriControls) | Drags up/down with left mouse button down to zoom out/in, or with right mouse button down to pan. |
| ControlsNetworkAnalystCreateLocationTool (esriControls) | Create a Network Location. |
| ControlsNetworkAnalystSelectLocationTool (esriControls) | Select or Move Network Locations. |
| ControlsNewCircleTool (esriControls) | Draws a circle. |
| ControlsNewCurveTool (esriControls) | Draws a cubic Bezier curve. |
| ControlsNewEllipseTool (esriControls) | Draws an ellipse. |
| ControlsNewFrameTool (esriControls) | Creates a new frame element. |
| ControlsNewFreeHandTool (esriControls) | Draws a freehand line. |
| ControlsNewLineTool (esriControls) | Draws a straight line. |
| ControlsNewMarkerTool (esriControls) | Create a new marker graphic element. |
| ControlsNewPolygonTool (esriControls) | Draws a polygon. |
| ControlsNewRectangleTool (esriControls) | Draws a rectangle. |
| ControlsPagePanTool (esriControls) | Pans the map layout by dragging it. |
| ControlsPageZoomInTool (esriControls) | Zooms in on the map layout by clicking a point or dragging a box. |
| ControlsPageZoomOutTool (esriControls) | Zooms out on the map layout by clicking a point or dragging a box. |
| ControlsRotateElementTool (esriControls) | Rotates the selected text or graphic(s). |
| ControlsSceneFlyTool (esriControls) | Flies through the scene. |
| ControlsSceneNavigateTool (esriControls) | Navigates the scene. |
| ControlsScenePanTool (esriControls) | Pans the scene. |
| ControlsSceneSelectFeaturesTool (esriControls) | Selects features by clicking. |
| ControlsSceneSelectGraphicsTool (esriControls) | Selects graphics by clicking. |
| ControlsSceneSetObserverTool (esriControls) | Sets observer position to selected point. |
| ControlsSceneTargetCenterTool (esriControls) | Centers view at selected target. |
| ControlsSceneTargetZoomTool (esriControls) | Zooms to selected target. |
| ControlsSceneZoomInOutTool (esriControls) | Dynamically zooms in and out on the scene. |
| ControlsSceneZoomInTool (esriControls) | Zooms in on the scene. |
| ControlsSceneZoomOutTool (esriControls) | Zooms out on the scene. |
| ControlsSchematicMoveElementTool (esriSchematicControls) | Move the Schematic element. |
| ControlsSchematicSelectEndTool (esriSchematicControls) | Define a Schematic end. |
| ControlsSchematicSelectRootTool (esriSchematicControls) | Define a Schematic root. |
| ControlsSelectFeaturesTool (esriControls) | Selects features by clicking or dragging a box. |
| ControlsSelectTool (esriControls) | Selects, resizes and moves text, graphics and other objects placed on the map. |
| CutPolygonsTool (esriEditor) | |
| EditTool (esriEditor) | Editing tool which edits features. |
| LineFeatureTool (esriEditor) | Tool that creates a new Polyline sketch geometry. |
| MirrorFeaturesTool (esriEditor) | |
| MxAddressInspectorTool (esriLocationUI) | Displays Address of a Location Using Application's Current Locator. |
| MxLocatorManager2Control (esriLocationUI) | Management of the Locators. |
| PointAtEndOfLineTool (esriEditor) | Tool that creates a new Point/Multipoint at the end of a line geometry. |
| PointFeatureTool (esriEditor) | Tool that creates a new Point/Multipoint sketch geometry. |
| PolygonFeatureTool (esriEditor) | Tool that creates a new Polygon sketch geometry. |
| ReplaceGeometryTool (esriEditor) | |
| ReshapeFeatureTool (esriEditor) | |
| SingleLineSearchControl (esriLocationUI) | Make a Search of Address Based on Entered String Using Application's Current Locator. |
| Tool (esriFramework) | Tool CoType. |
| ToolHost | Use this class to host pure C++ tool implementations in a Toolbar. |
---------------------------------------------------------------------------------------------------------
╔════════╗
╠════╣ 第A3个 ╠══════════════════════════════════════════════════╣
╚════════╝
●·● 三种添加命令、工具的方法:
//Adding a command by UID UID uID = new UIDClass(); uID.Value = "esriControlCommands.ControlsMapFullExtentCommand"; axToolbarControl1.AddItem(uID,-1,-1,false,0,esriCommandStyles.esriCommandStyleIconOnly); //Adding a command by ProgID string progID = "esriControlCommands.ControlsMapFullExtentCommand"; axToolbarControl1.AddItem(progID,-1,-1,false,0,esriCommandStyles.esriCommandStyleIconOnly); //Adding a command by ICommand ICommand command = new ControlsMapFullExtentCommandClass(); axToolbarControl1.AddItem(command,-1,-1,false,0,esriCommandStyles.esriCommandStyleIconOnly);



浙公网安备 33010602011771号