《AE+C#實例開發教程》_第1頁
《AE+C#實例開發教程》_第2頁
《AE+C#實例開發教程》_第3頁
《AE+C#實例開發教程》_第4頁
《AE+C#實例開發教程》_第5頁
已閱讀5頁,還剩24頁未讀, 繼續免費閱讀

下載本文檔

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

文檔簡介

《AE+C#實例開發教程》

第一講本講主要是使用MapControl、PageLayoutControl、ToolbarControl、TOCControl四個控件建立起基本的桌面GIS應用程序框架。最終成果預覽如下:

1、新建項目

啟動VS2005,選擇“文件|新建|項目”,在項目類型中選擇VisualC#,再選擇Windows應用程序模板,輸入名稱“3sdnMap”,點擊確定。

在解決方案管理器中將“Form1.cs”重命名為“3sdnMap.cs”,在設計視圖中,選中窗體,將其屬性中的“Text”改為“3sdnMap”。

2、添加控件

選擇工具箱中的“菜單和工具欄|MenuStrip”,將其拖入窗體。

選擇工具箱中的“ArcGISWindowsForms”節,將“ToolbarControl”控件拖入窗體,并將其屬性中的Dock設置為Top。

選擇工具箱中的“菜單和工具欄|StatusStrip”,將其拖入到窗體。

選擇工具箱中的“容器|SplitContainer”容器拖入窗體,并將其屬性中的Dock設置為Fill。

將TabControl控件拖入Panel1,將Alignment屬性設置為Bottom,Dock屬性設置為Fill。點擊TabPages屬性右邊的按鈕,彈出TabPage集合編輯器,將tabPage1的Name設置為tabPageLayer,Text設置為圖層,將tabPage2的Name設置為tabPageProperty,Text設置為屬性。如下所示。

選擇“圖層”選項卡,拖入TOCControl控件,設置Dock屬性為Fill。

選擇“屬性”選項卡,拖入PropertyGrid控件,設置Dock屬性為Fill。

拖入TabControl控件到Panel2,設置Dock屬性為Fill。并上述類似的方法,將兩個選項卡的Name和Text分別設置為:(tabPageMap、地圖),(tabPageLayout,制版)。

選擇“地圖”選項卡,拖入MapControl控件,設置Dock屬性為Fill。

選擇“制版”選項卡,拖入PageLayoutControl控件,設置Dock屬性為Fill。

最后將LicenseControl控件拖入到窗體的任意地方。

按F5編譯運行,可以看到剛才布局好的程序界面了。

3、控件綁定

通過以上步驟添加的控件還只是單獨存在,而我們的程序需要各控件間協同工作,因此要進行控件綁定。

分別右擊ToolbarControl、TOCControl控件,將Buddy設置為axMapControl1,如下圖所示。

這樣,工具條和圖層控件就與地圖控件關聯了。

4、添加工具

此時,工具條中還沒有任何工具,添加的方法也很簡單。右擊ToolbarControl,選擇“屬性|Items”,點擊Add,選擇Commands選項卡中的Generic,雙擊Open、SaveAs、Redo、Undo即可將相應工具添加到工具條。

常見的工具有:

MapNavigation中的導航工具,MapInquiry中的查詢工具,FeatureSelection中的選擇工具,你可以根據需要酌情添加工具。

5、編譯運行

