ArcGIS Engine C#實例開發(fā)教程_第1頁
ArcGIS Engine C#實例開發(fā)教程_第2頁
ArcGIS Engine C#實例開發(fā)教程_第3頁
ArcGIS Engine C#實例開發(fā)教程_第4頁
ArcGIS Engine C#實例開發(fā)教程_第5頁
已閱讀5頁,還剩47頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、ArcGISEngine+C#實例開發(fā)教程目 錄第一講 桌面GIS應用程序框架的建立2第二講 菜單的添加及其實現(xiàn)5第三講 MapControl與PageLayoutControl同步 9第四講 狀態(tài)欄信息的添加與實現(xiàn)24第五講 鷹眼的實現(xiàn)27第六講 右鍵菜單添加與實現(xiàn)32教程Bug及優(yōu)化方案1 40第七講 圖層符號選擇器的實現(xiàn)140第七講 圖層符號選擇器的實現(xiàn)257第一講 桌面GIS應用程序框架的建立本講主要是使用MapControl、PageLayoutControl、ToolbarControl、TOCControl四個控件建立起基本的桌面GIS應用程序框架。最終成果預覽如下:1、新建項目

2、啟動VS2005,選擇“文件|新建|項目”,在項目類型中選擇VisualC#,再選擇Windows應用程序模板,輸入名稱“3sdnMap”,點擊確定。在解決方案管理器中將“Form1.cs”重命名為“3sdnMap.cs”,在設計視圖中,選中窗體,將其屬性中的“Text”改為“3sdnMap”。2、添加控件選擇工具箱中的“菜單和工具欄|MenuStrip”,將其拖入窗體。選擇工具箱中的“ArcGISWindowsForms”節(jié),將“ToolbarControl”控件拖入窗體,并將其屬性中的Dock設置為Top。選擇工具箱中的“菜單和工具欄|StatusStrip”,將其拖入到窗體。選擇工具箱中

3、的“容器|SplitContainer”容器拖入窗體,并將其屬性中的Dock設置為Fill。將TabControl控件拖入Panel1,將Alignment屬性設置為Bottom,Dock屬性設置為Fill。點擊TabPages屬性右邊的按鈕,彈出TabPage集合編輯器,將tabPage1的Name設置為tabPageLayer,Text設置為圖層,將tabPage2的Name設置為tabPageProperty,Text設置為屬性。如下所示。選擇“圖層”選項卡,拖入TOCControl控件,設置Dock屬性為Fill。選擇“屬性”選項卡,拖入DataGridView控件,設置Dock屬性為

4、Fill。拖入TabControl控件到Panel2,設置Dock屬性為Fill。并上述類似的方法,將兩個選項卡的Name和Text分別設置為:(tabPageMap、地圖),(tabPageLayout,制版)。選擇“地圖”選項卡,拖入MapControl控件,設置Dock屬性為Fill。選擇“制版”選項卡,拖入PageLayoutControl控件,設置Dock屬性為Fill。最后將LicenseControl控件拖入到窗體的任意地方。按F5編譯運行,可以看到剛才布局好的程序界面了。3、控件綁定通過以上步驟添加的控件還只是單獨存在,而我們的程序需要各控件間協(xié)同工作,因此要進行控件綁定。分別

5、右擊ToolbarControl、TOCControl控件,將Buddy設置為axMapControl1,如下圖所示。這樣,工具條和圖層控件就與地圖控件關聯(lián)了。4、添加工具此時,工具條中還沒有任何工具,添加的方法也很簡單。右擊ToolbarControl,選擇“屬性|Items”,點擊Add,選擇Commands選項卡中的Generic,雙擊Open、SaveAs、Redo、Undo即可將相應工具添加到工具條常見的工具有:MapNavigation中的導航工具,MapInquiry中的查詢工具,F(xiàn)eatureSelection中的選擇工具,你可以根據(jù)需要酌情添加工具。5、編譯運行按F5即可編譯

