《ArcGISEngine+C實(shí)例開(kāi)發(fā)教程》_第1頁(yè)
《ArcGISEngine+C實(shí)例開(kāi)發(fā)教程》_第2頁(yè)
《ArcGISEngine+C實(shí)例開(kāi)發(fā)教程》_第3頁(yè)
《ArcGISEngine+C實(shí)例開(kāi)發(fā)教程》_第4頁(yè)
已閱讀5頁(yè),還剩93頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第一講:桌面GIS應(yīng)用程序框架的建立講主要是使用MapControkPageLayoutControl>ToolbarControLTOCContro!四個(gè)捽件建立起基本的桌面GIS應(yīng)用程序框架。最終成果預(yù)覽如下:1、新建項(xiàng)目啟動(dòng)VS2005,選擇“文件|新建|項(xiàng)目”,在項(xiàng)目類(lèi)型中選擇VisualC#,再選擇Windows應(yīng)用程序模板,輸入名稱(chēng)“3sdnMap',點(diǎn)擊確定。在解決方案管理器中將“Forml.cs”重命名為“3sdnMap.cs”,在設(shè)計(jì)視圖中,選中窗體,將其屬性中的“Text”改為“3sdnMap”〇2、添加控件選擇工具箱中的“菜單和工具欄|MenuStrip”,將其拖入窗體。選擇工具箱中的“ArcGISWindowsForms”節(jié),將aToolbarControlM控件拖入窗體,并將其屬性中的Dock設(shè)置為T(mén)op。選擇工具箱中的“菜單和工具欄|StatusStrip”,將其拖入到窗體。選擇工具箱中的“容器ドplitContainer”容器拖入窗體,并將其屬性中的Dock設(shè)置為Fill。將TabContro!控件拖入Panell,將Alignment屬性設(shè)置為Bottom,Dock屬性設(shè)置為F川。點(diǎn)擊TabPages屬性右邊的按鈕,彈出TabPage集合編輯器,將tabPage!的Name設(shè)置為tabPageLayer,Text設(shè)置為圖層,將tabPage2的Name設(shè)置為tabPageProperty,Text設(shè)置為屬性。如下所示。選擇“圖層”選項(xiàng)卡,拖入TOCControl控件,設(shè)置Dock屬性為FilL選擇“屬性”選項(xiàng)卡,拖入PropertyGrid控件(“工具箱|所有Windows窗體|PropertyGrid”)'設(shè)置Dock屬性為FilL拖入TabContro!控件到Panel2,設(shè)置Dock屬性為Fill。并上述類(lèi)似的方法,將兩個(gè)選項(xiàng)卡的Name和Text分別設(shè)置為:(tabPageMap、地圖),(tabPageLayout,制版)〇選擇“地圖”選項(xiàng)卡,拖入MapControl控件,設(shè)置Dock屬性為Fill。選擇“制版”選項(xiàng)卡,拖入PageLayoutControl控件,設(shè)置Dock屬性為Fill=最后將Licensecontro!控件拖入到窗體的任意地方。按F5編譯運(yùn)行,可以看到剛才布局好的程序界面了。3、控件綁定通過(guò)以上步驟添加的控件還只是單獨(dú)存在,而我們的程序需要各控件間協(xié)同工作,因此要進(jìn)行控件綁定。分別右擊Toolbarcontrol、TOCControl控件,將Buddy設(shè)置為axMapControll,如下圖所示。

