




已閱讀5頁,還剩151頁未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
此文檔收集于網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系網(wǎng)站刪除1. 基礎(chǔ)篇1.1. 開發(fā)環(huán)境1.1.1. 如何在ArcMap的VBA環(huán)境中編程ArcMap是ArcGIS家族的成員之一,它內(nèi)置了一種集成編程環(huán)境VBA(Visaul Basic for Apllications)。通過VBA編程,用戶不但可以擴(kuò)展ArcMap的菜單、工具條等,而且可以完成大多數(shù)用戶的特定需求。ArcMap中VBA編程的方法有兩種,一種是寫VBA宏,另一種是創(chuàng)建UIControl并在其事件中寫入實(shí)現(xiàn)用戶需求的代碼。下面列出兩種方法的一般步驟。方法一:寫VBA宏(直接在VBA編輯器中編輯函數(shù)和過程)1、如圖1,單擊菜單欄中的命令,選擇的項(xiàng), 直接啟動(dòng)ArcMap的VBA編輯器;或者選擇的項(xiàng),進(jìn)入如圖2所示Macro對(duì)話框,在“Macro Name”文本框中輸入要?jiǎng)?chuàng)建的宏的名稱,并點(diǎn)按鈕,啟動(dòng)VBA編輯器。圖1 啟動(dòng)Macro對(duì)話框/啟動(dòng)VBA編輯器圖2 Macro對(duì)話框2、在圖3所示的窗口中,用戶可以根據(jù)實(shí)際選擇在Normal節(jié)點(diǎn)或者Project節(jié)點(diǎn)的ThisDocument、Forms、Modules中編寫宏(函數(shù)或過程),Normal節(jié)點(diǎn)下所寫的宏系統(tǒng)自動(dòng)保存,除非用戶刪除,否則它將始終存在并在任何工程中都有效;而在Project節(jié)點(diǎn)下所寫得宏隨工程保存(如不保存工程,則宏也將不被保存),并只在工程中有效。圖3 VBA編輯器(VBE)3、運(yùn)行VBA宏在VBA編輯器中寫好VBA代碼后,有兩種方式運(yùn)行:第一,點(diǎn)擊VBA編輯器工具條中的(運(yùn)行)按鈕,可立即運(yùn)行寫好的代碼;第二,退出VBA編輯器,重新啟動(dòng)Macro對(duì)話框,如圖2,選擇要運(yùn)行的VBA宏名稱,點(diǎn)擊按鈕即可運(yùn)行相應(yīng)的VBA宏。方法二:創(chuàng)建UIControl(交互式VBA編程) 1、用鼠標(biāo)右擊任何工具欄(條),在彈出的上托式菜單中選擇菜單項(xiàng),如圖4,進(jìn)入圖5所示的Customize對(duì)話框。圖4 啟動(dòng)“Customize”對(duì)話框2、切換到“Customize”對(duì)話框的“Commands”頁,選中“UIControls”后點(diǎn)擊按鈕,進(jìn)入圖6所示的“New UIControl”對(duì)話框。3、在“New UIControl”對(duì)話框中,用戶可根據(jù)需要選擇UIControl類型:UIButtonControl:創(chuàng)建Button;UIToolControl:創(chuàng)建與Map交互的Tool;UIEditBoxControl:創(chuàng)建EditBox;UIComboBoxControl:創(chuàng)建ComboBox。最后點(diǎn)擊按鈕只創(chuàng)建UIControl或者點(diǎn)擊按鈕創(chuàng)建UIControl并進(jìn)入VBA編輯器。與方法一不同,此時(shí)應(yīng)在UIControl的事件中進(jìn)行VBA編程。圖5 Customize對(duì)話框圖6 New UIControl對(duì)話框4、UIControl創(chuàng)建后,在圖5所示的“Customize”對(duì)話框選中UIControl并將其拖置到任意工具條上,用戶便可象使用系統(tǒng)已有的Control一樣使用所創(chuàng)建的UIControl。1.1.2. 如何在VB環(huán)境中利用ArcObjects組件開發(fā)ActiveX DLL1.1.1節(jié)討論了如何在ArcGis的VBA環(huán)境中編程,雖然通過這種方式可以完成大多數(shù)用戶的定制需求,但是,在某些情況下,對(duì)于特殊的應(yīng)用,用戶需要脫離ArcGIS環(huán)境而在VB開發(fā)環(huán)境中開發(fā)外部獨(dú)立的應(yīng)用程序,這種外部獨(dú)立的應(yīng)用程序有兩種形式: ActiveX DLL和Standard EXE。Standard EXE的開發(fā)將在1.1.4中討論,本節(jié)將討論ActiveX DLL的開發(fā),其關(guān)鍵是引用ArcObjects對(duì)象庫和實(shí)現(xiàn)ArcObjects接口(例如ICommand,ITool,IToolBar等)。下面介紹在VB環(huán)境利用ArcObjects組件開發(fā)ActiveX DLL的一般步驟。1、啟動(dòng)VB開發(fā)環(huán)境,在圖7所示的“New Project”對(duì)話框中選擇“ActiveX DLL”項(xiàng),并點(diǎn)擊按鈕,進(jìn)入VBE環(huán)境。圖7 New Project對(duì)話框2、引用ArcObjects對(duì)象庫:首先點(diǎn)擊菜單中的項(xiàng),如圖8,進(jìn)入對(duì)象庫引用對(duì)話框,如圖9。圖8 啟動(dòng)對(duì)象庫引用對(duì)話框圖9 對(duì)象庫引用對(duì)話框3、對(duì)象庫引用對(duì)話框(圖9)中選中“Esri ArcMap Object Library ”和“Esri Object Library” 兩項(xiàng),并點(diǎn)擊按鈕,返回VBE環(huán)境。4、一般在類模塊中寫入實(shí)現(xiàn)特定ArcObjects接口的代碼,如圖10,然后運(yùn)行菜單中的項(xiàng),生成DLL文件,如圖11。(project1.dll隨項(xiàng)目名改變)。圖10 類模塊編輯窗口圖11 生成DLL文件1.1.3. 如何在ArcMap中加載利用ArcObjects組件開發(fā)的ActiveX DLL用戶通過1.1.2中介紹的方法開發(fā)好一個(gè)ActiveX DLL程序后,便可根據(jù)實(shí)際需要,在ArcMap環(huán)境下加載這個(gè)ActiveX DLL程序。其一般步驟如下:1、用鼠標(biāo)右擊任何工具欄(條),點(diǎn)擊彈出的上托式菜單中的菜單項(xiàng)(參見圖4)。2、在Customize對(duì)話框中,根據(jù)被加載DLL的類型切換到“Toolbars”或者“Commands”頁(參見圖5),然后點(diǎn)擊按鈕。3、在“打開文件”對(duì)話框中(Windows通用“打開文件”對(duì)話框,圖略),選擇被加載的Dll文件,并點(diǎn)擊按鈕。4、如果加載是“Commands”,則在圖5所示的對(duì)話框中顯示加載的Command,并可以將其拖置于任何工具條上;如果加載是“ToolBars”,則在圖12所示的對(duì)話框中顯示加載的ToolBar,選中后即可在ArcMap中顯示。圖12 加載ToolBar1.1.4. 如何在VB環(huán)境中利用ArcObjects控件開發(fā)EXE利用ArcObjects控件開發(fā)EXE的前三步類似于1.1.2中開發(fā)“Acrtive Dll”的前三步,唯一不同的是在“New Project”對(duì)話框中選擇“Standard EXE”。4、點(diǎn)擊菜單項(xiàng)中的項(xiàng),打開“Components”對(duì)話框,如圖13。圖13 打開Components對(duì)話框5、在“Components”對(duì)話框中,切換到Controls頁,并選中“ESRI MapControl”項(xiàng),點(diǎn)擊或按鈕,如圖14。圖14 Components對(duì)話框6、如圖15所示,加載MapControl控件之后,在VBE的控件面板中出現(xiàn)了MapControl控件圖標(biāo),用戶便可以象在Form中添加Button一樣在Form中添加MapControl控件,并利用它開發(fā)EXE。圖15 添加MapControl控件1.2. 用戶界面1.2.1. 如何創(chuàng)建定制的按鈕(Button)本例要實(shí)現(xiàn)的是如何創(chuàng)建定制的按鈕(Button)。l 要點(diǎn)用戶通過在類模塊中實(shí)現(xiàn)ICommand接口來創(chuàng)建定制的按鈕(COM command)。ICommand接口包括 caption、 name、 category、 bitmap、 message(StatusBarr的提示信息)、 tooltip(微幫助)、 help context id 、help file、enabled以及checked等十個(gè)屬性和OnCreate、OnClick兩個(gè)事件。從Icommand接口的OnCreate事件中獲取的ArcMap的Application實(shí)例必須用一個(gè)公共變量保存,以便在其它事件中(或者其它接口的事件中甚至整個(gè)工程中)使用。OnCreate事件的參數(shù)hook傳入的是一個(gè)Object,也就是ArcMAP的Application實(shí)例,可把它賦給一個(gè)IApplication接口的變量,便獲得了ArcMAP的實(shí)例。在OnClick事件中寫入相關(guān)代碼,表示按下按鈕時(shí)要實(shí)現(xiàn)的功能.l 程序說明程序在類模塊中實(shí)現(xiàn)Icommand接口來創(chuàng)建自己的按鈕(Button)l 代碼Option Explicit實(shí)現(xiàn)Icommand接口Implements ICommandDim m_pPicture as PictureDim m_pApplication As IApplicationPrivate Sub Class_Initialize() 調(diào)入.RES文件中ID為101的BitMap作為該按鈕的顯示圖片 Set m_pPicture = LoadResPicture(101, vbResBitmap)End SubPrivate Property Get ICommand_Bitmap() As esriCore.OLE_HANDLE ICommand_Bitmap = m_pPictureEnd PropertyPrivate Property Get ICommand_Caption() As String ICommand_Caption = Create ButtonEnd PropertyPrivate Property Get ICommand_Category() As String ICommand_Category = Create Button End PropertyPrivate Property Get ICommand_Checked() As BooleanEnd PropertyPrivate Property Get ICommand_Enabled() As Boolean ICommand_Enabled = TrueEnd PropertyPrivate Property Get ICommand_HelpContextID() As LongEnd PropertyPrivate Property Get ICommand_HelpFile() As StringEnd PropertyPrivate Property Get ICommand_Message() As StringEnd PropertyPrivate Property Get ICommand_Name() As StringICommand_Name = CreateButton End PropertyPrivate Sub ICommand_OnClick() 加入按下按鈕時(shí)實(shí)現(xiàn)的功能代碼。在這里,按鈕按下時(shí)顯示ArcMap的Document的Tittle Dim pDocument As IDocument Set pDocument = m_pApplication.Document MsgBox pDocument.TitleEnd SubPrivate Sub ICommand_OnCreate(ByVal hook As Object) 獲取ArcMap的Application實(shí)例 Set m_pApplication = hookEnd SubPrivate Property Get ICommand_Tooltip() As String ICommand_Tooltip = Create Button End Property1.2.2. 如何創(chuàng)建定制的Tool本例要實(shí)現(xiàn)的是如何創(chuàng)建定制的Tooll 要點(diǎn)用戶在類模塊中實(shí)現(xiàn)Icommand(參見1.2.1)和ITool接口。ITool接口包括 mouse move, mouse button press/release, keyboard key press/release, double-click以及right click等事件、Cursor屬性和Refresh方法。Tool既具有Button的功能,又具有與ArcMAP界面交互的功能,Button的功能代碼必須寫在Icommand的OnClick事件中,而所有實(shí)現(xiàn)交互功能的代碼必須寫在Itool接口的各個(gè)事件中。Itool接口的各個(gè)事件,用戶可以在其中寫入相關(guān)代碼,表示用戶與ArcMAP界面交互時(shí)一旦觸發(fā)某事件要實(shí)現(xiàn)的功能。l 程序說明 程序在類模塊中實(shí)現(xiàn)Icommand和Itool接口來創(chuàng)建自己的Tool.l 代碼Option Explicit實(shí)現(xiàn)Icommand和Itool接口Implements ICommandImplements IToolDim m_pApplication As IApplication Dim m_pBitmap As IPictureDispDim m_pCursor As IpictureDispPrivate Sub Class_Initialize() Set m_pBitmap = LoadResPicture(101, 0) 從.RES文件中調(diào)入ID為102的圖片作為按下Tool后的MouseCursor Set m_pCursor = LoadResPicture(102, 2)End SubPrivate Property Get ICommand_Bitmap() As esriCore.OLE_HANDLE ICommand_Bitmap = m_pBitmapEnd PropertyPrivate Property Get ICommand_Caption() As String ICommand_Caption = MyToolEnd PropertyPrivate Property Get ICommand_Category() As String ICommand_Category = MyCustomToolsEnd PropertyPrivate Property Get ICommand_Checked() As BooleanEnd PropertyPrivate Property Get ICommand_Enabled() As Boolean ICommand_Enabled = TrueEnd PropertyPrivate Property Get ICommand_HelpContextID() As LongEnd PropertyPrivate Property Get ICommand_HelpFile() As StringEnd PropertyPrivate Property Get ICommand_Message() As String ICommand_Message = This is my custom toolEnd PropertyPrivate Property Get ICommand_Name() As String ICommand_Name = MyCustomTool_MyToolEnd PropertyPrivate Sub ICommand_OnClick() 加入按下按鈕時(shí)實(shí)現(xiàn)的功能代碼 MsgBox Clicked on my commandEnd SubPrivate Sub ICommand_OnCreate(ByVal hook As Object) 獲取ArcMAP的Application實(shí)例 Set m_pApplication = hookEnd SubPrivate Property Get ICommand_Tooltip() As String ICommand_Tooltip = MyToolEnd PropertyPrivate Property Get ITool_Cursor() As esriCore.OLE_HANDLE ITool_Cursor = m_pCursorEnd PropertyPrivate Function ITool_Deactivate() As Boolean 如果ITool_Deactivate設(shè)為False,則Tool不可用 ITool_Deactivate = TrueEnd FunctionPrivate Function ITool_OnContextMenu(ByVal X As Long, ByVal Y As Long) As Boolean 在這里可以加入用戶代碼,點(diǎn)擊Mouse右鍵時(shí)顯示一個(gè)定制的context menu End FunctionPrivate Sub ITool_OnDblClick() 在這里加入Mouse雙擊時(shí)的功能代碼End SubPrivate Sub ITool_OnKeyDown(ByVal keyCode As Long, ByVal Shift As Long)End SubPrivate Sub ITool_OnKeyUp(ByVal keyCode As Long, ByVal Shift As Long)End SubPrivate Sub ITool_OnMouseDown(ByVal Button As Long, ByVal Shift As Long, _ByVal X As Long, ByVal Y As Long) 加入Mouse單擊時(shí)的功能代碼 If Button = 1 Then Dim pPoint As IPoint Dim pMxApplication As IMxApplication Set pMxApplication = m_pApp Set pPoint=pMxApplication.Display.DisplayTransformation.ToMapPoint(X, Y) m_pApplication.StatusBar.Message(0) = Str(pPoint.X) & , & Str(pPoint.Y) End IfEnd SubPrivate Sub ITool_OnMouseMove(ByVal Button As Long, ByVal Shift As Long, _ByVal X As Long, ByVal Y As Long) 加入Mouse移動(dòng)時(shí)的功能代碼 m_pApplication.StatusBar.Message(0) = ITool_OnMouseMoveEnd SubPrivate Sub ITool_OnMouseUp(ByVal Button As Long, ByVal Shift As Long, _ByVal X As Long, ByVal Y As Long) 加入釋放Mouse時(shí)的功能代碼 m_pApplication.StatusBar.Message(0) = ITool_OnMouseUpEnd SubPrivate Sub ITool_Refresh(ByVal hDC As esriCore.OLE_HANDLE)End Sub1.2.3. 如何創(chuàng)建定制的工具條(Tool Bar)本例要實(shí)現(xiàn)的是如何創(chuàng)建定制的工具條(Tool Bar)。就必須在類模塊中實(shí)現(xiàn)IToolBarDef接口。IToolBarDef接口包括 Caption、ItemCount及Name三個(gè)屬性和GetItemInfo方法。l 要點(diǎn)通過在類模塊中實(shí)現(xiàn)IToolBarDef接口。IToolBarDef接口包括 Caption、ItemCount及Name三個(gè)屬性和GetItemInfo方法。ItemCount屬性表示ToolBar顯示的條目(Button、Tool或其它控件)數(shù)。 GetItemInfo方法定義工具條上各條目的CLSID,其中,參數(shù)pos表示條目在ToolBar中的位置,itemDef 是定義相應(yīng)位置的條目的IItemDef 對(duì)象。工具條條目的CLSID分為兩種:1、系統(tǒng)CLSID,代表ArcGIS的一個(gè)功能,其引用方式為esriCore.命令名稱,如esriCore.AddDataCommand、esriCore.FileSaveCommand等。2、用戶定制CLSID,表示用戶自己定義的功能。其引用方式為工程名稱.定制功能類名稱,如 ToolBarDef.ClsBar 。必須注意,這里“定制功能類名稱”是工程中實(shí)現(xiàn)的一個(gè)功能類名稱,“工程名稱”即為當(dāng)前工程的名稱(不是DLL文件名,也不是工具條的名稱),每次新建一個(gè)工程時(shí),系統(tǒng)默認(rèn)的工程名在某些情況下無法使用(在中文版的VB中是一個(gè)亂字符),必須改名后方能用。l 程序說明程序在類模塊中實(shí)現(xiàn)IToolBarDef接口來創(chuàng)建自己的工具條(ToolBar)。l 代碼Option ExplicitImplements IToolBarDefPrivate Property Get IToolBarDef_Caption() As StringIToolBarDef_Caption = CustomToolBarEnd PropertyPrivate Sub IToolBarDef_GetItemInfo(ByVal pos As Long, ByVal itemDef As _esriCore.IItemDef)這里假設(shè)在當(dāng)前工程(工程名稱為ToolBarDef)中定義了一個(gè)類模塊(名為ClsBar),它實(shí)現(xiàn)了Icommand接口(可參照1.2.1)Select Case posCase 0 用戶自定義條目 itemDef.ID = ToolBarDef.ClsBar itemDef.Group = FalseCase 1 系統(tǒng)條目 itemDef.ID = esriCore.AddDataCommand itemDef.Group = FalseEnd SelectEnd SubPrivate Property Get IToolBarDef_ItemCount() As LongIToolBarDef_ItemCount = 2End PropertyPrivate Property Get IToolBarDef_Name() As StringIToolBarDef_Name = CustomToolBarEnd Property1.2.4. 如何創(chuàng)建定制的MultiItem本例要實(shí)現(xiàn)的是如何創(chuàng)建定制的MultiItem。l 要點(diǎn)需要實(shí)現(xiàn)IMultiItem接口,但不需要同時(shí)實(shí)現(xiàn)Icommand接口。IMultiItem接口包括Caption,itemCaption,ItemBitmap,ItemEnabled,ItemChecked, Message及Name等屬性和OnItemClick, OnPopup事件。itemCaption,ItemBitmap,ItemEnabled,ItemChecked等屬性的參數(shù)index表示當(dāng)前Item的下標(biāo)索引。 OnPopup事件的參數(shù)hook同Icommand接口的OnCreate事件的參數(shù)hook一樣,傳入ArcGIS的Application實(shí)例,同時(shí),該事件返回將要顯示的Item數(shù)目。OnItemClick事件的參數(shù)Index表示用戶當(dāng)前點(diǎn)擊的Item的索引,用戶根據(jù)該索引分別定義點(diǎn)擊各個(gè)Item時(shí)實(shí)現(xiàn)的功能。l 程序說明程序在類模塊中實(shí)現(xiàn)IMultiItem接口來創(chuàng)建定制自己的MultiItem。l 代碼Option ExplicitImplements IMultiItemPrivate m_pApp As IApplicationArcMap的DocumentPrivate m_pMxDoc As IMxDocument當(dāng)前Focus MapPrivate m_pMap As IMapMap中的層數(shù)Private m_pLayerCnt As LongPrivate Property Get IMultiItem_Caption() As String IMultiItem_Caption = ZoomToLayersEnd PropertyPrivate Property Get IMultiItem_HelpContextID() As LongEnd PropertyPrivate Property Get IMultiItem_HelpFile() As StringEnd PropertyPrivate Property Get IMultiItem_ItemBitmap(ByVal Index As Long) As esriCore.OLE_HANDLEEnd PropertyPrivate Property Get IMultiItem_ItemCaption(ByVal Index As Long) As String Dim i As Integer 遍歷每一個(gè)層 For i = 0 To m_pLayerCnt - 1 如果層號(hào)與當(dāng)前Item的Index相同,就設(shè)置該Item的Caption If Index = i Then IMultiItem_ItemCaption = Zoom to & m_pMap.Layer(i).Name End If NextEnd PropertyPrivate Property Get IMultiItem_ItemChecked(ByVal Index As Long) As BooleanEnd PropertyPrivate Property Get IMultiItem_ItemEnabled(ByVal Index As Long) As Boolean Dim i As Integer 遍歷每一個(gè)層 For i = 0 To m_pLayerCnt - 1 如果層號(hào)與當(dāng)前Item的Index相同,則當(dāng)前Item的Enable根據(jù)該層的Visible設(shè)置。 If Index = i Then If m_pMap.Layer(i).Visible Then IMultiItem_ItemEnabled = True End If End If NextEnd PropertyPrivate Property Get IMultiItem_Message() As String IMultiItem_Message = Zooms to the layer.End PropertyPrivate Property Get IMultiItem_Name() As String IMultiItem_Name = ZoomMultiEnd PropertyPrivate Sub IMultiItem_OnItemClick(ByVal Index As Long) Dim i As Integer Dim pEnv As IEnvelope Dim m_BookMark As IAOIBookmark 遍歷每一個(gè)層 For i = 0 To m_pLayerCnt 1 如果層號(hào)與當(dāng)前Item的Index相同,則以該層的AreaOfInterest 為范圍執(zhí)行Zoom If Index = i Then Set pEnv = m_pMap.Layer(i).AreaOfInterest Set m_BookMark = New AOIBookmark Set m_BookMark.Location = pEnv m_BookMark.ZoomTo m_pMap m_pMxDoc.ActiveView.Refresh End If NextEnd SubPrivate Function IMultiItem_OnPopup(ByVal hook As Object) As Long Set m_pApp = hook 獲取Map中的層數(shù) Set m_pMxDoc = m_pApp.Document Set m_pMap = m_pMxDoc.FocusMap m_pLayerCnt = m_pMap.LayerCount 顯示的Item數(shù)等于層數(shù) IMultiItem_OnPopup = m_pLayerCntEnd Function1.2.5. 如何創(chuàng)建定制的菜單(Menu)本例要實(shí)現(xiàn)的是如何創(chuàng)建定制的菜單(Menu)。l 要點(diǎn)用戶通過在類模塊中實(shí)現(xiàn)IMenuDef接口來創(chuàng)建定制的菜單(Menu),如果要使菜單出現(xiàn)在Customize Dialog的Menus類型中,必須同時(shí)實(shí)現(xiàn)IrootLevelMenu接口,它表明菜單為root menu。IMenuDef接口包括 Caption、ItemCount及Name三個(gè)屬性和GetItemInfo方法。類似IToolBarDef(參照1.2.3)l 程序說明程序在類模塊中實(shí)現(xiàn)IMenuDef接口來創(chuàng)建定制的菜單(Menu)。l 代碼Option Explicit Implement the IMenuDef interface and IRootLevelMenu interface Implements IMenuDef Implements IRootLevelMenu Private Property Get IMenuDef_Caption() As String Set the string that appears as the menus title IMenuDef_Caption = MyMenu End Property Private Sub IMenuDef_GetItemInfo(ByVal pos As Long, _ByVal itemDef As esriCore.IItemDef) Define the commands that will be on the menu. The built-in ArcMap Full Extent command, and Fixed Zoom In command are added to this custom menu. ID is the ClassID of the command. Group determines whether the command begins a new group on the menu Select Case pos Case 0 itemDef.ID = promenu.clsmultitem itemDef.Group = False Case 1 itemDef.ID = esriCore.FullExtentCommand itemDef.Group = True Case 2 itemDef.ID = esriCore.ZoomInFixedCommand itemDef.Group = False End Select End Sub Private Property Get IMenuDef_ItemCount() As Long Set how many commands will be on the menu IMenuDef_ItemCount = 3 End Property Private Property Get IMenuDef_Name() As String Set the internal name of the menu. IMenuDef_Name = MyMenu End Property1.2.6. 如何創(chuàng)建定制的ToolControl本例要實(shí)現(xiàn)的是如何創(chuàng)建定制的ToolControl。ToolControl是指具有ComboBox的下拉列表 或 EditBox的編輯功能的一類控件。要?jiǎng)?chuàng)建定制的ToolControl,必須在類模塊中實(shí)現(xiàn)ICommand 和 IToolControl接口。IToolControl接口包括hWnd屬性和OnDrop, OnFocus事件。l 要點(diǎn)IToolControl接口的hWnd屬性,接受一個(gè)Window Handle。IToolControl接口的OnDrop事件,支持ToolControl的拖放,傳入?yún)?shù)barType表示Bar類型。IToolControl接口的OnFocus事件,傳入IcompletionNotify類型的參數(shù)complete,可以通過執(zhí)行IcompletionNotify接口的SetComplete方法告之ArcMAP,ToolControl可以失去Focus。l 程序說明本例中涉及三個(gè)模塊,詳細(xì)描述如下,其中,在類模塊中實(shí)現(xiàn)了IToolBarDef接口來創(chuàng)建自己的ToolControl。l 代碼1、frmImageCombo.frm模塊,定義選中Combox某一項(xiàng)之后實(shí)現(xiàn)的功能。要求在Form上放置一個(gè)ImageComb控件(名為ImageCombo1)和一個(gè)ImageList控件(名為ImageList1),并在ImageList1中添加三張圖片。Private Sub Form_Load() 設(shè)置ImageCombo1的選擇Item Me.ImageCombo1.ImageList = Me.ImageList1 Me.ImageCombo1.ComboItems.Add 1, Red, Red Me.ImageCombo1.ComboItems.Add 2, Blue, Blue Me.ImageCombo1.ComboItems.Add 3, Green, Green Me.ImageCombo1.ComboItems(1).Image = 1 Me.ImageCombo1.ComboItems(2).Image = 2 Me.ImageCombo1.ComboItems(3).Image = 3End SubPrivate Sub ImageCombo1_Click() 選擇顏色 Dim sel As Variant sel = Me.ImageCombo1.SelectedItem Dim color As Variant Select Case sel Case Blue color = vbBlue Case Red color = vbRed Case Green color = vbGreen End Select Dim pDocument As IMxDocument Set pDocument = g_pApplication.Document 設(shè)置顏色Dim pRgbColor As IrgbColor Set pRgbColor = New RgbColor pRgbColor.RGB = color 改變選中部分的顏色 Dim pSelectionEnvironment As ISelectionEnvironment Set pSelectionEnvironment = New SelectionEnvironment Set pSelectionEnvironment.DefaultColor = pRgbColor 刷新視圖pDocument.ActivatedView.Refresh 通知ArcMap,ToolControl現(xiàn)在可以失去Focus g_pCompletionNotify.SetCompleteEnd Sub 2、modPublicVars.bas模塊,定義工程中用到的全局變量。Option ExplicitPublic g_pApplication As IApplicationPublic g_pCompletionNotify As IcompletionNotify 3、CustImageCombo.cls模塊,實(shí)現(xiàn)接口Icommand和IToolControl。Option ExplicitImplements ICommandImplements IToolControlPrivate Property Get ICommand_Bitmap() As esriCore.OLE_HANDLEEnd PropertyPrivate Property Get ICommand_Caption() As String ICommand_Caption = Custom ImageComboEnd PropertyPrivate Property Get ICommand_Category() As String ICommand_Category = Developer SamplesEnd PropertyPrivate Property Get ICommand_Checked() As BooleanEnd PropertyPrivate Property Get ICommand_Enabled() As Boolean ICommand_Enabled = TrueEnd PropertyPrivate Property Get ICommand_HelpContextID() As LongEnd PropertyPrivate Property Get ICommand_HelpFile() As StringEnd PropertyPrivate Property Get ICommand_Message() As String ICommand_Message = Change feature selection colorEnd PropertyPrivate Property Get ICommand_Name() As String ICommand_Name = DevelperSamples_CustomImageComboEnd PropertyPrivate Sub ICommand_OnClick()End SubPrivate Sub ICommand_OnCreate(ByVal hook As Object) Set g_pApp = hookEnd SubPrivate Property Get ICommand_Tooltip() As String ICommand_Tooltip = Change Selection ColorEnd PropertyPrivate Property Get IToolControl_hWnd() As esriCore.OLE_HANDLE 將frmImageCombo.ImageCombo1的Window Handle賦給IToolControl_hWndIToolControl_hWnd = frmImageCombo.ImageCombo1.hWndEnd PropertyPrivate Function IToolControl_OnDrop(ByVal barType As esriCore.esriCmdBarType) As Boolean 僅能將ToolControl拖放到ToolBar上 If barType = esriCmdBarTypeToolbar Then IToolCon
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 自動(dòng)控制原理(第2版)(余成波-張蓮-胡曉倩)習(xí)題全解及MATLAB實(shí)驗(yàn)-第1、2章習(xí)題解答
- 計(jì)量管理制度范文
- 湖南省株洲市攸縣第三中學(xué)2024-2025學(xué)年高三下學(xué)期5月期中地理試題(含答案)
- 設(shè)備操作規(guī)程匯編
- 高一年級(jí)5月月考地理 試題
- 幼兒園 疫情防控主題班會(huì)教案
- 建筑施工特種作業(yè)-建筑起重機(jī)械安裝拆卸工(塔式起重機(jī))真題庫-3
- 建筑施工特種作業(yè)-建筑焊工真題庫-5
- 廈門物理初中題目及答案
- 日語初級(jí)助詞題目及答案
- 跨區(qū)域就讀證明
- 國開期末考試《建筑制圖基礎(chǔ)》機(jī)考試題及答案(第D-1套)
- SA8000-2014社會(huì)責(zé)任績效委員會(huì)SPT組織架構(gòu)、職責(zé)和定期檢討及評(píng)審會(huì)議記錄
- 學(xué)術(shù)論文寫作規(guī)范與技巧課件
- 生物高中-基于大數(shù)據(jù)分析的精準(zhǔn)教學(xué)課件
- 焊接熱處理工藝卡
- 公共政策學(xué)(第三版)-課件
- 齊魯醫(yī)學(xué)Lisfranc-損傷
- 大型鋼網(wǎng)架整體提升施工工法
- 干熄焦?fàn)t內(nèi)固_氣流動(dòng)與傳熱數(shù)值模擬畢業(yè)論文
- 公司股東變更登記申請(qǐng)書(一變一)
評(píng)論
0/150
提交評(píng)論