阅读:57319回复:116
[帝国首发]ArcGIS开发教材,0830更新,提问前请先看
<P><FONT color=#ff0000>还没有更新完毕,请大家不要在这里回帖,谢谢!:)</FONT></P>
<P>如何在ArcMap的VBA环境中编程</P> <P><FONT face="MS UI Gothic" size=2>ArcMap是ArcGIS家族的成员之一,它内置了一种集成编程环境―VBA(Visaul Basic for Apllications)。通过VBA编程,用户不但可以扩展ArcMap的菜单、工具条等,而且可以完成大多数用户的特定需求。<br> ArcMap中VBA编程的方法有两种,一种是写VBA宏,另一种是创建UIControl并在其事件中写入实现用户需求的代码。下面列出两种方法的一般步骤。<br> 方法一:写VBA宏(直接在VBA编辑器中编辑函数和过程)<br> 1、如图1,单击菜单栏中的<TOOLS>命令,选择<MACROS>的<VISUAL Basic Editor>项, 直接启动ArcMap的VBA编辑器;或者选择<MACROS>的<MACROS>项,进入如图2所示Macro对话框,在“Macro Name”文本框中输入要创建的宏的名称,并点<CREATE>按钮,启动VBA编辑器。<br></FONT></P> <DIV align=center><FONT face="MS UI Gothic" size=2><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0101-1.jpg" border=0></FONT></DIV> <P align=center><br>图1 启动Macro对话框/启动VBA编辑器<br></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0101-2.jpg" border=0></DIV> <P align=center><br>图2 Macro对话框<br> 2、在图3所示的窗口中,用户可以根据实际选择在Normal节点或者Project节点的ThisDocument、Forms、Modules中编写宏(函数或过程),Normal节点下所写的宏系统自动保存,除非用户删除,否则它将始终存在并在任何工程中都有效;而在Project节点下所写得宏随工程保存(如不保存工程,则宏也将不被保存),并中有效。<br></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0101-3.jpg" border=0></DIV> <P align=center>图3 VBA编辑器(VBE)<br></P> <P> 3、运行VBA宏<br> 在VBA编辑器中写好VBA代码后,有两种方式运行:第一,点击VBA编辑器工具条中的 (运行)按钮,可立即运行写好的代码;第二,退出VBA编辑器,重新启动Macro对话框,如图2,选择要运行的VBA宏名称,点击<RUN>按钮即可运行相应的VBA宏。<br> 方法二:创建UIControl(交互式VBA编程)<br> 1、用鼠标右击任何工具栏(条),在弹出的上托式菜单中选择<CUSTOMIZE>菜单项,如图4,进入图5所示的Customize对话框。<br></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0101-4.jpg" border=0></DIV> <P align=center>图4 启动“Customize”对话框</P> <P> 2、切换到“Customize”对话框的“Commands”页,选中“UIControls”后点击<NEW UIControl>按钮,进入图6所示的“New UIControl”对话框。<br> 3、在“New UIControl”对话框中,用户可根据需要选择UIControl类型:<br> UIButtonControl:创建Button;<br> UIToolControl:创建与Map交互的Tool;<br> UIEditBoxControl:创建EditBox;<br> UIComboBoxControl:创建ComboBox。<br> 最后点击<CREATE>按钮只创建UIControl或者点击<CREATE and Edit>按钮创建UIControl并进入VBA编辑器。与方法一不同,此时应在UIControl的事件中进行VBA编程。<br></P><br> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0101-5.jpg" border=0></DIV> <P align=center><br>图5 Customize对话框<br></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0101-6.jpg" border=0></DIV> <P align=center>图6 New UIControl对话框</P> <P> 4、UIControl创建后,在图5所示的“Customize”对话框选中UIControl并将其拖置到任意工具条上,用户便可象使用系统已有的Control一样使用所创建的UIControl。<br></P> [此贴子已经被作者于2005-8-30 15:10:07编辑过]
|
|
|
1楼#
发布于:2005-07-26 10:43
<P>如何在VB环境中利用ArcObjects组件开发ActiveX DLL</P>
<P><FONT face="MS UI Gothic" size=2> </FONT>1.1.1节讨论了如何在ArcGis的VBA环境中编程,虽然通过这种方式可以完成大多数用户的定制需求,但是,在某些情况下,对于特殊的应用,用户需要脱离ArcGIS环境而在VB开发环境中开发外部独立的应用程序,这种外部独立的应用程序有两种形式: ActiveX DLL和Standard EXE。Standard EXE的开发将在1.1.4中讨论,本节将讨论ActiveX DLL的开发,其关键是引用ArcObjects对象库和实现ArcObjects接口(例如ICommand,ITool,IToolBar等)。<BR> 下面介绍在VB环境利用ArcObjects组件开发ActiveX DLL的一般步骤。<BR> 1、启动VB开发环境,在图7所示的“New Project”对话框中选择“ActiveX DLL”项,并点击<打开>按钮,进入VBE环境。<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0102-1.jpg" border=0></DIV> <P align=center>图7 New Project对话框</P> <P>???? 2、引用ArcObjects对象库:首先点击<PROJECT>菜单中的<REFERENCES>项,如图8,进入对象库引用对话框,如图9。<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0102-2.jpg" border=0></DIV> <P align=center>图8 启动对象库引用对话框<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0102-3.jpg" border=0></DIV> <P align=center>图9 对象库引用对话框</P> <P>???? 3、对象库引用对话框(图9)中选中“Esri ArcMap Object Library ”和“Esri Object Library” 两项,并点击<OK>按钮,返回VBE环境。<BR> 4、一般在类模块中写入实现特定ArcObjects接口的代码,如图10,然后运行<FILE>菜单中的<MAKE project1.dll>项,生成DLL文件,如图11。(project1.dll随项目名改变)。<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0102-4.jpg" border=0></DIV> <P align=center>图10 类模块编辑窗口<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0102-5.jpg" border=0></DIV> <P align=center>图11 生成DLL文件<BR></P> |
|
|
2楼#
发布于:2005-07-26 10:47
如何在ArcMap中加载利用ArcObjects组件开发的ActiveX DLL
<P> 用户通过1.1.2中介绍的方法开发好一个ActiveX DLL程序后,便可根据实际需要,在ArcMap环境下加载这个ActiveX DLL程序。其一般步骤如下:<BR> 1、用鼠标右击任何工具栏(条),点击弹出的上托式菜单中的<CUSTOMIZE>菜单项(参见图4)。<BR> 2、在Customize对话框中,根据被加载DLL的类型切换到“Toolbars”或者“Commands”页(参见图5),然后点击<ADD From File>按钮。<BR> 3、在“打开文件”对话框中(Windows通用“打开文件”对话框,图略),选择被加载的Dll文件,并点击<打开>按钮。<BR> 4、如果加载是“Commands”,则在图5所示的对话框中显示加载的Command,并可以将其拖置于任何工具条上;如果加载是“ToolBars”,则在图12所示的对话框中显示加载的ToolBar,选中后即可在ArcMap中显示。<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0103-1.jpg" border=0></DIV> |
|
|
3楼#
发布于:2005-07-26 10:48
如何在VB环境中利用ArcObjects控件开发EXE
<P> 利用ArcObjects控件开发EXE的前三步类似于1.1.2中开发“Acrtive Dll”的前三步,唯一不同的是在“New Project”对话框中选择“Standard EXE”。<BR> 4、点击<PROJECT>菜单项中的<COMPONENTS>项,打开“Components”对话框,如图13。<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0104-1.jpg" border=0></DIV> <P align=center>图13 打开Components对话框</P> <P>???? 5、在“Components”对话框中,切换到Controls页,并选中“ESRI MapControl”项,点击<应用>或<确定>按钮,如图14。<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0104-2.jpg" border=0></DIV> <P align=center>图14 Components对话框</P> <P>???? 6、如图15所示,加载MapControl控件之后,在VBE的控件面板中出现了MapControl控件图标,用户便可以象在Form中添加Button一样在Form中添加MapControl控件,并利用它开发EXE。<BR></P> <DIV align=center><IMG src="http://www.sdgis.com/Data/Chinese/Develop/ArcGISBook/1/pic/wd0104-3.jpg" border=0></DIV> <P align=center>图15 添加MapControl控件</P> |
|
|
4楼#
发布于:2005-07-26 10:51
如何创建定制的按钮(Button)
<P 17.95pt">本例要实现的是如何创建定制的按钮(Button)。</P> <P 39pt; TEXT-INDENT: -39pt">l 要点</P> <P 0.05pt; TEXT-INDENT: 17.95pt">用户通过在类模块中实现ICommand接口来创建定制的按钮(COM command)。ICommand接口包括 caption、 name、 category、 bitmap、 message(StatusBarr的提示信息)、 tooltip(微帮助)、 help context id 、help file、enabled以及checked等十个属性和OnCreate、 OnClick两个事件。从Icommand接口的OnCreate事件中获取的ArcMap的Application实例必须用一个公共变量保存,以便在其它事件中(或者其它接口的事件中甚至整个工程中)使用。</P> <P 17.95pt">·OnCreate事件的参数hook传入的是一个Object,也就是ArcMAP的Application实例,可把它赋给一个IApplication接口的变量,便获得了ArcMAP的实例。</P> <P 17.95pt; TEXT-INDENT: 0.05pt">·在OnClick事件中写入相关代码,表示按下按钮时要实现的功能.</P> <P 39pt; TEXT-INDENT: -39pt">l 程序说明</P> <P 17.95pt; TEXT-INDENT: 0.05pt">程序在类模块中实现Icommand接口来创建自己的按钮(Button)</P> <P 39pt; TEXT-INDENT: -39pt">l 代码</P> <P> <TABLE height=2202 width=541 align=center border=0> <TR> <TD width=531 height=2198> <P><CODE>Option Explicit</CODE><CODE><BR>'实现Icommand接口<BR>Implements ICommand<BR>Dim m_pPicture as Picture<BR>Dim m_pApplication As IApplication<BR><BR>Private Sub Class_Initialize()<BR> '调入.RES文件中ID为101的BitMap作为该按钮的显示图片<BR> Set m_pPicture = LoadResPicture(101, vbResBitmap)<BR>End Sub</CODE></P> <P><CODE>Private Property Get ICommand_Bitmap() As esriCore.OLE_HANDLE<BR> ICommand_Bitmap = m_pPicture<BR>End Property</CODE></P> <P><CODE>Private Property Get ICommand_Caption() As String<BR> ICommand_Caption = "Create Button"<BR>End Property</CODE></P> <P><CODE>Private Property Get ICommand_Category() As String<BR> ICommand_Category = " Create Button "<BR>End Property</CODE></P> <P><CODE>Private Property Get ICommand_Checked() As Boolean<BR>End Property</CODE></P> <P><CODE>Private Property Get ICommand_Enabled() As Boolean<BR> ICommand_Enabled = True<BR>End Property</CODE></P> <P><CODE>Private Property Get ICommand_HelpContextID() As Long<BR>End Property</CODE></P> <P><CODE>Private Property Get ICommand_HelpFile() As String<BR>End Property</CODE></P> <P><CODE>Private Property Get ICommand_Message() As String<BR>End Property</CODE></P> <P>Private Property Get ICommand_Name() As String<BR>ICommand_Name = " CreateButton "<BR>End Property</P> <P>Private Sub ICommand_OnClick()<BR> '加入按下按钮时实现的功能代码。在这里,<BR> '按钮按下时显示ArcMap的Document的Tittle<BR> Dim pDocument As IDocument<BR> Set pDocument = m_pApplication.Document<BR> MsgBox pDocument.Title<BR>End Sub</P> <P>Private Sub ICommand_OnCreate(ByVal hook As Object)<BR> '获取ArcMap的Application实例<BR> Set m_pApplication = hook<BR>End Sub</P> <P>Private Property Get ICommand_Tooltip() As String<BR> ICommand_Tooltip = " Create Button "<BR>End Property</P> <P>Private Sub ITool_OnDblClick()<BR> '在这里加入Mouse双击时的功能代码<BR>End Sub </P> <P>Private Sub ITool_OnKeyDown(ByVal keyCode As Long, ByVal Shift As Long)<BR>End Sub </P> <P>Private Sub ITool_OnKeyUp(ByVal keyCode As Long, ByVal Shift As Long)<BR>End Sub </P> <P 32pt; TEXT-INDENT: -32pt">Private Sub ITool_OnMouseDown(ByVal Button As Long, ByVal Shift As Long, _<BR>ByVal X As Long, ByVal Y As Long)<BR> '加入Mouse单击时的功能代码<BR> If Button = 1 Then<BR> Dim pPoint As IPoint<BR> Dim pMxApplication As IMxApplication<BR> Set pMxApplication = m_pApp<BR> Set pPoint=pMxApplication.Display.DisplayTransformation.ToMapPoint(X, Y)<BR> m_pApplication.StatusBar.Message(0) = Str(pPoint.X) ; "," ; Str(pPoint.Y)<BR> End If</P> <P>End Sub</P> <P>Private Sub ITool_OnMouseMove(ByVal Button As Long, ByVal Shift As Long, _<BR> ByVal X As Long, ByVal Y As Long)<BR> '加入Mouse移动时的功能代码<BR> m_pApplication.StatusBar.Message(0) = "ITool_OnMouseMove"<BR>End Sub</P> <P>Private Sub ITool_OnMouseUp(ByVal Button As Long, ByVal Shift As Long, _<BR> ByVal X As Long, ByVal Y As Long)<BR> '加入释放Mouse时的功能代码<BR> m_pApplication.StatusBar.Message(0) = "ITool_OnMouseUp"<BR>End Sub</P> <P>Private Sub ITool_Refresh(ByVal hDC As esriCore.OLE_HANDLE)<BR>End Sub</P></TD></TR></TABLE></P> |
|
|
5楼#
发布于:2005-07-26 10:52
<P><FONT size=2>本例要实现的是如何创建定制的Tool<FONT face="MS UI Gothic"> </FONT></FONT></P>
<P> 要点</P> <P 17.95pt">用户在类模块中实现Icommand(参见1.2.1)和ITool接口。ITool接口包括 mouse move, mouse button press/release, keyboard key press/release, double-click以及right click等事件、Cursor属性和Refresh方法。</P> <P 17.95pt">Tool既具有Button的功能,又具有与ArcMAP界面交互的功能,Button的功能代码必须写在Icommand的OnClick事件中,而所有实现交互功能的代码必须写在Itool接口的各个事件中。Itool接口的各个事件,用户可以在其中写入相关代码,表示用户与ArcMAP界面交互时一旦触发某事件要实现的功能。</P> <P 21pt; TEXT-INDENT: -21pt">l 程序说明</P> <P 21pt; TEXT-INDENT: -21pt"> 程序在类模块中实现Icommand和Itool接口来创建自己的Tool.</P> <P 21pt; TEXT-INDENT: -21pt">l 代码</P> <P> <TABLE height=43 width=541 align=center border=0> <TR> <TD width=531> <P>Option Explicit<BR>'实现Icommand和Itool接口<BR>Implements ICommand<BR>Implements ITool<BR>Dim m_pApplication As IApplication <BR>Dim m_pBitmap As IPictureDisp<BR>Dim m_pCursor As IpictureDisp </P> <P>Private Sub Class_Initialize()<BR> Set m_pBitmap = LoadResPicture(101, 0)<BR> '从.RES文件中调入ID为102的图片作为按下Tool后的MouseCursor<BR> Set m_pCursor = LoadResPicture(102, 2)<BR>End Sub </P> <P>Private Property Get ICommand_Bitmap() As esriCore.OLE_HANDLE<BR> ICommand_Bitmap = m_pBitmap<BR>End Property</P> <P>Private Property Get ICommand_Caption() As String<BR> ICommand_Caption = "MyTool"<BR>End Property </P> <P>Private Property Get ICommand_Category() As String<BR> ICommand_Category = "MyCustomTools"<BR>End Property</P> <P>Private Property Get ICommand_Checked() As Boolean<BR>End Property</P> <P>Private Property Get ICommand_Enabled() As Boolean<BR> ICommand_Enabled = True<BR>End Property</P> <P>Private Property Get ICommand_HelpContextID() As Long<BR>End Property </P> <P>Private Property Get ICommand_HelpFile() As String<BR>End Property </P> <P>Private Property Get ICommand_Message() As String<BR> ICommand_Message = "This is my custom tool"<BR>End Property</P> <P>Private Property Get ICommand_Name() As String<BR> ICommand_Name = "MyCustomTool_MyTool"<BR>End Property</P> <P>Private Sub ICommand_OnClick()<BR> '加入按下按钮时实现的功能代码<BR> MsgBox "Clicked on my command"<BR>End Sub</P> <P>Private Sub ICommand_OnCreate(ByVal hook As Object)<BR> '获取ArcMAP的Application实例<BR> Set m_pApplication = hook<BR>End Sub</P> <P>Private Property Get ICommand_Tooltip() As String<BR> ICommand_Tooltip = "MyTool"<BR>End Property </P> <P>Private Property Get ITool_Cursor() As esriCore.OLE_HANDLE<BR> ITool_Cursor = m_pCursor<BR>End Property</P> <P>Private Function ITool_Deactivate() As Boolean<BR> '如果ITool_Deactivate设为False,则Tool不可用<BR> ITool_Deactivate = True<BR>End Function</P> <P>Private Function ITool_OnContextMenu(ByVal X As Long, ByVal Y As Long) As Boolean<BR> '在这里可以加入用户代码,点击Mouse右键时显示一个定制的context menu <BR>End Function </P> <P>Private Sub ITool_OnDblClick()<BR> '在这里加入Mouse双击时的功能代码<BR>End Sub </P> <P>Private Sub ITool_OnKeyDown(ByVal keyCode As Long, ByVal Shift As Long)<BR>End Sub </P> <P>Private Sub ITool_OnKeyUp(ByVal keyCode As Long, ByVal Shift As Long)<BR>End Sub </P> <P 32pt; TEXT-INDENT: -32pt">Private Sub ITool_OnMouseDown(ByVal Button As Long, ByVal Shift As Long, _<BR>ByVal X As Long, ByVal Y As Long)<BR> '加入Mouse单击时的功能代码<BR> If Button = 1 Then<BR> Dim pPoint As IPoint<BR> Dim pMxApplication As IMxApplication<BR> Set pMxApplication = m_pApp<BR> Set pPoint=pMxApplication.Display.DisplayTransformation.ToMapPoint(X, Y)<BR> m_pApplication.StatusBar.Message(0) = Str(pPoint.X) ; "," ; Str(pPoint.Y)<BR> End If</P> <P>End Sub</P> <P>Private Sub ITool_OnMouseMove(ByVal Button As Long, ByVal Shift As Long, _<BR> ByVal X As Long, ByVal Y As Long)<BR> '加入Mouse移动时的功能代码<BR> m_pApplication.StatusBar.Message(0) = "ITool_OnMouseMove"<BR>End Sub</P> <P>Private Sub ITool_OnMouseUp(ByVal Button As Long, ByVal Shift As Long, _<BR> ByVal X As Long, ByVal Y As Long)<BR> '加入释放Mouse时的功能代码<BR> m_pApplication.StatusBar.Message(0) = "ITool_OnMouseUp"<BR>End Sub</P> <P>Private Sub ITool_Refresh(ByVal hDC As esriCore.OLE_HANDLE)<BR>End Sub <P>Private Property Get ICommand_Name() As String<BR> ICommand_Name = "MyCustomTool_MyTool"<BR>End Property</P> <P>Private Sub ICommand_OnClick()<BR> '加入按下按钮时实现的功能代码<BR> MsgBox "Clicked on my command"<BR>End Sub</P> <P>Private Sub ICommand_OnCreate(ByVal hook As Object)<BR> '获取ArcMAP的Application实例<BR> Set m_pApplication = hook<BR>End Sub</P> <P>Private Property Get ICommand_Tooltip() As String<BR> ICommand_Tooltip = "MyTool"<BR>End Property </P> <P>Private Property Get ITool_Cursor() As esriCore.OLE_HANDLE<BR> ITool_Cursor = m_pCursor<BR>End Property</P> <P>Private Function ITool_Deactivate() As Boolean<BR> '如果ITool_Deactivate设为False,则Tool不可用<BR> ITool_Deactivate = True<BR>End Function</P> <P>Private Function ITool_OnContextMenu(ByVal X As Long, ByVal Y As Long) As Boolean<BR> '在这里可以加入用户代码,点击Mouse右键时显示一个定制的context menu <BR>End Function </P> <P>Private Sub ITool_OnDblClick()<BR> '在这里加入Mouse双击时的功能代码<BR>End Sub </P> <P>Private Sub ITool_OnKeyDown(ByVal keyCode As Long, ByVal Shift As Long)<BR>End Sub </P> <P>Private Sub ITool_OnKeyUp(ByVal keyCode As Long, ByVal Shift As Long)<BR>End Sub </P> <P 32pt; TEXT-INDENT: -32pt">Private Sub ITool_OnMouseDown(ByVal Button As Long, ByVal Shift As Long, _<BR>ByVal X As Long, ByVal Y As Long)<BR> '加入Mouse单击时的功能代码<BR> If Button = 1 Then<BR> Dim pPoint As IPoint<BR> Dim pMxApplication As IMxApplication<BR> Set pMxApplication = m_pApp<BR> Set pPoint=pMxApplication.Display.DisplayTransformation.ToMapPoint(X, Y)<BR> m_pApplication.StatusBar.Message(0) = Str(pPoint.X) ; "," ; Str(pPoint.Y)<BR> End If</P> <P>End Sub</P> <P>Private Sub ITool_OnMouseMove(ByVal Button As Long, ByVal Shift As Long, _<BR> ByVal X As Long, ByVal Y As Long)<BR> '加入Mouse移动时的功能代码<BR> m_pApplication.StatusBar.Message(0) = "ITool_OnMouseMove"<BR>End Sub</P> <P>Private Sub ITool_OnMouseUp(ByVal Button As Long, ByVal Shift As Long, _<BR> ByVal X As Long, ByVal Y As Long)<BR> '加入释放Mouse时的功能代码<BR> m_pApplication.StatusBar.Message(0) = "ITool_OnMouseUp"<BR>End Sub</P> <P>Private Sub ITool_Refresh(ByVal hDC As esriCore.OLE_HANDLE)<BR>End Sub</P></TD></TR></TABLE></P> |
|
|
6楼#
发布于:2005-07-26 10:53
如何创建定制的工具条(Tool Bar)
<P 17.95pt">本例要实现的是如何创建定制的工具条(Tool Bar)。就必须在类模块中实现IToolBarDef接口。IToolBarDef接口包括 Caption、ItemCount及Name三个属性和GetItemInfo方法。</P> <P 21pt; TEXT-INDENT: -21pt">l 要点</P> <P 21pt">通过在类模块中实现IToolBarDef接口。IToolBarDef接口包括 Caption、ItemCount及Name三个属性和GetItemInfo方法。</P> <P 17.95pt">·ItemCount属性表示ToolBar显示的条目(Button、Tool或其它控件)数。 </P> <P 17.95pt">· GetItemInfo方法定义工具条上各条目的CLSID,其中,参数<I>pos</I>表示条目在ToolBar中的位置,<I>itemDef</I> 是定义相应位置的条目的IItemDef 对象。</P> <P 17.85pt">·工具条条目的CLSID分为两种:</P> <P 17.95pt">1、系统CLSID,代表ArcGIS的一个功能,其引用方式为"esriCore.命令名称",如"esriCore.AddDataCommand"、"esriCore.FileSaveCommand"等。</P> <P 17.95pt">2、用户定制CLSID,表示用户自己定义的功能。其引用方式为"工程名称.定制功能类名称",如" ToolBarDef.ClsBar "。必须注意,这里“定制功能类名称”是工程中实现的一个功能类名称,“工程名称”即为当前工程的名称(不是DLL文件名,也不是工具条的名称),每次新建一个工程时,系统默认的工程名在某些情况下无法使用(在中文版的VB中是一个乱字符),必须改名后方能用。</P> <P 0cm; TEXT-INDENT: 0cm">l 程序说明</P> <P 17.95pt">程序在类模块中实现IToolBarDef接口来创建自己的工具条(ToolBar)。</P> <P 21pt; TEXT-INDENT: -21pt">l 代码</P> <P> <TABLE height=43 width=541 align=center border=0> <TR> <TD align=left width=531> <P>Option Explicit<BR>Implements IToolBarDef </P> <P>Private Property Get IToolBarDef_Caption() As String<BR>IToolBarDef_Caption = "CustomToolBar"<BR>End Property</P> <P>Private Sub IToolBarDef_GetItemInfo(ByVal pos As Long, ByVal itemDef As_ esriCore.IItemDef)<BR> '这里假设在当前工程(工程名称为ToolBarDef)中定义了一个类模块(名为ClsBar),<BR> '它实现了Icommand接口(可参照1.2.1)<BR> Select Case pos<BR> Case 0<BR> '用户自定义条目<BR> itemDef.ID = "ToolBarDef.ClsBar"<BR> itemDef.Group = False<BR> Case 1<BR> '系统条目<BR> itemDef.ID = "esriCore.AddDataCommand"<BR> itemDef.Group = False<BR> End Select<BR>End Sub </P> <P 37.3pt; TEXT-INDENT: -37.3pt">Private Property Get IToolBarDef_ItemCount() As Long<BR>IToolBarDef_ItemCount = 2</P> <P>End Property </P> <P 37.3pt; TEXT-INDENT: -37.3pt">Private Property Get IToolBarDef_Name() As String<BR>IToolBarDef_Name = "CustomToolBar"</P> <P>End Property</P></TD></TR></TABLE></P> |
|
|
7楼#
发布于:2005-07-26 10:53
<P>本例要实现的是如何创建定制的MultiItem。</P>
<P 21pt; TEXT-INDENT: -21pt">l 要点</P> <P 0.05pt; TEXT-INDENT: 17.95pt">需要实现IMultiItem接口,但不需要同时实现Icommand接口。IMultiItem接口包括Caption,itemCaption,ItemBitmap,ItemEnabled,ItemChecked, Message及Name等属性和OnItemClick, OnPopup事件。</P> <P 17.95pt">·itemCaption,ItemBitmap,ItemEnabled,ItemChecked等属性的参数index表示当前Item的下标索引。</P> <P> ·OnPopup事件的参数hook同Icommand接口的OnCreate事件的参数hook一样,传入ArcGIS的Application实例,同时,该事件返回将要显示的Item数目。</P> <P 0.1pt; TEXT-INDENT: 17.85pt">·OnItemClick事件的参数Index表示用户当前点击的Item的索引,用户根据该索引分别定义点击各个Item时实现的功能。</P> <P 21pt; TEXT-INDENT: -21pt">l 程序说明</P> <P 21pt">程序在类模块中实现IMultiItem接口来创建定制自己的MultiItem。</P> <P 21pt; TEXT-INDENT: -21pt">l 代码</P> <P> <TABLE height=43 width=541 align=center border=0> <TR> <TD width=531> <P 37.4pt; TEXT-INDENT: -37.3pt">Option Explicit</P> <P 37.4pt; TEXT-INDENT: -37.3pt">Implements IMultiItem</P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private m_pApp As IApplication<BR>'ArcMap的Document</P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private m_pMxDoc As IMxDocument<BR>'当前Focus Map</P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private m_pMap As IMap<BR>'Map中的层数</P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private m_pLayerCnt As Long </P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private Property Get IMultiItem_Caption() As String<BR> IMultiItem_Caption = "ZoomToLayers"</P> <P 37.4pt; TEXT-INDENT: -37.3pt">End Property</P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private Property Get IMultiItem_HelpContextID() As Long</P> <P 37.4pt; TEXT-INDENT: -37.3pt">End Property </P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private Property Get IMultiItem_HelpFile() As String</P> <P 37.4pt; TEXT-INDENT: -37.3pt">End Property</P> <P 37.4pt; TEXT-INDENT: -37.3pt">Private Property Get IMultiItem_ItemBitmap(ByVal Index As Long) As esriCore.OLE_HANDLE</P> <P 0.05pt">End Property</P> <P 0.05pt">Private Property Get IMultiItem_ItemCaption(ByVal Index As Long) As String Dim i As Integer<BR> ' 遍历每一个层<BR> For i = 0 To m_pLayerCnt - 1<BR> ' 如果层号与当前Item的Index相同,就设置该Item的Caption<BR> If Index = i Then<BR> IMultiItem_ItemCaption = "Zoom to " ; m_pMap.Layer(i).Name<BR> End If<BR> Next<BR>End Property</P> <P 0.05pt">Private Property Get IMultiItem_ItemChecked(ByVal Index As Long) As Boolean<BR>End Property<BR> </P> <P 0.05pt">Private Property Get IMultiItem_ItemEnabled(ByVal Index As Long) As Boolean<BR> Dim i As Integer<BR> ' 遍历每一个层<BR> For i = 0 To m_pLayerCnt - 1<BR> '如果层号与当前Item的Index相同,则当前Item的Enable根据该层的Visible设置。<BR> If Index = i Then<BR> If m_pMap.Layer(i).Visible Then<BR> IMultiItem_ItemEnabled = True<BR> End If<BR> End If<BR> Next<BR>End Property</P> <P 0.05pt">Private Property Get IMultiItem_Message() As String<BR> IMultiItem_Message = "Zooms to the layer."<BR>End Property</P> <P 0.05pt">Private Property Get IMultiItem_Name() As String<BR> IMultiItem_Name = "ZoomMulti"<BR>End Property </P> <P>Private Sub IMultiItem_OnItemClick(ByVal Index As Long)<BR> Dim i As Integer<BR> Dim pEnv As IEnvelope<BR> Dim m_BookMark As IAOIBookmark<BR> ' 遍历每一个层<BR> For i = 0 To m_pLayerCnt – 1<BR> '如果层号与当前Item的Index相同,则以该层的AreaOfInterest 为范围执行Zoom<BR> If Index = i Then<BR> Set pEnv = m_pMap.Layer(i).AreaOfInterest<BR> Set m_BookMark = New AOIBookmark<BR> Set m_BookMark.Location = pEnv<BR> m_BookMark.ZoomTo m_pMap<BR> m_pMxDoc.ActiveView.Refresh<BR> End If<BR> Next<BR>End Sub </P> <P>Private Function IMultiItem_OnPopup(ByVal hook As Object) As Long<BR> Set m_pApp = hook<BR> ' 获取Map中的层数<BR> Set m_pMxDoc = m_pApp.Document<BR> Set m_pMap = m_pMxDoc.FocusMap<BR> m_pLayerCnt = m_pMap.LayerCount<BR> ' 显示的Item数等于层数<BR> IMultiItem_OnPopup = m_pLayerCnt<BR>End Function </P></TD></TR></TABLE></P> |
|
|
8楼#
发布于:2005-07-26 10:54
<P>本例要实现的是如何创建定制的菜单(Menu)</P>
<P 21pt; TEXT-INDENT: -21pt">l 要点</P> <P 18.1pt">用户通过在类模块中实现IMenuDef接口来创建定制的菜单(Menu),如果要使菜单出现在Customize Dialog的Menus类型中,必须同时实现IrootLevelMenu接口,它表明菜单为root menu。IMenuDef接口包括 Caption、ItemCount及Name三个属性和GetItemInfo方法。类似IToolBarDef(参照1.2.3)</P> <P 21pt; TEXT-INDENT: -21pt">l 程序说明</P> <P 21pt">程序在类模块中实现IMenuDef接口来创建定制的菜单(Menu)。</P> <P 21pt; TEXT-INDENT: -21pt">l 代码</P> <P> <TABLE height=43 width=541 align=center border=0> <TR> <TD width=531> <P>Option Explicit </P> <P> 'Implement the IMenuDef interface and IRootLevelMenu interface<BR> Implements IMenuDef<BR> Implements IRootLevelMenu </P> <P> Private Property Get IMenuDef_Caption() As String<BR> ' Set the string that appears as the menu's title<BR> IMenuDef_Caption = "MyMenu"<BR> End Property </P> <P> Private Sub IMenuDef_GetItemInfo(ByVal pos As Long, _<BR>ByVal itemDef As esriCore.IItemDef)<BR> ' Define the commands that will be on the menu. The built-in ArcMap<BR> ' Full Extent command, and Fixed Zoom In command are added to this custom menu.<BR> ' ID is the ClassID of the command. Group determines whether the command<BR> ' begins a new group on the menu<BR> Select Case pos<BR> Case 0<BR> itemDef.ID = "promenu.clsmultitem"<BR> itemDef.Group = False<BR> Case 1<BR> itemDef.ID = "esriCore.FullExtentCommand"<BR> itemDef.Group = True<BR> Case 2<BR> itemDef.ID = "esriCore.ZoomInFixedCommand"<BR> itemDef.Group = False<BR> End Select<BR> End Sub </P> <P> Private Property Get IMenuDef_ItemCount() As Long<BR> ' Set how many commands will be on the menu<BR> IMenuDef_ItemCount = 3<BR> End Property </P> <P> Private Property Get IMenuDef_Name() As String<BR> ' Set the internal name of the menu.<BR> IMenuDef_Name = "MyMenu"<BR> End Property</P></TD></TR></TABLE></P> |
|
|
9楼#
发布于:2005-07-26 10:55
<P>如何创建定制的ToolControl</P>
<P 17.95pt">本例要实现的是如何创建定制的ToolControl。ToolControl是指具有ComboBox的下拉列表 或 EditBox的编辑功能的一类控件。要创建定制的ToolControl,必须在类模块中实现<STRONG>ICommand</STRONG> 和 IToolControl接口。IToolControl接口包括<STRONG>hWnd</STRONG>属性和<STRONG>OnDrop</STRONG>, <STRONG>OnFocus</STRONG>事件。</P> <P 0cm; TEXT-INDENT: 0cm">l 要点</P> <P 17.95pt">·IToolControl接口的<STRONG>hWnd</STRONG>属性,接受一个Window Handle。</P> <P 17.95pt">·IToolControl接口的<STRONG>OnDrop事件,支持</STRONG>ToolControl的拖放,传入参数<I>barType</I>表示Bar类型。</P> <P 17.95pt">·IToolControl接口的<STRONG>OnFocus事件,传入</STRONG>IcompletionNotify类型的<STRONG>参数</STRONG><I>complete</I>,可以通过执行IcompletionNotify接口的SetComplete方法告之ArcMAP,ToolControl可以失去Focus。</P> <P 0cm; TEXT-INDENT: 0cm">l 程序说明</P> <P 21pt">本例中涉及三个模块,详细描述如下,其中,在类模块中实现了IToolBarDef接口来创建自己的ToolControl。</P> <P 21pt; TEXT-INDENT: -21pt">l 代码</P> <P> <TABLE height=43 width=541 align=center border=0> <TR> <TD width=531> <P>'1、frmImageCombo.frm模块,定义选中Combox某一项之后实现的功能。要求在Form上放置一个<BR>'ImageComb控件(名为ImageCombo1)和一个ImageList控件(名为ImageList1),并在ImageList1<BR>'中添加三张图片。</P> <P>Private Sub Form_Load()<BR> ' 设置ImageCombo1的选择Item<BR> Me.ImageCombo1.ImageList = Me.ImageList1<BR> Me.ImageCombo1.ComboItems.Add 1, "Red", "Red"<BR> Me.ImageCombo1.ComboItems.Add 2, "Blue", "Blue"<BR> Me.ImageCombo1.ComboItems.Add 3, "Green", "Green"<BR> Me.ImageCombo1.ComboItems(1).Image = 1<BR> Me.ImageCombo1.ComboItems(2).Image = 2<BR> Me.ImageCombo1.ComboItems(3).Image = 3<BR>End Sub </P> <P>Private Sub ImageCombo1_Click()<BR> ' 选择颜色<BR> Dim sel As Variant<BR> sel = Me.ImageCombo1.SelectedItem<BR> Dim color As Variant<BR> Select Case sel<BR> Case "Blue"<BR> color = vbBlue<BR> Case "Red"<BR> color = vbRed<BR> Case "Green"<BR> color = vbGreen<BR> End Select<BR> Dim pDocument As IMxDocument<BR> Set pDocument = g_pApplication.Document<BR> ' 设置颜色<BR> Dim pRgbColor As IrgbColor<BR> Set pRgbColor = New RgbColor<BR> pRgbColor.RGB = color<BR> ' 改变选中部分的颜色<BR> Dim pSelectionEnvironment As ISelectionEnvironment<BR> Set pSelectionEnvironment = New SelectionEnvironment<BR> Set pSelectionEnvironment.DefaultColor = pRgbColor<BR> ' 刷新视图<BR> pDocument.ActivatedView.Refresh<BR> ' 通知ArcMap,ToolControl现在可以失去Focus<BR> g_pCompletionNotify.SetComplete<BR>End Sub </P> <P>' 2、modPublicVars.bas模块,定义工程中用到的全局变量。<BR>Option Explicit<BR>Public g_pApplication As IApplication<BR>Public g_pCompletionNotify As IcompletionNotify</P> <P>' 3、CustImageCombo.cls模块,实现接口Icommand和IToolControl。<BR>Option Explicit<BR>Implements ICommand<BR>Implements IToolControl </P> <P>Private Property Get ICommand_Bitmap() As esriCore.OLE_HANDLE<BR>End Property </P> <P>Private Property Get ICommand_Caption() As String<BR> ICommand_Caption = "Custom ImageCombo"<BR>End Property </P> <P>Private Property Get ICommand_Category() As String<BR> ICommand_Category = "Developer Samples"<BR>End Property </P> <P>Private Property Get ICommand_Checked() As Boolean<BR>End Property </P> <P>Private Property Get ICommand_Enabled() As Boolean<BR> ICommand_Enabled = True<BR>End Property </P> <P>Private Property Get ICommand_HelpContextID() As Long<BR>End Property </P> <P>Private Property Get ICommand_HelpFile() As String<BR>End Property </P> <P>Private Property Get ICommand_Message() As String<BR> ICommand_Message = "Change feature selection color"<BR>End Property </P> <P>Private Property Get ICommand_Name() As String<BR> ICommand_Name = "DevelperSamples_CustomImageCombo"<BR>End Property </P> <P>Private Sub ICommand_OnClick()<BR>End Sub </P> <P>Private Sub ICommand_OnCreate(ByVal hook As Object)<BR> Set g_pApp = hook<BR>End Sub </P> <P>Private Property Get ICommand_Tooltip() As String<BR> ICommand_Tooltip = "Change Selection Color"<BR>End Property </P> <P>Private Property Get IToolControl_hWnd() As esriCore.OLE_HANDLE<BR> '将frmImageCombo.ImageCombo1的Window Handle赋给IToolControl_hWnd<BR>IToolControl_hWnd = frmImageCombo.ImageCombo1.hWnd<BR>End Property </P> <P 16pt; TEXT-INDENT: -16pt">Private Function IToolControl_OnDrop(ByVal barType As esriCore.esriCmdBarType) As Boolean '仅能将ToolControl拖放到ToolBar上<BR> If barType = esriCmdBarTypeToolbar Then<BR> IToolControl_OnDrop = True<BR> End If</P> <P 16pt; TEXT-INDENT: -16pt">End Function </P> <P>Private Sub IToolControl_OnFocus(ByVal complete As esriCore.ICompletionNotify)<BR> Set g_pCompletionNotify = complete<BR>End Sub</P></TD></TR></TABLE></P> |
|
|
上一页
下一页