這樣,工具條和圖層控件就與地圖控件關(guān)聯(lián)了。4、添加工具此時(shí),工具條中還沒(méi)有任何工具,添加的方法也很簡(jiǎn)單。右擊Toolbarcontrol,選擇“屬性|Items",點(diǎn)擊Add,選擇Commands選項(xiàng)卡中的Generic,雙擊Open、SaveAs、Redo>Undo即可將相應(yīng)工具添加到工具條。常見(jiàn)的工具有:MapNavigation中的導(dǎo)航工具,MapInquiry中的査詢(xún)工具,FeatureSelection中的選擇工具,你可以根據(jù)需要酌情添加工具。5、編譯運(yùn)行按F5即可編譯運(yùn)行程序,至此桌面GIS應(yīng)用程序框架基本框架已經(jīng)搭建好了,你可以通過(guò)工具條的工具打開(kāi)地圖文檔,瀏覽地圖了,效果如開(kāi)篇所示。下ー講中將大家?guī)?lái)的是菜單的添加及其實(shí)現(xiàn),敬請(qǐng)關(guān)注3SDN.NET。第二講菜單的添加及其實(shí)現(xiàn)在這ー講中,主要講解菜單的添加和實(shí)現(xiàn)。1、添加菜單在設(shè)計(jì)視圖中,單擊菜單欄,會(huì)出現(xiàn)“請(qǐng)?jiān)诖颂庢I入”的提示,單擊提示就可以鍵入菜單名稱(chēng),如“文件”,再單擊“文件”,即可輸入其下拉子菜單,如下所示:Tips:每創(chuàng)建一個(gè)菜單,請(qǐng)?jiān)谄鋵傩悦姘逯性O(shè)置Name屬性,而且不要為中文,因此Name值將是此菜單響應(yīng)函數(shù)的函數(shù)名的一部分,帶中文的函數(shù)名,總是不好吧。本講中,我們將添加新建(New)、打開(kāi)(Open)、添加數(shù)據(jù)(AddData)、保存(Save)、另存為(SaveAs)、退出(Exit)這些菜單,()內(nèi)為相應(yīng)的Name屬性值。Tips:你可以在屬性面板中的Text屬性中,把菜單名設(shè)置為中英文形式,如“打開(kāi)Open”,帶下劃線(xiàn)的〇表示此項(xiàng)菜單的快捷鍵是字母〇,設(shè)置方法是在相應(yīng)字母前加上“&”字符,如“打開(kāi)&Open”。但這種快捷鍵只在打開(kāi)此下拉菜單時(shí)オ有效,即當(dāng)你單擊“文件”菜單彈出下拉菜單時(shí),按下字母〇就可以定位到“打開(kāi)”菜單。還有一種在程序運(yùn)行時(shí)都有效的全局快捷鍵,可以在屬性面板中的“ShortCutKeys”中設(shè)置。你還可以在屬性面板中的Image屬性中設(shè)置你喜歡的菜單圖標(biāo)。單擊!mage那?行右邊的按鈕,彈出如下菜單。選擇“項(xiàng)目資源文件”,再單擊導(dǎo)入就可以選擇你的圖標(biāo)了。最終效果如下所示。