6、運行程序,至此桌面GIS應用程序框架基本框架已經(jīng)搭建好了,你可以通過工具條的工具打開地圖文檔,瀏覽地圖了,效果如開篇所示.第二講 菜單的添加及其實現(xiàn)在上一講中,我們實現(xiàn)了應用程序基本框架,其中有個小錯誤,在此先跟大家說明下。在“屬性”選項卡中,我們當時添加的是DataGridView控件,這個控件是用來顯示數(shù)據(jù)表的,而專門用于屬性的查詢和設置的控件是PropertyGrid控件。因此請你刪除“屬性”選項卡中的DataGridView控件,再把位于“工具箱|所有Windows窗體|PropertyGrid”(如果沒有,右擊選擇“選擇項”以添加此控件)控件拖到該選項卡。在這一講中,主要講解菜單的添

7、加和實現(xiàn)。1、添加菜單在設計視圖中,單擊菜單欄,會出現(xiàn)“請在此處鍵入”的提示,單擊提示就可以鍵入菜單名稱,如“文件”,再單擊“文件”,即可輸入其下拉子菜單,如下所示:Tips:每創(chuàng)建一個菜單,請在其屬性面板中設置Name屬性,而且不要為中文,因此Name值將是此菜單響應函數(shù)的函數(shù)名的一部分,帶中文的函數(shù)名,總是不好吧。本講中,我們將添加新建(New)、打開(Open)、添加數(shù)據(jù)(AddData)、保存(Save)、另存為(SaveAs)、退出(Exit)這些菜單,()內(nèi)為相應的Name屬性值。Tips:你可以在屬性面板中的Text屬性中,把菜單名設置為中英文形式,如“打開Open”,帶下劃線的

8、O表示此項菜單的快捷鍵是字母O,設置方法是在相應字母前加上“&”字符,如“打開&Open”。但這種快捷鍵只在打開此下拉菜單時才有效,即當你單擊“文件”菜單彈出下拉菜單時,按下字母O就可以定位到“打開”菜單。還有一種在程序運行時都有效的全局快捷鍵,可以在屬性面板中的“ShortCutKeys”中設置。你還可以在屬性面板中的Image屬性中設置你喜歡的菜單圖標。單擊Image那一行右邊的按鈕,彈出如下菜單。選擇“項目資源文件”,再單擊導入就可以選擇你的圖標了最終效果如下所示。注意,在解決方案面板中,選中剛才添加的所有圖標,在其屬性面板中將生成操作設置為“嵌入的資源”,這一點很重要!2、實現(xiàn)相關菜單

9、首先定義指針(寫在public partial classForm1:Form下面即可):Private3 m_mapControl=null;private 2 m_pageLayoutControl=null;private IMapDocumentpMapDocument;若以上指針無效,請?zhí)砑右韵乱茫簎singESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.Display;usingESRI.ArcGIS.Geometry;usingESRI.ArcGIS

10、.SystemUI;在設計視圖中的屬性面板中,選擇Form1,即主窗體,單擊事件按鈕(閃電形狀的那個按鈕),打到“Load”事件并雙擊,添加此事件。在Form1_Load函數(shù)中初始化這些指針:/取得MapControl和PageLayoutControl的引用m_mapControl=(IMapControl3)this.axMapControl1.Object;m_pageLayoutControl=(IPageLayoutControl2)this.axPageLayoutControl1.Object;依次雙擊每個菜單項,添加菜單響應函數(shù)。實現(xiàn)代碼如下:/新建地圖命令/privatevo

11、idNew_Click(objectsender,EventArgse)/本命令涉及到MapControl和PageLayoutControl同步問題,將在下一講中實現(xiàn)/打開地圖文檔Mxd命令/Private void Open_Click(objectsender,EventArgse)/本命令涉及到MapControl和PageLayoutControl同步問題,將在下一講中實現(xiàn)/添加數(shù)據(jù)命令/Private void AddData_Click(objectsender,EventArgse)Int currentLayerCount=this.axMapControl1.LayerCo

