ArcEngine的ICommand接口,讲解oncreate(转载)

ArcEngine的ICommand接口

所有嵌入ArcCatalogArcMap应用程序中的按钮都是基于COM的按钮。开发者可以在支持COM的开发环境中如Visual Basic.Net(C#VB.Net),或C++中创建自定义的按钮。基于COM的按钮是以ActiveX DLLs形式分发的。可以创建下列形式的按钮:

Buttonsmenu是最简单的commands类型。Toolsbuttons很像,但需要和应用程序的显示交互。放大按钮就是个例子。Tool controls是像comboboxeseditboxes一样的按钮。

创建访问COM command的属性或创建自己的COM command时使用这个接口。

当你创建一个新的COM按钮时,要在你的类代码中实现ICommand接口。这些接口决定了简单按钮的行为和属性。例如,ICommand接口设置了按钮属性如captionnamecaegorybitmapstatusbar messagetooltiphelp context idhelp fileenable state,和checked state。同时也定义了按钮单击时发生的行为。

成员

Bitmap,Caption,Category,Checked,Enabled,HelpContextID,HelpFile,Message,Name,OnClick,OnCreate,Tooltip

当这个按钮创建时引发该方法

[Visual Basic.NET]        Public Sub OnCreate(ByVal hook As Object)

[C#]                             public void OnCreate(object hook);

OnCreate方法将一个hook传给按钮以关联应用程序。

ICommand创建自定义的按钮时,每个阶段会调用多次类的构造器和析构器。一旦获得按钮的相关信息,如namebitmap等等就要构造按钮,然后释放他们。最后调用完整的构造函数和OnCreateFeature方法。OnCreate只会调用一次,所以你可以用它来执行成员变量的初始化。可以在类的析构器中核对初始化的成员变量,以确定前面是否调用了OnCreate函数。

The following code obtains a hook to the application.

[C#]

Private IApplication m_app;

public void OnCreate(object hook)

{

       //the hook argument is pointer to Application object.

       m_app=hook as IApplication;

}

[Visual Basic.NET]

Private m_app As IApplication

Public Sub OnCreate(ByVal hook As Object) Implements ESRI.ArcGIS.SystmeUI.ICommand.OnCreate

       ‘The hook argument is pointer to Application object.

       m_app=CType(hook,IApplication)

End Sub

 

ICommand Example

[Visual Basic 6.0]

这个例子展示了使用Visual Basic如何创建一个名为MyCommand的自定义按钮。

按照下述步骤使用这个例子:

1.       建立一个新的ActiveX dll工程,在这个工程中添加对ESRI SystemsUI类库(esriSystemUI.olb),ESRI Framework类库(esriFramework.olb)ESRI ArcMapUI类库(esriArcMapUI.olb)的引用。

2.       将这个代码粘贴到一个类模块中。

3.       向窗体中添加一个Form1窗体和名为Picture1的控件。

4.       设置Picture1Picture属性为command所需的bitmap

5.       编译工程创建dll

6.       ArcMap的自定义对话框中,从文件添加创建的dll。自定义按钮将添加到ArcMap中。

7.       在自定义的Commands板块中,在种类列表中选择MyCustomTools

8.       测试按钮。

'Implement the ICommand interface

Implements ICommand

Dim m_pApp As IApplication        'ArcMap application

 

Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE

       'The VB project contains a form called Form1.

       'Picture1 is the name of a pictureBox control on the form.

       'The Picture property of PictureBox1 is set to some bitmap on your system.

       ICommand_Bitmap=Form1.Picture1.Picture.Handle

End Property

 

Private Property Get ICommand_Caption() As String

       'Set the string that appears when the command is used as menu item.

       ICommand_Caption="MyCommand"

End Property

 

Private Property Get ICommand_Category() As String

       'Set the category of this command.This determines where the command appears in the Commands panel of the Customize dialog.

       ICommand_Category="MyCustomTools"

End Property

 

Private Property Get ICommand_Checked() As Boolean

End Property

 

Private Property Get ICommand_Enabled() As Boolean

       'Add some logic here To specify in what state the application should be in for the command to be enabled.

       'In this example,the command is enabled only when there is at least one data layer loaded in ArcMap.

       Dim pMxDoc As IMxDocument

       Dim pLayerCount As Integer

       'm_pApp is set in OnCreate

       Set pMxDoc=m_pApp.Document

       pLayerCount=pMxDoc.FocusMap.LayerCount

       If pLayerCount>0 Then

              ICommand_Enabled=True

       Else

              ICommand_Enabled=False

       End If

       End Property

      

       Private Property Get ICommand_HelpFile() As String

              'If the help file is not registered you may need to supply th full path to the file

              ICommand_HelpFile="MyHelp.hlp"

       End Property

      

       Private Property Get ICommand_Message() As String

              'Set the message string that appears in statusbar of the application when the mouse passes over the command

              ICommand_Message="This is my custom command"

       End Property

      

       Private Property Get ICommand_Name() As String

              'Set the internal name of this command.By convention,this name string contains the category and caption of the command.

              ICommand_Name="MyCustomTool_MyCommand"

       End Property

      

       Private Sub ICommand_OnClick()

              'Add some code to do some action when the command is clicked.In this example, a message box is displayed.

              MsgBox"Cliced om my command"

       End Sub

      

       Private Sub ICommand_OnCreate(ByVal hook As Object)

              'The hook argument is a pointer to Application object.

              'Establish a hook to the application

              Set m_pApp=hook

       End Sub

      

       Private Property Get ICommand_Tooltip() As String

              'Set the string that appears in the screen tip.

              ICommand_Tooltip="MyCommand"

       End Property

所有嵌入ArcCatalogArcMap应用程序中的按钮都是基于COM的按钮。开发者可以在支持COM的开发环境中如Visual Basic.Net(C#VB.Net),或C++中创建自定义的按钮。基于COM的按钮是以ActiveX DLLs形式分发的。可以创建下列形式的按钮:

Buttonsmenu是最简单的commands类型。Toolsbuttons很像,但需要和应用程序的显示交互。放大按钮就是个例子。Tool controls是像comboboxeseditboxes一样的按钮。

创建访问COM command的属性或创建自己的COM command时使用这个接口。

当你创建一个新的COM按钮时,要在你的类代码中实现ICommand接口。这些接口决定了简单按钮的行为和属性。例如,ICommand接口设置了按钮属性如captionnamecaegorybitmapstatusbar messagetooltiphelp context idhelp fileenable state,和checked state。同时也定义了按钮单击时发生的行为。

成员

Bitmap,Caption,Category,Checked,Enabled,HelpContextID,HelpFile,Message,Name,OnClick,OnCreate,Tooltip

当这个按钮创建时引发该方法

[Visual Basic.NET]        Public Sub OnCreate(ByVal hook As Object)

[C#]                             public void OnCreate(object hook);

OnCreate方法将一个hook传给按钮以关联应用程序。

ICommand创建自定义的按钮时,每个阶段会调用多次类的构造器和析构器。一旦获得按钮的相关信息,如namebitmap等等就要构造按钮,然后释放他们。最后调用完整的构造函数和OnCreateFeature方法。OnCreate只会调用一次,所以你可以用它来执行成员变量的初始化。可以在类的析构器中核对初始化的成员变量,以确定前面是否调用了OnCreate函数。

The following code obtains a hook to the application.

[C#]

Private IApplication m_app;

public void OnCreate(object hook)

{

       //the hook argument is pointer to Application object.

       m_app=hook as IApplication;

}

[Visual Basic.NET]

Private m_app As IApplication

Public Sub OnCreate(ByVal hook As Object) Implements ESRI.ArcGIS.SystmeUI.ICommand.OnCreate

       ‘The hook argument is pointer to Application object.

       m_app=CType(hook,IApplication)

End Sub

 

ICommand Example

[Visual Basic 6.0]

这个例子展示了使用Visual Basic如何创建一个名为MyCommand的自定义按钮。

按照下述步骤使用这个例子:

1.       建立一个新的ActiveX dll工程,在这个工程中添加对ESRI SystemsUI类库(esriSystemUI.olb),ESRI Framework类库(esriFramework.olb)ESRI ArcMapUI类库(esriArcMapUI.olb)的引用。

2.       将这个代码粘贴到一个类模块中。

3.       向窗体中添加一个Form1窗体和名为Picture1的控件。

4.       设置Picture1Picture属性为command所需的bitmap

5.       编译工程创建dll

6.       ArcMap的自定义对话框中,从文件添加创建的dll。自定义按钮将添加到ArcMap中。

7.       在自定义的Commands板块中,在种类列表中选择MyCustomTools

8.       测试按钮。

'Implement the ICommand interface

Implements ICommand

Dim m_pApp As IApplication        'ArcMap application

 

Private Property Get ICommand_Bitmap() As esriSystem.OLE_HANDLE

       'The VB project contains a form called Form1.

       'Picture1 is the name of a pictureBox control on the form.

       'The Picture property of PictureBox1 is set to some bitmap on your system.

       ICommand_Bitmap=Form1.Picture1.Picture.Handle

End Property

 

Private Property Get ICommand_Caption() As String

       'Set the string that appears when the command is used as menu item.

       ICommand_Caption="MyCommand"

End Property

 

Private Property Get ICommand_Category() As String

       'Set the category of this command.This determines where the command appears in the Commands panel of the Customize dialog.

       ICommand_Category="MyCustomTools"

End Property

 

Private Property Get ICommand_Checked() As Boolean

End Property

 

Private Property Get ICommand_Enabled() As Boolean

       'Add some logic here To specify in what state the application should be in for the command to be enabled.

       'In this example,the command is enabled only when there is at least one data layer loaded in ArcMap.

       Dim pMxDoc As IMxDocument

       Dim pLayerCount As Integer

       'm_pApp is set in OnCreate

       Set pMxDoc=m_pApp.Document

       pLayerCount=pMxDoc.FocusMap.LayerCount

       If pLayerCount>0 Then

              ICommand_Enabled=True

       Else

              ICommand_Enabled=False

       End If

       End Property

      

       Private Property Get ICommand_HelpFile() As String

              'If the help file is not registered you may need to supply th full path to the file

              ICommand_HelpFile="MyHelp.hlp"

       End Property

      

       Private Property Get ICommand_Message() As String

              'Set the message string that appears in statusbar of the application when the mouse passes over the command

              ICommand_Message="This is my custom command"

       End Property

      

       Private Property Get ICommand_Name() As String

              'Set the internal name of this command.By convention,this name string contains the category and caption of the command.

              ICommand_Name="MyCustomTool_MyCommand"

       End Property

      

       Private Sub ICommand_OnClick()

              'Add some code to do some action when the command is clicked.In this example, a message box is displayed.

              MsgBox"Cliced om my command"

       End Sub

      

       Private Sub ICommand_OnCreate(ByVal hook As Object)

              'The hook argument is a pointer to Application object.

              'Establish a hook to the application

              Set m_pApp=hook

       End Sub

      

       Private Property Get ICommand_Tooltip() As String

              'Set the string that appears in the screen tip.

              ICommand_Tooltip="MyCommand"

       End Property

转载自:http://www.cnblogs.com/hehewoya/archive/2009/11/29/1613255.html

原文地址:https://www.cnblogs.com/atravellers/p/1647601.html