按F5即可編譯運行程序,至此桌面GIS應用程序框架基本框架已經搭建好了,你可以通過工具條的工具打開地圖文檔,瀏覽地圖了,效果如開篇所示.在上一講中,我們實現了應用程序基本框架,其中有個小錯誤,在此先跟大家說明下。在“屬性”選項卡中,我們當時添加的是DataGridView控件,這個控件是用來顯示數據表的,而專門用于屬性的查詢和設置的控件是PropertyGrid控件。因此請你刪除“屬性”選項卡中的DataGridView控件,再把位于“工具箱|所有Windows窗體|PropertyGrid”(如果沒有,右擊選擇“選擇項”以添加此控件)控件拖到該選項卡。在這一講中,主要講解菜單的添加和實現。1、添加菜單在設計視圖中,單擊菜單欄,會出現“請在此處鍵入”的提示,單擊提示就可以鍵入菜單名稱,如“文件”,再單擊“文件”,即可輸入其下拉子菜單,如下所示:Tips:每創建一個菜單,請在其屬性面板中設置Name屬性,而且不要為中文,因此Name值將是此菜單響應函數的函數名的一部分,帶中文的函數名,總是不好吧。本講中,我們將添加新建(New)、打開(Open)、添加數據(AddData)、保存(Save)、另存為(SaveAs)、退出(Exit)這些菜單,()內為相應的Name屬性值。Tips:你可以在屬性面板中的Text屬性中,把菜單名設置為中英文形式,如“打開Open”,帶下劃線的O表示此項菜單的快捷鍵是字母O,設置方法是在相應字母前加上“&”字符,如“打開&Open”。但這種快捷鍵只在打開此下拉菜單時才有效,即當你單擊“文件”菜單彈出下拉菜單時,按下字母O就可以定位到“打開”菜單。還有一種在程序運行時都有效的全局快捷鍵,可以在屬性面板中的“ShortCutKeys”中設置。你還可以在屬性面板中的Image屬性中設置你喜歡的菜單圖標。單擊Image那一行右邊的按鈕,彈出如下菜單。選擇“項目資源文件”,再單擊導入就可以選擇你的圖標了。最終效果如下所示。注意,在解決方案面板中,選中剛才添加的所有圖標,在其屬性面板中將生成操作設置為“嵌入的資源”,這一點很重要!2、實現相關菜單首先定義指針(寫在publicpartialclassForm1:Form下面即可):

privateESRI.ArcGIS.Controls.IMapControl3m_mapControl=null;privateESRI.ArcGIS.Controls.IPageLayoutControl2m_pageLayoutControl=null;privateIMapDocumentpMapDocument;

若以上指針無效,請添加以下引用:usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.Display;usingESRI.ArcGIS.Geometry;usingESRI.ArcGIS.SystemUI;在設計視圖中的屬性面板中,選擇Form1,即主窗體,單擊事件按鈕(閃電形狀的那個按鈕),打到“Load”事件并雙擊,添加此事件。在Form1_Load函數中初始化這些指針://取得MapControl和PageLayoutControl的引用m_mapControl=(IMapControl3)this.axMapControl1.Object;m_pageLayoutControl=(IPageLayoutControl2)this.axPageLayoutControl1.Object;依次雙擊每個菜單項,添加菜單響應函數。實現代碼如下:///<summary>///新建地圖命令///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidNew_Click(objectsender,EventArgse){//本命令涉及到MapControl和PageLayoutControl同步問題,將在下一講中實現}///<summary>///打開地圖文檔Mxd命令///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidOpen_Click(objectsender,EventArgse){//本命令涉及到MapControl和PageLayoutControl同步問題,將在下一講中實現}///<summary>///添加數據命令///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidAddData_Click(objectsender,EventArgse){intcurrentLayerCount=this.axMapControl1.LayerCount;ICommandpCommand=newControlsAddDataCommandClass();pCommand.OnCreate(this.axMapControl1.Object);pCommand.OnClick();}///<summary>///保存地圖文檔命令///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidSave_Click(objectsender,EventArgse){//首先確認當前地圖文檔是否有效if(null!=m_pageLayoutControl.DocumentFilename&&m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename)){//創建一個新的地圖文檔實例IMapDocumentmapDoc=newMapDocumentClass();//打開當前地圖文檔mapDoc.Open(m_pageLayoutControl.DocumentFilename,string.Empty);//用PageLayout中的文檔替換當前文檔中的PageLayout部分mapDoc.ReplaceContents((IMxdContents)m_pageLayoutControl.PageLayout);//保存地圖文檔mapDoc.Save(mapDoc.UsesRelativePaths,false);mapDoc.Close();}}///<summary>///另存為地圖文檔命令///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidSaveAs_Click(objectsender,EventArgse){//調用另存為命令ICommandcommand=newControlsSaveAsDocCommandClass();command.OnCreate(m_controlsSynchronizer.ActiveControl);command.OnClick();}///<summary>///退出程序///</summary>///<paramname="sender"></param>///<paramname="e"></param>privatevoidExit_Click(objectsender,EventArgse){Application.Exit();}3、編譯運行按F5編譯運行程序。也許你會發現,菜單命令的實現方式都是類型的。沒錯,在AE9.2中,內置了許多常用的Command和Tool,如ControlsAddDataCommandClass、ControlsMapZoomInToolClass、ControlsMapPanToolClass等等,這些內置對象在ESRI.ArcGIS.Controls命名空間中,你可以對象瀏覽器中查看。而且這些內置對象的調用方式都類似,如下所示:

//定義

ICommandcommand=newControlsSaveAsDocCommandClass();

//創建

command.OnCreate(m_controlsSynchronizer.ActiveControl);

//調用

command.OnClick();希望你可以舉一反三,去實現更多的你想要的功能。第三講MapControl與PageLayoutControl同步(1)在ArcMap中,能夠很方面地進行MapView和LayoutView兩種視圖的切換,而且二者之間的數據是同步顯示的。

關于兩種視圖同步的實現方法有多種,可以使用ObjectCopy對象進行數據硬拷貝,而比較簡單的方法莫過于二者共享一份地圖了,這也是最常用的方法。

1、新建同步類ControlsSynchronizer

在解決方案面板中右擊項目名,選擇“添加|類”,在類別中選擇“VisualC#項目項”,在模板中選擇“類”,輸入類名“ControlsSynchronizer.cs”,將以下代碼覆蓋自動生成的代碼:usingSystem;

usingSystem.Drawing;

usingSystem.Collections;

usingSystem.ComponentModel;

usingSystem.Windows.Forms;

usingSystem.IO;

usingSystem.Runtime.InteropServices;

usingESRI.ArcGIS.esriSystem;

usingESRI.ArcGIS.Carto;

usingESRI.ArcGIS.Controls;

usingESRI.ArcGIS.SystemUI;namespace_sdnMap