12、unt;ICommand pCommand=new ControlsAddDataCommandClass();pCommand.OnCreate(this.axMapControl1.Object);pCommand.OnClick();/保存地圖文檔命令/Private void Save_Click(objectsender,EventArgse)/首先確認當前地圖文檔是否有效if(null!=m_pageLayoutControl.DocumentFilename&m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename

13、)/創(chuàng)建一個新的地圖文檔實例IMapDocumentmapDoc=newMapDocumentClass();/打開當前地圖文檔mapDoc.Open(m_pageLayoutControl.DocumentFilename,string.Empty);/用PageLayout中的文檔替換當前文檔中的PageLayout部分mapDoc.ReplaceContents(IMxdContents)m_pageLayoutControl.PageLayout);/保存地圖文檔mapDoc.Save(mapDoc.UsesRelativePaths,false);mapDoc.Close();/另存

14、為地圖文檔命令/Private void SaveAs_Click(objectsender,EventArgse)/調(diào)用另存為命令ICommand command=newControlsSaveAsDocCommandClass();l);command.OnClick();/退出程序/Private void Exit_Click(objectsender,EventArgse)Application.Exit();3、編譯運行按F5編譯運行程序。也許你會發(fā)現(xiàn),菜單命令的實現(xiàn)方式都是類型的。沒錯,在中,內(nèi)置了許多常用的Command和Tool,如ControlsAddDataCommand

15、Class、ControlsMapZoomInToolClass、ControlsMapPanToolClass等等,這些內(nèi)置對象在命名空間中,你可以對象瀏覽器中查看。而且這些內(nèi)置對象的調(diào)用方式都類似,如下所示:/定義ICommandcommand=new ControlsSaveAsDocCommandClass();/創(chuàng)建command.OnCreate(m_controlsSynchronizer.ActiveControl);/調(diào)用command.OnClick();希望你可以舉一反三,去實現(xiàn)更多的你想要的功能。第三講 MapControl與PageLayoutControl同步在Ar

16、cMap中,能夠很方面地進行MapView和LayoutView兩種視圖的切換,而且二者之間的數(shù)據(jù)是同步顯示的。關于兩種視圖同步的實現(xiàn)方法有多種,可以使用ObjectCopy對象進行數(shù)據(jù)硬拷貝,而比較簡單的方法莫過于二者共享一份地圖了,這也是最常用的方法。1、新建同步類ControlsSynchronizer在解決方案面板中右擊項目名,選擇“添加|類”,在類別中選擇“VisualC#項目項”,在模板中選擇“類”,輸入類名“ControlsSynchronizer.cs”,將以下代碼覆蓋自動生成的代碼:usingSystem;usingSystem.Drawing;usingSystem.Col

17、lections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.IO;untime.InteropServices;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.SystemUI;namespace_sdnMap/ThisclassisusedtosynchronizeagvenPageLayoutControlandaMapControl./Wheninitialize

18、d,theusermustpassthereferenceofthesecontroltotheclass,bind/thecontroltogetherbycallingBindControlswhichinturnsetsajoinedMapreferenced/bybothcontrol;andsetallthebuddycontrolsjoinedbetweenthesetwocontrols./WhenalternatingbetweentheMapControlandPageLayoutControl,youshouldactivatethevisiblecontrol/andde

19、activatetheotherbycallingActivateXXX./Thiscalssislimitedtoasituationwherethecontrolsarenotsimultaneouslyvisible./publicclassControlsSynchronizer#regionclassmembersprivateIMapControl3m_mapControl=null;privateIPageLayoutControl2m_pageLayoutControl=null;privateIToolm_mapActiveTool=null;privateIToolm_pa

20、geLayoutActiveTool=null;privateboolm_IsMapCtrlactive=true;privateArrayListm_frameworkControls=null;#endregion#regionconstructor/默認構造函數(shù)/publicControlsSynchronizer()/初始化ArrayListm_frameworkControls=newArrayList();/構造函數(shù)/publicControlsSynchronizer(IMapControl3mapControl,IPageLayoutControl2pageLayoutCont

21、rol):this()/為類成員賦值m_mapControl=mapControl;m_pageLayoutControl=pageLayoutControl;#endregion#regionproperties/取得或設置MapControl/publicIMapControl3MapControlgetreturnm_mapControl;setm_mapControl=value;/取得或設置PageLayoutControl/publicIPageLayoutControl2PageLayoutControlgetreturnm_pageLayoutControl;setm_page