ontrol[文陣]ontrol'J新建New/打開(kāi)Open用添加數(shù)據(jù)AddData?|保存Save屯另存為SaveAs軸退出Exitj 二地圖a制版ArcGIS地圖a制版ArcGISIName:<2、實(shí)現(xiàn)相關(guān)菜單首先定義指針(寫(xiě)在publicpartialclassForml:Form下面即可):privateESRI.ArcGIS.Controls.lMapControl3m_mapControl=null;privateESRI.ArcGIS.Controls.lPageLayoutControl2m_pageLayoutControl=null;privateIMapDocumentpMapDocument;若以上指針無(wú)效,請(qǐng)?zhí)砑右韵乱?usingESRLArcGIS.Carto;usingESRLArcGIS.Controls;usingESRI.ArcGIS.esriSystem;usingESRLArcGIS.Display;usingESRI.ArcGIS.Geometry;usingESRLArcGIS.SystemUI;在設(shè)計(jì)視圖中的屬性面板中,選擇Forml,即主窗體,單擊事件按鈕(閃電形狀的那個(gè)按鈕),打到“Load”事件并雙擊,添加此事件。在Forml_Load函數(shù)中初始化這些指針://取得MapControl和PageLayoutControl的引用m_mapControl=(IMapControl3)this.axMapControll.Object;m_pageLayoutControl=(IPageLayoutControl2)this.axPageLayoutControll.Object;依次雙擊每個(gè)菜單項(xiàng),添加菜單響應(yīng)函數(shù)。實(shí)現(xiàn)代碼如下:III<summary>II!新建地圖命令I(lǐng)II</summary>///<paramname="sender"x/param>///<paramname=He"></param>privatevoidNew_Click(objectsender,EventArgse)(〃本命令涉及到MapControl和PageLayoutControl同步問(wèn)題,將在下ー講中實(shí)現(xiàn))HI<summary>III打開(kāi)地圖文檔Mxd命令I(lǐng)II</summary>///<paramname=Hsender"></param>///<paramname=He"x/param>privatevoidOpen_Click(objectsender,EventArgse)(〃本命令涉及到MapControl和PageLayoutControl同步問(wèn)題,將在下ー講中實(shí)現(xiàn)}Ill添加數(shù)據(jù)命令I(lǐng)II</summary>///<paramname=Hsender"></param>///<paramname=He"></param>privatevoidAddData_Click(objectsender,EventArgse)(intcurrentLayerCount=this.axMapControll.LayerCount;ICommandpCommand=newControlsAddDataCommandClass();pCommand.OnCreate(this.axMapControll.Object);pCommand.OnClick();)///<summary>III保存地圖文檔命令///</summary>///<paramname=Hsender"></param>///<paramname=He"x/param>privatevoidSave_Click(objectsender,EventArgse)(1Z首先確認(rèn)當(dāng)前地圖文檔是否有效if(null!=m_pageLayoutControl.DocumentFilename&&m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename))/Z創(chuàng)建一個(gè)新的地圖文檔實(shí)例IMapDocumentmapDoc=newMapDocumentClass();/Z打開(kāi)當(dāng)前地圖文檔mapDoc.Openfm_pageLayoutControl.DocumentFilename,string.Empty);/Z用PageLayout中的文檔替換當(dāng)前文檔中的PageLayout部分mapDoc.ReplaceContents((IMxdContents)m_pageLayoutControl.PageLayout);/Z保存地圖文檔mapDoc.SavefmapDoc.UsesRelativePaths,false);mapDoc.Close();)}///<summary>II!另存為地圖文檔命令I(lǐng)II</summary>///<paramname="sender"x/param>///<paramname=ne"></param>privatevoidSaveAs_Click(objectsender,EventArgse)(/Z調(diào)用另存為命令I(lǐng)Commandcommand=newControlsSaveAsDocCommandClass();command.OnCreate(m_controlsSynchronizer.ActiveControl);command.OnClick();)//Z退出程序III</summary>III<paramname="sender"x/param>III<paramname="e"x/param>privatevoidExit_Click(objectsender,EventArgse)(Application.Exit();}3、編譯運(yùn)行按F5編譯運(yùn)行程序。也許你會(huì)發(fā)現(xiàn),菜單命令的實(shí)現(xiàn)方式都是類(lèi)型的。沒(méi)錯(cuò),在A(yíng)E9.2中,內(nèi)置了許多常用的Command和Tool,如ControlsAddDataCommandClassヽControlsMapZoomlnToolClassヽControlsMapPanToolClass等等,這些內(nèi)置對(duì)象在ESRLArcGIS.Controls命名空間中,你可以對(duì)象瀏覽器中查看。而且這些內(nèi)置對(duì)象的調(diào)用方式都類(lèi)似,如下所示:/Z定義ICommandcommand=newControlsSaveAsDocCommandClass();/Z創(chuàng)建command.OnCreate(m_controlsSynchronizer.ActiveControl);II調(diào)用command.OnClick();希望你可以舉一反三,去實(shí)現(xiàn)更多的你想要的功能。第三講MapControI與PageLayoutContro!同步在A(yíng)rcMap中,能夠很方面地進(jìn)行MapView和LayoutView兩種視圖的切換,而且二者之間的數(shù)據(jù)是同步顯示的。關(guān)于兩種視圖同步的實(shí)現(xiàn)方法有多種,可以使用Objectcopy對(duì)象進(jìn)行數(shù)據(jù)硬拷貝,而比較簡(jiǎn)單的方法莫過(guò)于二者共享一份地圖了,這也是最常用的方法。1、新建同步類(lèi)ControlsSynchronizer在解決方案面板中右擊項(xiàng)目名,選擇“添加|類(lèi)”,在類(lèi)別中選擇“VisualC#項(xiàng)目項(xiàng)”,在模板中選擇“類(lèi)”,輸入類(lèi)名“ControlsSynchronizer.cs”,將以下代碼覆蓋自動(dòng)生成的代碼:usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.10;usingSystem.Runtime.InteropServices;usingESRLArcGIS.esriSystem;usingESRLArcGIS.Carto;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.SystemUI;namespace_sdnMap{///<summary>///ThisclassisusedtosynchronizeagvenPageLayoutControlandaMapControl.///Wheninitialized,theusermustpassthereferenceofthesecontroltotheclass,bindHIthecontroltogetherbycalling'BindControls'whichinturnsetsajoinedMapreferenced///bybothcontrol;andsetallthebuddycontrolsjoinedbetweenthesetwocontrols.IllWhenalternatingbetweentheMapControlandPageLayoutControl,youshouldactivatethevisiblecontrol///anddeactivatetheotherbycallingActivateXXX.IllThiscalssislimitedtoasituationwherethecontrolsarenotsimultaneouslyvisible.///</summary>publicclassControlsSynchronizer#regionclassmembersprivateIMapControl3m_mapControl=null;privateIPageLayoutControl2m_pageLayoutControl=null;privateIToolm_mapActiveTool=null;privateIToolm_pageLayoutActiveTool=null;privateboolm_lsMapCtrlactive=true;privateArrayListm_frameworkControls=null;#endregionftregionconstructor///<summary>//Z默認(rèn)構(gòu)造函數(shù)///</summary>publicControlsSynchronizer()(〃初始化ArrayListm_frameworkControls=newArrayList();}///<summary>III構(gòu)造函數(shù)III</summary>///<paramname="mapControl"></param>///<paramname="pageLayoutContror,x/param>publicControlsSynchronizer(IMapControl3mapControl,IPageLayoutControl2pageLayoutControl):this()(〃為類(lèi)成員賦值m_mapControl=mapControl;m_pageLayoutControl=pageLayoutControl;}#endregion#regionproperties///<summary>///取得或設(shè)置MapControl///</summary>publicIMapControl3MapControl(get{returnm_mapControl;}set{m_mapControl=value;}}///<summary>///取得或設(shè)置PageLayoutControlIII</summary>publicIPageLayoutControl2PageLayoutControl(get{returnm_pageLayoutControl;}set{m_pageLayoutControl=value;}}///<summary>III取得當(dāng)前ActiveView的類(lèi)型///</summary>publicstringActiveViewTypegetif(m_lsMapCtrlactive)return"MapControl";elsereturn"PageLayoutControl',;})///<summary>//Z取得當(dāng)前活動(dòng)的Control///</summary>publicobjectActiveControl(get(if(m_mapControl==null11m_pageLayoutControl==null)thrownewException("ControlsSynchronizer::ActiveControl:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");if(mJsMapCtrlactive)returnm_mapControl.Object;elsereturnm_pageLayoutControLObject;})#endregion#regionMethods///<summary>///激活MapControl并解除thePagleLayoutControl///</summary>publicvoidActivateMap()(try(if(m_pageLayoutControl==null11m_mapControl==null)thrownewException("ControlsSynchronizer::ActivateMap:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");〃緩存當(dāng)前PageLayout的CurrentToolif(m_pageLayoutControl.CurrentTool!=null)m_pageLayoutActiveTool=m_pageLayoutControl.CurrentTool;〃解除PagleLayoutm_pageLayoutControl.ActiveView.Deactivate();〃激活MapControlm_mapControl.ActiveView.Activate(m_mapControl.hWnd);〃將之前MapControl最后使用的tool,作為活動(dòng)的tool,賦給MapControl的CurrentToolif(m_mapActiveTool!=null)m_mapControl.CurrentTool=m_mapActiveTool;mJsMapCtrlactive=true;〃為每ー個(gè)的frameworkcontrols,iSSBuddycontrol為MapControlthis.SetBuddies(m_mapControl.Object);}catch(Exceptionex)(thrownewException(string.Format(,,ControlsSynchronizer::ActivateMap:\r\n{0},,/ex.Message));})HI<summary>///激活PagleLayoutContro!并減活MapCotrol///</summary>publicvoidActivatePageLayout()(try(if(m_pageLayoutControl==null11m_mapControl==null)thrownewException("ControlsSynchronizer::ActivatePageLayout:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");〃緩存當(dāng)前MapContro!的CurrentToolif(m_mapControl.CurrentTool!=null)m_mapActiveTool=m_mapControl.CurrentTool;〃解除MapControlm_mapControl.ActiveView.Deactivate();〃激活PageLayoutControlm_pageLayoutControl.ActiveView.Activate(m_pageLayoutControl.hWnd);〃將之前PageLayoutControl最后使用的tool,作為活動(dòng)的tool,賦給PageLayoutContro!的CurrentToolif(m_pageLayoutActiveTool!=null)m_pageLayoutControl.CurrentTool=m_pageLayoutActiveTool;m_lsMapCtrlactive=false;〃為每ー個(gè)的frameworkcontrols,設(shè)置Buddycontrol為PageLayoutControlthis.SetBuddies(m_pageLayoutControl.Object);catch(Exceptionex)(thrownewException(string.Format("ControlsSynchronizer::ActivatePageLayout:\r\n{0}",ex.Message));}}///<summary>//Z給予ー個(gè)地圖,置換PageLayoutControl和MapContro!的focusmap///</summary>///<paramname="newMapK></param>publicvoidReplaceMap(IMapnewMap)(if(newMap==null)thrownewException("ControlsSynchronizer::ReplaceMap:\r\nNewmapforreplacementisnotinitialized!");if(m_pageLayoutControl==null11m_mapControl==null)thrownewException("ControlsSynchronizer::ReplaceMap:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");//createanewinstanceofIMapscollectionwhichisneededbythePageLayout〃創(chuàng)建一個(gè)PageLayout需要用到的,新的IMapscollection的實(shí)例IMapsmaps=newMaps();//addthenewmaptotheMapscollection〃把新的地圖加到Mapscollection里頭去maps.Add(newMap);boolblsMapActive=mJsMapCtrlactive;//callreplacemaponthePageLayoutinordertoreplacethefocusmap//wemustcallActivatePageLayout,sinceitisthecontrolwecall'ReplaceMaps'〃調(diào)用PageLayout的replacemap來(lái)置換focusmap〃我們必須調(diào)用ActivatePageLayout,因?yàn)樗悄莻€(gè)我們可以調(diào)用"ReplaceMaps"的Controlthis.ActivatePageLayout();m_pageLayoutControl.PageLayout.ReplaceMaps(maps);//assignthenewmaptotheMapControl〃把新的地圖賦給MapControlm_mapControl.Map=newMap;//resettheactivetools〃重設(shè)activetoolsm_pageLayoutActiveTool=null;m_mapActiveTool=null;//makesurethatthelastactivecontrolisactivated〃確認(rèn)之前活動(dòng)的control被激活if(blsMapActive)(this.ActivateMap();m_mapControl.ActiveView.Refresh();)else(this.ActivatePageLayout();m_pageLayoutControl.ActiveView.Refresh();)}///<summary>///bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocusmap///指定共同的Map來(lái)把MapControl和PageLayoutContro!綁在ー起HI</summary>///<paramname="mapControl"x/param>///<paramname="pageLayoutControl"x/param>///<paramname="activateMapFirsゼ、trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,則為true</param>publicvoidBindControls(IMapControl3mapControl,IPageLayoutControl2pageLayoutControl,boolactivateMapFirst)(if(mapControl==null11pageLayoutControl==null)thrownewException("ControlsSynchronizer::BindControls:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");m_mapControl=MapControl;m_pageLayoutControl=pageLayoutControl;this.BindControls(activateMapFirst);}///<summary>///bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocusmap///指定共同的Map來(lái)把MapControl和PageLayoutContro!綁在ー起HI</summary>III<paramname="activateMapFirst">trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,則為true</param>publicvoidBindControls(boolactivateMapFirst)if(m_pageLayoutControl==null11m_mapControl==null)thrownewException("ControlsSynchronizer::BindControls:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");//createanewinstanceofIMap〃創(chuàng)造IMap的ー個(gè)實(shí)例IMapnewMap=newMapClass();newMap.Name="Map";//createanewinstanceofIMapscollectionwhichisneededbythePageLayout〃創(chuàng)造ー個(gè)新的IMapscollection的實(shí)例,這是PageLayout所需要的IMapsmaps=newMaps();//addthenewMapinstancetotheMapscollection〃把新的Map實(shí)例賦給Mapscollectionmaps.Add(newMap);//callreplacemaponthePageLayoutinordertoreplacethefocusmap〃調(diào)用PageLayout的replacemap來(lái)置換focusmapm_pageLayoutControl.PageLayout.ReplaceMaps(maps);//assignthenewmaptotheMapControl〃把新的map賦給MapControlm_mapControl.Map=newMap;//resettheactivetools〃重設(shè)activetoolsm_pageLayoutActiveTool=null;m_mapActiveTool=null;//makesurethatthelastactivecontrolisactivated〃確定最后活動(dòng)的control被激活if(activateMapFirst)this.ActivateMap();elsethis.ActivatePageLayout();}///<summary>///bypassingtheapplication'stoolbarsandTOCtothesynchronizationclass,itsavesyouthe///managementofthebuddycontroleachtimetheactivecontrolchanges.Thismethodadstheframework///controltoanarray;oncetheactivecontrolchanges,theclassiteratesthroughthearrayand///callesSetBuddyControloneachofthestoredframeworkcontrol.///</summary>///<paramname="control"x/param>publicvoidAddFrameworkControl(objectcontrol)if(control==null)thrownewException("ControlsSynchronizer::AddFrameworkControl:\r\nAddedcontrolisnotinitialized!");m_frameworkControls.Add(control);}///<summary>///Removeaframeworkcontrolfromthemanagedlistofcontrols///</summary>///<paramname="control"x/param>publicvoidRemoveFrameworkControl(objectcontrol)(if(control==null)thrownewException("ControlsSynchronizer::RemoveFrameworkControl:\r\nControltoberemovedisnotinitialized!");m_frameworkControls.Remove(control);}///<summary>///RemoveaframeworkcontrolfromthemanagedlistofcontrolsbyspecifyingitsindexinthelistHI</summary>///<paramname="index"x/param>publicvoidRemoveFrameworkControlAt(intindex)(if(m_frameworkControls.Count<index)thrownewException("ControlsSynchronizer::RemoveFrameworkControlAt:\r\nlndexisoutofrange!");m_frameworkControls.RemoveAt(index);}///<summary>///whentheactivecontrolchanges,theclassiteratesthroughthearrayoftheframeworkcontrols///andcallesSetBuddyControloneachofthecontrols.///</summary>///<paramname="buddy">theactivecontrol</param>privatevoidSetBuddies(objectbuddy)tryif(buddy==null)thrownewException("ControlsSynchronizer::SetBuddies:\r\nTargetBuddyControlisnotinitialized!");foreach(objectobjinm_frameworkControls)(if(objisIToolbarControl)(((IToolbarControl)obj).SetBuddyControl(buddy);)elseif(objisITOCControl)(((ITOCControl)obj).SetBuddyControl(buddy);)})catch(Exceptionex){thrownewException(string.Format("ControlsSynchronizer::SetBuddies:\r\n{0}"zex.Message));})#endregion}}2、新建Maps類(lèi)在同步類(lèi)中,要用到Maps類(lèi),用于管理地圖對(duì)象。與新建同步類(lèi)ControlsSynchronizer類(lèi)似,我們新建一Maps類(lèi),其所有代碼如下所示:usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingESRLArcGIS.Carto;namespace_sdnMap([Guid("f27d8789-fbbc-4801-be78-0e3cd8fff9d5")][Classlnterface(ClasslnterfaceType.None)][Progld("_sdnMap.Maps")]publicclassMaps:IMaps,Disposable//classmember-usinginternallyanArrayListtomanagetheMapscollectionprivateArrayListm_array=null;#regionclassconstructorpublicMaps()(m_array=newArrayList();)#endregion#regionIDisposableMembers///<summary>///Disposethecollection///</summary>publicvoidDispose()(if(m_array!=null){m_array.Clear();m_array=null;}}#endregion#regionIMapsMembers///<summary>///RemovetheMapatthegivenindex///</summary>///<paramname=',lndex,,x/param>publicvoidRemoveAt(intIndex)(if(Index>m_array.Count11Index<0)thrownewException("Maps::RemoveAt:\r\nlndexisoutofrange!");m_array.RemoveAt(lndex);}///<summary>///ResettheMapsarray///</summary>publicvoidReset()m_array.Clear();///<summary>///GetthenumberofMapsinthecollection///</summary>publicintCount(get(returnm_array.Count;}}///<summary>///ReturntheMapatthegivenindexIII</summary>///<paramname="lndexH></param>///<returns></returns>publicIMapget_ltem(intIndex)(if(Index>m_array.Count11Index<0)thrownewException("Maps::get_ltem:\r\nlndexisoutofrange!");returnm_array[lndex]asIMap;}///<summary>///RemovetheinstanceofthegivenMap///</summary>///<paramname="Map"x/param>publicvoidRemove(IMapMap)(m_array.Remove(Map);}III<summary>IIICreateanewMap,addittothecollectionandreturnittothecaller///</summary>///<returnsx/returns>publicIMapCreate()(IMapnewMap=newMapClass();m_array.Add(newMap);returnnewMap;)///<summary>///AddthegivenMaptothecollection///</summary>///<paramname=,,Map"x/param>publicvoidAdd(IMapMap)(if(Map==null)thrownewException("Maps::Add:\r\nNewMapismotinitialized!");m_array.Add(Map);}#endregion)}3、新建打開(kāi)文檔類(lèi)OpenNewMapDocument由于從工具欄自帶的打開(kāi)按鈕打開(kāi)地圖文檔的時(shí)候,不會(huì)自動(dòng)進(jìn)行兩種視圖之間的同步,所以我們要自己派生一個(gè)OpenNewMapDocument類(lèi),用于打開(kāi)地圖文檔。右擊項(xiàng)目名,選擇“添加|類(lèi)”,再選擇ArcGIS類(lèi)別中的BaseCommand模板,輸入類(lèi)名為“OpenNewMapDocument.cs"〇首先添加引用:usingSystem.Windows.Forms;usingESRLArcGIS.Carto;再添加如下成員變量:privateControlsSynchronizerm_controlsSynchronizer=null;修改默認(rèn)的構(gòu)造函數(shù)如下所示:〃添加參數(shù)publicOpenNewMapDocument(ControlsSynchronizerControlsSynchronizer){////TODO:Definevaluesforthepublicproperties//〃設(shè)定相關(guān)屬性值base.m__category="Generic";//localizabletextbase.m_caption="Open";//localizabletextbase.m_message="ThisshouldworkinArcMap/MapControl/PageLayoutControl";//localizabletextbase.m_toolTip="Open";//localizabletextbase.m_name="GenerijOpen";//uniqueid,non-localizable(e.g."MyCategory_MyCommand")〃初始化m_controlsSynchronizerm_controlsSynchronizer=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()(//TODO:AddOpenNewMapDocument.OnClickimplementationOpenFileDialogdig=newOpenFileDialogO;dlg.Filter="MapDocuments(*.mxd)|*.mxd";dlg.Multiselect=false;dlg.Title="OpenMapDocument";if(dlg.ShowDialog()==DialogResult.OK)(stringdocName=dlg.FileName;IMapDocumentmapDoc=newMapDocumentClass();if(mapDoc.getJsPresent(docName)&&!mapDoc.get_lsPasswordProtected(docName))mapDoc.Open(docName,string.Empty);IMapmap=mapDoc.get_Map(0);m__controlsSynchronizer.ReplaceMap(map);mapDoc.Close();}在添加類(lèi)時(shí),模板會(huì)自動(dòng)添加―?個(gè)名為"OpenNewMapDocument.bmp”的圖標(biāo),你可以自己修改或者替換為打開(kāi)的文件夾的圖標(biāo)。4、兩種視圖的同步在3sdnMap.cs中添加成員變量,即同步類(lèi)對(duì)象:privateControlsSynchronizerm_controlsSynchronizer=null;在Forml_Load函數(shù)中進(jìn)行初始化工作:〃初始化controlssynchronizationcalssm_controlsSynchronizer=newControlsSynchronizer(m_mapControl,m_pageLayoutControl);〃把MapControl和PageLayoutContro!綁定起來(lái)(兩個(gè)都指向同一個(gè)Map),然后設(shè)置MapControl為活動(dòng)的Controlm_controlsSynchronizer.BindControls(true);〃為了在切換MapControl和PageLayoutControl視圖同步,要添加FrameworkControlm_controlsSynchronizer.AddFrameworkControl(axToolbarControll.Object);m_controlsSynchronizer.AddFrameworkControl(this.axTOCControll.Object);//添加打開(kāi)命令按鈕到工具條OpenNewMapDocumentopenMapDoc=newOpenNewMapDocument(m_controlsSynchronizer);axToolbarControll.Addltem(openMapDoc,-1,0,false,-1,esriCommandStyles.esriCommandStylelconOnly);因?yàn)槲覀冏詣?dòng)派生了打開(kāi)文檔類(lèi),并自己將其添加到工具條,所以我們就不需要工具條原來(lái)的“打開(kāi)”按鈕了,可以Toolbarcontrol的屬性中將其刪除。下面,我們可完成上一講遺留的功能了。HI<summary>II!新建地圖命令HI</summary>///<paramname="sender"x/param>///<paramname="e"x/param>privatevoidNew__Click(objectsender,EventArgse)(〃詢(xún)問(wèn)是否保存當(dāng)前地圖DialogResultres=MessageBox.Show("是否保存當(dāng)前地圖?ツ"提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);if(res==DialogResult.Yes)(〃如果要保存,調(diào)用另存為對(duì)話(huà)框ICommandcommand=newControlsSaveAsDocCommandClass();if(m_mapControl!=null)command.OnCreate(m_controlsSynchronizer.MapControl.Object);elsecommand.OnCreate(m_controlsSynchronizer.PageLayoutControl.Object);command.OnClick();}〃創(chuàng)建新的地圖實(shí)例IMapmap=newMapClass();map.Name="Map";m_controlsSynchronizer.MapControl.DocumentFilename=string.Empty;〃量新新建地圖實(shí)例的共享地圖文檔m_controlsSynchronizer.ReplaceMap(map);}HI<summary>//Z打開(kāi)地圖文檔Mxd命令HI</summary>///<paramname="sender"x/param>///<paramname="e"x/param>privatevoidOpen_Click(objectsender,EventArgse)(if(this.axMapControll.LayerCount>0)(DialogResultresult=MessageBox.Show("是否保存當(dāng)前地圖?","警告",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);if(result==DialogResult.Cancel)return;if(result==DialogResult.Yes)this.Save_Click(null,null);)OpenNewMapDocumentopenMapDoc=newOpenNewMapDocument(m_controlsSynchronizer);openMapDoc.OnCreate(m_controlsSynchronizer.MapControl.Object);openMapDoc.OnClick(

溫馨提示

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

評(píng)論

0/150

提交評(píng)論