{

///<summary>

///ThisclassisusedtosynchronizeagvenPageLayoutControlandaMapControl.

///Wheninitialized,theusermustpassthereferenceofthesecontroltotheclass,bind

///thecontroltogetherbycalling'BindControls'whichinturnsetsajoinedMapreferenced

///bybothcontrol;andsetallthebuddycontrolsjoinedbetweenthesetwocontrols.

///WhenalternatingbetweentheMapControlandPageLayoutControl,youshouldactivatethevisiblecontrol

///anddeactivatetheotherbycallingActivateXXX.

///Thiscalssislimitedtoasituationwherethecontrolsarenotsimultaneouslyvisible.

///</summary>

publicclassControlsSynchronizer

{

#regionclassmembers

privateIMapControl3m_mapControl=null;

privateIPageLayoutControl2m_pageLayoutControl=null;

privateIToolm_mapActiveTool=null;

privateIToolm_pageLayoutActiveTool=null;

privateboolm_IsMapCtrlactive=true;

privateArrayListm_frameworkControls=null;

#endregion

#regionconstructor

///<summary>

///默認構造函數

///</summary>

publicControlsSynchronizer()

{

//初始化ArrayList

m_frameworkControls=newArrayList();

}

///<summary>

///構造函數

///</summary>

///<paramname="mapControl"></param>

///<paramname="pageLayoutControl"></param>

publicControlsSynchronizer(IMapControl3mapControl,IPageLayoutControl2pageLayoutControl)

:this()

{

//為類成員賦值

m_mapControl=mapControl;

m_pageLayoutControl=pageLayoutControl;

}

#endregion

#regionproperties

///<summary>

///取得或設置MapControl

///</summary>

publicIMapControl3MapControl

{

get{returnm_mapControl;}

set{m_mapControl=value;}

}

///<summary>

///取得或設置PageLayoutControl

///</summary>

publicIPageLayoutControl2PageLayoutControl

{

get{returnm_pageLayoutControl;}

set{m_pageLayoutControl=value;}

}

///<summary>

///取得當前ActiveView的類型

///</summary>

publicstringActiveViewType

{

get

{

if(m_IsMapCtrlactive)

return"MapControl";

else

return"PageLayoutControl";

}

}

///<summary>

///取得當前活動的Control

///</summary>

publicobjectActiveControl

{

get

{

if(m_mapControl==null||m_pageLayoutControl==null)

thrownewException("ControlsSynchronizer::ActiveControl:\r\nEitherMapControl

or

PageLayoutControlarenotinitialized!");

if(m_IsMapCtrlactive)

returnm_mapControl.Object;

else

returnm_pageLayoutControl.Object;

}

}

#endregion

#regionMethods

///<summary>

///激活MapControl并解除thePagleLayoutControl

///</summary>

publicvoidActivateMap()

{

try

{

if(m_pageLayoutControl==null||m_mapControl==null)

thrownewException("ControlsSynchronizer::ActivateMap:\r\nEitherMapControl

or

PageLayoutControlarenotinitialized!");

//緩存當前PageLayout的CurrentTool

if(m_pageLayoutControl.CurrentTool!=null)m_pageLayoutActiveTool=m_pageLayoutControl.CurrentTool;

//解除PagleLayout

m_pageLayoutControl.ActiveView.Deactivate();

//激活MapControl

m_mapControl.ActiveView.Activate(m_mapControl.hWnd);

//將之前MapControl最后使用的tool,作為活動的tool,賦給MapControl的CurrentTool

if(m_mapActiveTool!=null)m_mapControl.CurrentTool=m_mapActiveTool;

m_IsMapCtrlactive=true;

//為每一個的frameworkcontrols,設置Buddycontrol為MapControl

this.SetBuddies(m_mapControl.Object);

}

catch(Exceptionex)

{

thrownewException(string.Format("ControlsSynchronizer::ActivateMap:\r\n{0}",ex.Message));

}

}

///<summary>

///激活PagleLayoutControl并減活MapCotrol

///</summary>

publicvoidActivatePageLayout()

{

try

{

if(m_pageLayoutControl==null||m_mapControl==null)

thrownewException("ControlsSynchronizer::ActivatePageLayout:\r\nEitherMapControl

or

PageLayoutControlarenotinitialized!");

//緩存當前MapControl的CurrentTool

if(m_mapControl.CurrentTool!=null)m_mapActiveTool=m_mapControl.CurrentTool;

//解除MapControl

m_mapControl.ActiveView.Deactivate();

//激活PageLayoutControl

m_pageLayoutControl.ActiveView.Activate(m_pageLayoutControl.hWnd);

//將之前PageLayoutControl最后使用的tool,作為活動的tool,賦給PageLayoutControl的CurrentTool

if(m_pageLayoutActiveTool!=null)m_pageLayoutControl.CurrentTool=m_pageLayoutActiveTool;

m_IsMapCtrlactive=false;

//為每一個的frameworkcontrols,設置Buddycontrol為PageLayoutControl

this.SetBuddies(m_pageLayoutControl.Object);

}

catch(Exceptionex)

{

thrownewException(string.Format("ControlsSynchronizer::ActivatePageLayout:\r\n{0}",ex.Message));

}

}

///<summary>

///給予一個地圖,置換PageLayoutControl和MapControl的focusmap

///</summary>

///<paramname="newMap"></param>

publicvoidReplaceMap(IMapnewMap)

{

if(newMap==null)

thrownewException("ControlsSynchronizer::ReplaceMap:\r\nNewmapforreplacementisnotinitialized!");

if(m_pageLayoutControl==null||m_mapControl==null)

thrownewException("ControlsSynchronizer::ReplaceMap:\r\nEitherMapControl

or

PageLayoutControlarenotinitialized!");

//createanewinstanceofIMapscollectionwhichisneededbythePageLayout

//創建一個PageLayout需要用到的,新的IMapscollection的實例

IMapsmaps=newMaps();

//addthenewmaptotheMapscollection

//把新的地圖加到Mapscollection里頭去

maps.Add(newMap);

boolbIsMapActive=m_IsMapCtrlactive;

//callreplacemaponthePageLayoutinordertoreplacethefocusmap

//wemustcallActivatePageLayout,sinceitisthecontrolwecall'ReplaceMaps'

//調用PageLayout的replacemap來置換focusmap

//我們必須調用ActivatePageLayout,因為它是那個我們可以調用"ReplaceMaps"的Control

this.ActivatePageLayout();

m_pageLayoutControl.PageLayout.ReplaceMaps(maps);

//assignthenewmaptotheMapControl

//把新的地圖賦給MapControl

m_mapControl.Map=newMap;

//resettheactivetools

//重設activetools

m_pageLayoutActiveTool=null;

m_mapActiveTool=null;

//makesurethatthelastactivecontrolisactivated

//確認之前活動的control被激活

if(bIsMapActive)

{

this.ActivateMap();

m_mapControl.ActiveView.Refresh();

}

else

{

this.ActivatePageLayout();

m_pageLayoutControl.ActiveView.Refresh();

}

}

///<summary>

///bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocusmap

///指定共同的Map來把MapControl和PageLayoutControl綁在一起

///</summary>

///<paramname="mapControl"></param>

///<paramname="pageLayoutControl"></param>

///<paramname="activateMapFirst">trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,則為true</param>

publicvoidBindControls(IMapControl3mapControl,IPageLayoutControl2pageLayoutControl,boolactivateMapFirst)

{

if(mapControl==null||pageLayoutControl==null)

thrownewException("ControlsSynchronizer::BindControls:\r\nEitherMapControl

or

PageLayoutControlarenotinitialized!");

m_mapControl=MapControl;

m_pageLayoutControl=pageLayoutControl;

this.BindControls(activateMapFirst);

}

///<summary>

///bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocusmap

///指定共同的Map來把MapControl和PageLayoutControl綁在一起

///</summary>

///<paramname="activateMapFirst">trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,則為true</param>

publicvoidBindControls(boolactivateMapFirst)

{

if(m_pageLayoutControl==null||m_mapControl==null)

thrownewException("ControlsSynchronizer::BindControls:\r\nEitherMapControl

or

PageLayoutControlarenotinitialized!");

//createanewinstanceofIMap

//創造IMap的一個實例

IMapnewMap=newMapClass();

newMap.Name="Map";

//createanewinstanceofIMapscollectionwhichisneededbythePageLayout

//創造一個新的IMapscollection的實例,這是PageLayout所需要的

IMapsmaps=newMaps();

//addthenewMapinstancetotheMapscollection

//把新的Map實例賦給Mapscollection

maps.Add(newMap);

//callreplacemaponthePageLayoutinordertoreplacethefocusmap

//調用PageLayout的replacemap來置換focusmap

m_pageLayoutControl.PageLayout.ReplaceMaps(maps);

//assignthenewmaptotheMapControl

//把新的map賦給MapControl

m_mapControl.Map=newMap;

//resettheactivetools

//重設activetools

m_pageLayoutActiveTool=null;

m_mapActiveTool=null;

//makesurethatthelastactivecontrolisactivated

//確定最后活動的control被激活

if(activateMapFirst)

this.ActivateMap();

else

this.ActivatePageLayout();

}

///<summary>

///bypassingtheapplication'stoolbarsandTOCtothesynchronizationclass,itsavesyouthe

///managementofthebuddycontroleachtimetheactivecontrolchanges.Thismethodadstheframework

///controltoanarray;oncetheactivecontrolchanges,theclassiteratesthroughthearrayand

///callesSetBuddyControloneachofthestoredframeworkcontrol.

///</summary>

///<paramname="control"></param>

publicvoidAddFrameworkControl(objectcontrol)

{

if(control==null)

thrownewException("ControlsSynchronizer::AddFrameworkControl:\r\nAddedcontrolisnotinitialized!");

m_frameworkControls.Add(control);

}

///<summary>

///Removeaframeworkcontrolfromthemanagedlistofcontrols

///</summary>

///<paramname="control"></param>

publicvoidRemoveFrameworkControl(objectcontrol)

{

if(control==null)

thrownewException("ControlsSynchronizer::RemoveFrameworkControl:\r\nControltoberemovedisnotinitialized!");

m_frameworkControls.Remove(control);

}

///<summary>

///Removeaframeworkcontrolfromthemanagedlistofcontrolsbyspecifyingitsindexinthelist

///</summary>

///<paramname="index"></param>

publicvoidRemoveFrameworkControlAt(intindex)

{

if(m_frameworkControls.Count<index)

thrownewException("ControlsSynchronizer::RemoveFrameworkControlAt:\r\nIndexisoutofrange!");

m_frameworkControls.RemoveAt(index);

}

///<summary>

///whentheactivecontrolchanges,theclassiteratesthroughthearrayoftheframeworkcontrols

///

andcallesSetBuddyControloneachofthecontrols.

///</summary>

///<paramname="buddy">theactivecontrol</param>

privatevoidSetBuddies(objectbuddy)

{

try

{

if(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}",ex.Message));

}

}

#endregion

}

}

ArcGISEngine+C#實例開發教程》第三講MapControl與PageLayoutControl同步(2)2、新建Maps類在同步類中,要用到Maps類,用于管理地圖對象。與新建同步類ControlsSynchronizer類似,我們新建一Maps類,其所有代碼如下所示:usingSystem;

usingSystem.Collections;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Runtime.InteropServices;usingESRI.ArcGIS.Carto;namespace_sdnMap

{

[Guid("f27d8789-fbbc-4801-be78-0e3cd8fff9d5")]

[ClassInterface(ClassInterfaceType.None)]

[ProgId("_sdnMap.Maps")]

publicclassMaps:IMaps,IDisposable

{

//classmember-usinginternallyanArrayListtomanagetheMapscollection

privateArrayListm_array=null;

#regionclassconstructor

publicMaps()

{

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="Index"></param>

publicvoidRemoveAt(intIndex)

{

if(Index>m_array.Count||Index<0)

thrownewException("Maps::RemoveAt:\r\nIndexisoutofrange!");

m_array.RemoveAt(Index);

}

///<summary>

///ResettheMapsarray

///</summary>

publicvoidReset()

{

m_array.Clear();

}

///<summary>

///GetthenumberofMapsinthecollection

///</summary>

publicintCount

{

get

{

returnm_array.Count;

}

}

///<summary>

///ReturntheMapatthegivenindex

///</summary>

///<paramname="Index"></param>

///<returns></returns>

publicIMapget_Item(intIndex)

{

if(Index>m_array.Count||Index<0)

thrownewException("Maps::get_Item:\r\nIndexisoutofrange!");

returnm_array[Index]asIMap;

}

///<summary>

///RemovetheinstanceofthegivenMap

///</summary>

///<paramname="Map"></param>

publicvoidRemove(IMapMap)

{

m_array.Remove(Map);

}

///<summary>

///CreateanewMap,addittothecollectionandreturnittothecaller

///</summary>

///<returns></returns>

publicIMapCreate()

{

IMapnewMap=newMapClass();

m_array.Add(newMap);

returnnewMap;

}

///<summary>

///AddthegivenMaptothecollection

///</summary>

///<paramname="Map"></param>

publicvoidAdd(IMapMap)

{

if(Map==null)

thrownewException("Maps::Add:\r\nNewMapismotinitialized!");

m_array.Add(Map);

}

#endregion

}

}

3、新建打開文檔類OpenNewMapDocument

由于從工具欄自帶的打開按鈕打開地圖文檔的時候,不會自動進行兩種視圖之間的同步,所以我們要自己派生一個OpenNewMapDocument類,用于打開地圖文檔。

右擊項目名,選擇“添加|類”,再選擇ArcGIS類別中的BaseCommand模板,輸入類名為“OpenNewMapDocument.cs”。

首先添加引用:usingSystem.Windows.Forms;

usingESRI.ArcGIS.Carto;

再添加如下成員變量:

privateControlsSynchronizerm_controlsSynchronizer=null;

修改默認的構造函數如下所示:

//添加參數

publicOpenNewMapDocument(ControlsSynchronizercontrolsSynchronizer)

{

//

//TODO:Definevaluesforthepublicproperties

//

//設定相關屬性值

base.m_category="Generic";//localizabletext

base.m_caption="Open";

//localizabletext

base.m_message="ThisshouldworkinArcMap/MapControl/PageLayoutControl";

//localizabletext

base.m_toolTip="Open";

//localizabletext

base.m_name="Generic_Open";

//uniqueid,non-localizable(e.g."MyCategory_MyCommand")

//初始化m_controlsSynchronizer

m_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函數中添加如下代碼:

publicoverridevoidOnClick()

{

//TODO:AddOpenNewMapDocument.OnClickimplementation

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))

{

mapDoc.Open(docName,string.Empty);

IMapmap=mapDoc.get_Map(0);

m_controlsSynchronizer.ReplaceMap(map);

mapDoc.Close();

}

}

}

在添加類時,模板會自動添加一個名為“OpenNewMapDocument.bmp”的圖標,你可以自己修改或者替換為打開的文件夾的圖標。ArcGISEngine+C#實例開發教程》第三講MapControl與PageLayoutControl同步(3)4、兩種視圖的同步

在3sdnMap.cs中添加成員變量,即同步類對象:

privateControlsSynchronizerm_controlsSynchronizer=null;

在Form1_Load函數中進行初始化工作:

//初始化controlssynchronizationcalss

m_controlsSynchronizer=new

ControlsSynchronizer(m_mapControl,m_pageLayoutControl);

//把MapControl和PageLayoutControl綁定起來(兩個都指向同一個Map),然后設置MapControl為活動的Control

m_controlsSynchronizer.BindControls(true);

//為了在切換MapControl和PageLayoutControl視圖同步,要添加FrameworkControl

m_controlsSynchronizer.AddFrameworkControl(axToolbarControl1.Object);

m_controlsSynchronizer.AddFrameworkControl(this.axTOCControl1.Object);

//添加打開命令按鈕到工具條

OpenNewMapDocumentopenMapDoc=newOpenNewMapDocument(m_controlsSynchronizer);

axToolbarControl1.AddItem(openMapDoc,-1,0,false,-1,esriCommandStyles.esriCommandStyleIconOnly);

因為我們自動派生了打開文檔類,并自己將其添加到工具條,所以我們就不需要工具條原來的“打開”按鈕了,可以ToolbarControl的屬性中將其刪除。

下面,我們可完成上一講遺留的功能了。

///<summary>

///新建地圖命令

///</summary>

///<paramname="sender"></param>

///<paramname="e"></param>

privatevoidNew_Click(objectsender,EventArgse)

{

//詢問是否保存當前地圖

DialogResultres=MessageBox.Show("是否保存當前地圖?","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question);

if(res==DialogResult.Yes)

{

//如果要保存,調用另存為對話框

ICommandcommand=newControlsSaveAsDocCommandClass();

if(m_mapControl!=nul

溫馨提示

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

評論

0/150

提交評論