22、LayoutControl=value;/取得當前ActiveView的類型/publicstringActiveViewTypegetif(m_IsMapCtrlactive)returnMapControl;elsereturnPageLayoutControl;/取得當前活動的Control/publicobjectActiveControlgetif(m_mapControl=null|m_pageLayoutControl=null)thrownewException(ControlsSynchronizer:ActiveControl:rnEitherMapControlorPag

23、eLayoutControlarenotinitialized!);if(m_IsMapCtrlactive)returnm_mapControl.Object;elsereturnm_pageLayoutControl.Object;#endregion#regionMethods/激活MapControl并解除thePagleLayoutControl/publicvoidActivateMap()tryif(m_pageLayoutControl=null|m_mapControl=null)thrownewException(ControlsSynchronizer:ActivateM

24、ap:rnEitherMapControlorPageLayoutControlarenotinitialized!);/緩存當前PageLayout的CurrentToolif(m_pageLayoutControl.CurrentTool!=null)m_pageLayoutActiveTool=m_pageLayoutControl.CurrentTool;/解除PagleLayoutm_pageLayoutControl.ActiveView.Deactivate();/激活MapControlm_mapControl.ActiveView.Activate(m_mapControl.

25、hWnd);/將之前MapControl最后使用的tool,作為活動的tool,賦給MapControl的CurrentToolif(m_mapActiveTool!=null)m_mapControl.CurrentTool=m_mapActiveTool;m_IsMapCtrlactive=true;/為每一個的frameworkcontrols,設置Buddycontrol為MapControlthis.SetBuddies(m_mapControl.Object);catch(Exceptionex)thrownewException(string.Format(ControlsSyn

26、chronizer:ActivateMap:rn0,ex.Message);/激活PagleLayoutControl并減活MapCotrol/publicvoidActivatePageLayout()tryif(m_pageLayoutControl=null|m_mapControl=null)thrownewException(ControlsSynchronizer:ActivatePageLayout:rnEitherMapControlorPageLayoutControlarenotinitialized!);/緩存當前MapControl的CurrentToolif(m_ma

27、pControl.CurrentTool!=null)m_mapActiveTool=m_mapControl.CurrentTool;/解除MapControlm_mapControl.ActiveView.Deactivate();/激活PageLayoutControlm_pageLayoutControl.ActiveView.Activate(m_pageLayoutControl.hWnd);/將之前PageLayoutControl最后使用的tool,作為活動的tool,賦給PageLayoutControl的CurrentToolif(m_pageLayoutActiveToo

28、l!=null)m_pageLayoutControl.CurrentTool=m_pageLayoutActiveTool;m_IsMapCtrlactive=false;/為每一個的frameworkcontrols,設置Buddycontrol為PageLayoutControlthis.SetBuddies(m_pageLayoutControl.Object);catch(Exceptionex)thrownewException(string.Format(ControlsSynchronizer:ActivatePageLayout:rn0,ex.Message);/給予一個地圖

29、,置換PageLayoutControl和MapControl的focusmap/publicvoidReplaceMap(IMapnewMap)if(newMap=null)thrownewException(ControlsSynchronizer:ReplaceMap:rnNewmapforreplacementisnotinitialized!);if(m_pageLayoutControl=null|m_mapControl=null)thrownewException(ControlsSynchronizer:ReplaceMap:rnEitherMapControlorPageL

30、ayoutControlarenotinitialized!);/createanewinstanceofIMapscollectionwhichisneededbythePageLayout/創(chuàng)建一個PageLayout需要用到的,新的IMapscollection的實例IMapsmaps=newMaps();/addthenewmaptotheMapscollection/把新的地圖加到Mapscollection里頭去maps.Add(newMap);boolbIsMapActive=m_IsMapCtrlactive;/callreplacemaponthePageLayoutinor

31、dertoreplacethefocusmap/wemustcallActivatePageLayout,sinceitisthecontrolwecallReplaceMaps/調(diào)用PageLayout的replacemap來置換focusmap/我們必須調(diào)用ActivatePageLayout,因為它是那個我們可以調(diào)用ReplaceMaps的Controlthis.ActivatePageLayout();m_pageLayoutControl.PageLayout.ReplaceMaps(maps);/assignthenewmaptotheMapControl/把新的地圖賦給MapCo

32、ntrolap=newMap;/resettheactivetools/重設activetoolsm_pageLayoutActiveTool=null;m_mapActiveTool=null;/makesurethatthelastactivecontrolisactivated/確認之前活動的control被激活if(bIsMapActive)this.ActivateMap();m_mapControl.ActiveView.Refresh();elsetivatePageLayout();m_pageLayoutControl.ActiveView.Refresh();/bindth

33、eMapControlandPageLayoutControltogetherbyassigninganewjointfocusmap/指定共同的Map來把MapControl和PageLayoutControl綁在一起/trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,則為truepublicvoidBindControls(IMapControl3mapControl,IPageLayoutControl2pageLayoutControl,boolactivateMapFirst)if(mapControl=n

34、ull|pageLayoutControl=null)thrownewException(ControlsSynchronizer:BindControls:rnEitherMapControlorPageLayoutControlarenotinitialized!);m_mapControl=MapControl;m_pageLayoutControl=pageLayoutControl;this.BindControls(activateMapFirst);/bindtheMapControlandPageLayoutControltogetherbyassigninganewjoint

35、focusmap/指定共同的Map來把MapControl和PageLayoutControl綁在一起/trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,則為truepublicvoidBindControls(boolactivateMapFirst)if(m_pageLayoutControl=null|m_mapControl=null)thrownewException(ControlsSynchronizer:BindControls:rnEitherMapControlorPageLayoutContro

36、larenotinitialized!);/createanewinstanceofIMap/創(chuàng)造IMap的一個實例IMapnewMap=newMapClass();newMap.Name=Map;/createanewinstanceofIMapscollectionwhichisneededbythePageLayout/創(chuàng)造一個新的IMapscollection的實例,這是PageLayout所需要的IMapsmaps=newMaps();/addthenewMapinstancetotheMapscollection/把新的Map實例賦給Mapscollectionmaps.Add(n

37、ewMap);/callreplacemaponthePageLayoutinordertoreplacethefocusmap/調(diào)用PageLayout的replacemap來置換focusmapm_pageLayoutControl.PageLayout.ReplaceMaps(maps);/assignthenewmaptotheMapControl/把新的map賦給MapControlm_mapControl.Map=newMap;/resettheactivetools/重設activetoolsm_pageLayoutActiveTool=null;m_mapActiveTool=

38、null;/makesurethatthelastactivecontrolisactivated/確定最后活動的control被激活if(activateMapFirst)this.ActivateMap();elsethis.ActivatePageLayout();/bypassingtheapplicationstoolbarsandTOCtothesynchronizationclass,itsavesyouthe/managementofthebuddycontroleachtimetheact/controltoanarray;oncetheactivecontrolchange

39、s,theclassiteratesthroughthearrayand/callesSetBuddyControloneachofthestoredframeworkcontrol./publicvoidAddFrameworkControl(objectcontrol)if(control=null)thrownewException(ControlsSynchronizer:AddFrameworkControl:rnAddedcontrolisnotinitialized!);m_frameworkControls.Add(control);/Removeaframeworkcontr

40、olfromthemanagedlistofcontrols/publicvoidRemoveFrameworkControl(objectcontrol)if(control=null)thrownewException(ControlsSynchronizer:RemoveFrameworkControl:rnControltoberemovedisnotinitialized!);m_frameworkControls.Remove(control);/Removeaframeworkcontrolfromthemanagedlistofcontrolsbyspecifyingitsin

41、dexinthelist/publicvoidRemoveFrameworkControlAt(intindex)if(m_frameworkControls.Countindex)thrownewException(ControlsSynchronizer:RemoveFrameworkControlAt:rnIndexisoutofrange!);m_frameworkControls.RemoveAt(index);/whentheactivecontrolchanges,theclassiteratesthroughthearrayoftheframeworkcontrols/andc

42、allesSetBuddyControloneachofthecontrols./theactivecontrolprivatevoidSetBuddies(objectbuddy)tryif(buddy=null)thrownewException(ControlsSynchronizer:SetBuddies:rnTargetBuddyControlisnotinitialized!);foreach(objectobjinm_frameworkControls)if(objisIToolbarControl)(IToolbarControl)obj).SetBuddyControl(bu

43、ddy);elseif(objisITOCControl)(ITOCControl)obj).SetBuddyControl(buddy);catch(Exceptionex)thrownewException(string.Format(ControlsSynchronizer:SetBuddies:rn0,ex.Message);#endregion2、新建Maps類在同步類中,要用到Maps類,用于管理地圖對象。與新建同步類ControlsSynchronizer類似,我們新建一Maps類,其所有代碼如下所示:usingSystem;usingSystem.Collections;usi

44、ngSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingESRI.ArcGIS.Carto;namespace_sdnMapGuid(f27d8789-fbbc-4801-be78-0e3cd8fff9d5)ClassInterface(ClassInterfaceType.None)ProgId(_sdnMap.Maps)publicclassMaps:IMaps,IDisposable/classmember-usinginternallyanArrayListtomana

45、getheMapscollectionprivateArrayListm_array=null;#regionclassconstructorpublicMaps()m_array=newArrayList();#endregion#regionIDisposableMembers/Disposethecollection/publicvoidDispose()if(m_array!=null)m_array.Clear();m_array=null;#endregion#regionIMapsMembers/RemovetheMapatthegivenindex/publicvoidRemo

46、veAt(intIndex)if(Indexm_array.Count|Index0)thrownewException(Maps:RemoveAt:rnIndexisoutofrange!);m_array.RemoveAt(Index);/ResettheMapsarray/publicvoidReset()m_array.Clear();/GetthenumberofMapsinthecollection/publicintCountgetreturnm_array.Count;/ReturntheMapatthegivenindex/publicIMapget_Item(intInde

47、x)if(Indexm_array.Count|Index0)thrownewException(Maps:get_Item:rnIndexisoutofrange!);returnm_arrayIndexasIMap;/RemovetheinstanceofthegivenMap/publicvoidRemove(IMapMap)m_array.Remove(Map);/CreateanewMap,addittothecollectionandreturnittothecaller/publicIMapCreate()IMapnewMap=newMapClass();m_array.Add(

48、newMap);returnnewMap;/AddthegivenMaptothecollection/publicvoidAdd(IMapMap)if(Map=null)thrownewException(Maps:Add:rnNewMapismotinitialized!);m_array.Add(Map);#endregion3、新建打開文檔類OpenNewMapDocument由于從工具欄自帶的打開按鈕打開地圖文檔的時候,不會自動進行兩種視圖之間的同步,所以我們要自己派生一個OpenNewMapDocument類,用于打開地圖文檔。右擊項目名,選擇“添加|類”,再選擇ArcGIS類別中

49、的BaseCommand模板,輸入類名為“OpenNewMapDocument.cs”。首先添加引用:usingSystem.Windows.Forms;usingESRI.ArcGIS.Carto;再添加如下成員變量:privateControlsSynchronizerm_controlsSynchronizer=null;修改默認的構造函數(shù)如下所示:/添加參數(shù)publicOpenNewMapDocument(ControlsSynchronizercontrolsSynchronizer)/TODO:Definevaluesforthepublicproperties/設定相關屬性值ba

50、se.m_category=Generic;/localizabletextbase.m_caption=Open;/localizabletextbase.m_message=ThisshouldworkinArcMap/MapControl/PageLayoutControl;/localizabletextbase.m_toolTip=Open;/localizabletextbase.m_name=Generic_Open;/uniqueid,non-localizable(e.g.MyCategory_MyCommand)/初始化m_controlsSynchronizerm_con

51、trolsSynchronizer=controlsSynchronizer;try/TODO:changebitmapnameifnecessary/stringbitmapResourceName=GetType().Name+.bmp;base.m_bitmap=newBitmap(GetType(),bitmapResourceName);catch(Exceptionex)System.Diagnostics.Trace.WriteLine(ex.Message,InvalidBitmap);再在OnClick函數(shù)中添加如下代碼:publicoverridevoidOnClick()

52、OpenFileDialogdlg=newOpenFileDialog();dlg.Filter=MapDocuments(*.mxd)|*.mxd;dlg.Multiselect=false;dlg.Title=OpenMapDocument;if(dlg.ShowDialog()=DialogResult.OK)stringdocName=dlg.FileName;IMapDocumentmapDoc=newMapDocumentClass();if(mapDoc.get_IsPresent(docName)&!mapDoc.get_IsPasswordProtected(docName)

53、mapDoc.Open(docName,string.Empty);IMapmap=mapDoc.get_Map(0);m_controlsSynchronizer.ReplaceMap(map);mapDoc.Close();在添加類時,模板會自動添加一個名為“OpenNewMapDocument.bmp”的圖標,你可以自己修改或者替換為打開的文件夾的圖標4、兩種視圖的同步在中添加成員變量,即同步類對象:privateControlsSynchronizerm_controlsSynchronizer=null;在Form1_Load函數(shù)中進行初始化工作:/初始化controlssynchr

54、onizationcalssm_controlsSynchronizer=newControlsSynchronizer(m_mapControl,m_pageLayoutControl);/把MapControl和PageLayoutControl綁定起來(兩個都指向同一個Map),然后設置MapControl為活動的Controlm_controlsSynchronizer.BindControls(true);/為了在切換MapControl和PageLayoutControl視圖同步,要添加FrameworkControlorkControl(axToolbarControl1.Obj

55、ect);m_controlsSynchronizer.AddFrameworkControl(this.axTOCControl1.Object);/添加打開命令按鈕到工具條OpenNewMapDocumentopenMapDoc=newOpenNewMapDocument(m_controlsSynchronizer);axToolbarControl1.AddItem(openMapDoc,-1,0,false,-1,esriCommandStyles.esriCommandStyleIconOnly);因為我們自動派生了打開文檔類,并自己將其添加到工具條,所以我們就不需要工具條原來的“

56、打開”按鈕了,可以ToolbarControl的屬性中將其刪除。下面,我們可完成上一講遺留的功能了。/新建地圖命令/privatevoidNew_Click(objectsender,EventArgse)/詢問是否保存當前地圖DialogResultres=MessageBox.Show(是否保存當前地圖?,提示,MessageBoxButtons.YesNo,MessageBoxIcon.Question);if(res=DialogResult.Yes)/如果要保存,調(diào)用另存為對話框ICommandcommand=newControlsSaveAsDocCommandClass();if

57、(m_mapControl!=null)command.OnCreate(m_controlsSynchronizer.MapControl.Object);elsecommand.OnCreate(m_controlsSynchronizer.PageLayoutControl.Object);command.OnClick();/創(chuàng)建新的地圖實例IMapmap=newMapClass();map.Name=Map;m_controlsSynchronizer.MapControl.DocumentFilename=string.Empty;/更新新建地圖實例的共享地圖文檔m_control

58、sSynchronizer.ReplaceMap(map);/打開地圖文檔Mxd命令/privatevoidOpen_Click(objectsender,EventArgse)if(this.axMapControl1.LayerCount0)DialogResultresult=MessageBox.Show(是否保存當前地圖?,警告,MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);if(result=DialogResult.Cancel)return;if(result=DialogResult.Yes)this.Save_

59、Click(null,null);OpenNewMapDocumentopenMapDoc=newOpenNewMapDocument(m_controlsSynchronizer);openMapDoc.OnCreate(m_controlsSynchronizer.MapControl.Object);openMapDoc.OnClick();在添加數(shù)據(jù)AddData時,我們也要進行地圖共享,故在AddData_Click函數(shù)后面添加如下代碼:IMappMap=this.axMapControl1.Map;this.m_controlsSynchronizer.ReplaceMap(pMa

60、p);在另存為地圖文檔時,有可能會丟失數(shù)據(jù),因此我們需要提示用戶以確認操作,故需修改SaveAs_Click函數(shù),如下所示:/另存為地圖文檔命令/privatevoidSaveAs_Click(objectsender,EventArgse)/如果當前視圖為MapControl時,提示用戶另存為操作將丟失PageLayoutControl中的設置if(m_controlsSynchronizer.ActiveControlisIMapControl3)if(MessageBox.Show(另存為地圖文檔將丟失制版視圖的設置rn您要繼續(xù)嗎?,提示,MessageBoxButtons.YesNo,

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論