




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、?ArcGIS Engine+C#實(shí)例開發(fā)教程?u 讀者對象:使用C#開發(fā)ArcGIS Engine以下簡稱AE的初學(xué)者。u 預(yù)備知識:了解AE根本體系,了解C#根本語法,了解VS2005的根本使用方法。u 預(yù)期學(xué)習(xí)效果:進(jìn)一步理解AE的體系結(jié)構(gòu)與開發(fā)方法,掌握根本的GIS桌面應(yīng)用程序的開發(fā)。第一講 桌面GIS應(yīng)用程序框架的建立本講主要是使用MapControl、PageLayoutControl、ToolbarControl、TOCControl四個(gè)控件建立起根本的桌面GIS應(yīng)用程序框架。最終成果預(yù)覽如下:1、 新建工程啟動(dòng)VS2005,選擇“文件|新建|工程,在工程類型中選擇Visual
2、C#,再選擇Windows應(yīng)用程序模板,輸入名稱“3sdnMap,點(diǎn)擊確定。在解決方案管理器中將“Form1.cs重命名為“3sdnMap.cs,在設(shè)計(jì)視圖中,選中窗體,將其屬性中的“Text改為“3sdnMap。2、 添加控件選擇工具箱中的“菜單和工具欄|MenuStrip,將其拖入窗體。選擇工具箱中的“ArcGIS Windows Forms節(jié),將“ToolbarControl控件拖入窗體,并將其屬性中的Dock設(shè)置為Top。選擇工具箱中的“菜單和工具欄|StatusStrip,將其拖入到窗體。選擇工具箱中的“容器|SplitContainer容器拖入窗體,并將其屬性中的Dock設(shè)置為Fi
3、ll。將TabControl控件拖入Panel1,將Alignment屬性設(shè)置為Bottom,Dock屬性設(shè)置為Fill。點(diǎn)擊TabPages屬性右邊的按鈕,彈出TabPage集合編輯器,將tabPage1的Name設(shè)置為tabPageLayer,Text設(shè)置為圖層,將tabPage2的Name設(shè)置為tabPageProperty,Text設(shè)置為屬性。如下所示。選擇“圖層選項(xiàng)卡,拖入TOCControl控件,設(shè)置Dock屬性為Fill。選擇“屬性選項(xiàng)卡,拖入DataGridView控件,設(shè)置Dock屬性為Fill。拖入TabControl控件到Panel2,設(shè)置Dock屬性為Fill。并上述類
4、似的方法,將兩個(gè)選項(xiàng)卡的Name和Text分別設(shè)置為:tabPageMap、地圖,tabPageLayout,制版。選擇“地圖選項(xiàng)卡,拖入MapControl控件,設(shè)置Dock屬性為Fill。選擇“制版選項(xiàng)卡,拖入PageLayoutControl控件,設(shè)置Dock屬性為Fill。最后將LicenseControl控件拖入到窗體的任意地方。按F5編譯運(yùn)行,可以看到剛剛布局好的程序界面了。3、 控件綁定通過以上步驟添加的控件還只是單獨(dú)存在,而我們的程序需要各控件間協(xié)同工作,因此要進(jìn)行控件綁定。分別右擊ToolbarControl、TOCControl控件,將Buddy設(shè)置為axMapContro
5、l1,如以下圖所示。這樣,工具條和圖層控件就與地圖控件關(guān)聯(lián)了。4、 添加工具此時(shí),工具條中還沒有任何工具,添加的方法也很簡單。右擊ToolbarControl,選擇“屬性|Items,點(diǎn)擊Add,選擇Commands選項(xiàng)卡中的Generic,雙擊Open、SaveAs、Redo、Undo即可將相應(yīng)工具添加到工具條。常見的工具有:Map Navigation中的導(dǎo)航工具,Map Inquiry中的查詢工具,F(xiàn)eature Selection中的選擇工具,你可以根據(jù)需要酌情添加工具。5、 編譯運(yùn)行按F5即可編譯運(yùn)行程序,至此桌面GIS應(yīng)用程序框架根本框架已經(jīng)搭建好了,可以通過工具條的工具翻開地圖文
6、檔,瀏覽地圖了,效果如開篇所示。第二講 菜單的添加及其實(shí)現(xiàn)在上一講中,我們實(shí)現(xiàn)了應(yīng)用程序根本框架,其中有個(gè)小錯(cuò)誤,在此先跟大家說明下。在“屬性選項(xiàng)卡中,我們當(dāng)時(shí)添加的是DataGridView控件,這個(gè)控件是用來顯示數(shù)據(jù)表的,而專門用于屬性的查詢和設(shè)置的控件是PropertyGrid控件。因此請你刪除“屬性選項(xiàng)卡中的DataGridView控件,再把位于“工具箱|所有Windows窗體|PropertyGrid如果沒有,右擊選擇“選擇項(xiàng)以添加此控件控件拖到該選項(xiàng)卡。在這一講中,主要講解菜單的添加和實(shí)現(xiàn)。1、 添加菜單在設(shè)計(jì)視圖中,單擊菜單欄,會出現(xiàn)“請?jiān)诖颂庢I入的提示,單擊提示就可以鍵入菜單名
7、稱,如“文件,再單擊“文件,即可輸入其下拉子菜單,如下所示:Tips:每創(chuàng)立一個(gè)菜單,請?jiān)谄鋵傩悦姘逯性O(shè)置Name屬性,而且不要為中文,因此Name值將是此菜單響應(yīng)函數(shù)的函數(shù)名的一局部,帶中文的函數(shù)名,總是不好吧。本講中,我們將添加新建New、翻開Open、添加數(shù)據(jù)AddData、保存Save、另存為SaveAs、退出Exit這些菜單,內(nèi)為相應(yīng)的Name屬性值。Tips:你可以在屬性面板中的Text屬性中,把菜單名設(shè)置為中英文形式,如“翻開 Open,帶下劃線的O表示此項(xiàng)菜單的快捷鍵是字母O,設(shè)置方法是在相應(yīng)字母前加上“&字符,如“翻開 &Open。但這種快捷鍵只在翻開此下拉菜
8、單時(shí)才有效,即當(dāng)你單擊“文件菜單彈出下拉菜單時(shí),按下字母O就可以定位到“翻開菜單。還有一種在程序運(yùn)行時(shí)都有效的全局快捷鍵,可以在屬性面板中的“ShortCutKeys中設(shè)置。你還可以在屬性面板中的Image屬性中設(shè)置你喜歡的菜單圖標(biāo)。單擊Image那一行右邊的按鈕,彈出如下菜單。選擇“工程資源文件,再單擊導(dǎo)入就可以選擇你的圖標(biāo)了。最終效果如下所示。注意,在解決方案面板中,選中剛剛添加的所有圖標(biāo),在其屬性面板中將生成操作設(shè)置為“嵌入的資源,這一點(diǎn)很重要!2、 實(shí)現(xiàn)相關(guān)菜單首先定義指針寫在public partial class Form1 : Form下面即可:private ESRI.ArcG
9、IS.Controls.IMapControl3 m_mapControl = null;private ESRI.ArcGIS.Controls.IPageLayoutControl2 m_pageLayoutControl = null;private IMapDocument pMapDocument;假設(shè)以上指針無效,請?zhí)砑右韵乱茫簎sing ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.Geom
10、etry;using ESRI.ArcGIS.SystemUI;在設(shè)計(jì)視圖中的屬性面板中,選擇Form1,即主窗體,單擊事件按鈕閃電形狀的那個(gè)按鈕,打到“Load事件并雙擊,添加此事件。在Form1_Load函數(shù)中初始化這些指針:/取得MapControl和PageLayoutControl的引用m_mapControl = (IMapControl3)this.axMapControl1.Object;m_pageLayoutControl = (IPageLayoutControl2)this.axPageLayoutControl1.Object;依次雙擊每個(gè)菜單項(xiàng),添加菜單響應(yīng)函數(shù)。實(shí)
11、現(xiàn)代碼如下: / <summary> / 新建地圖命令 / </summary> / <param name="sender"></param> / <param name="e"></param> private void New_Click(object sender, EventArgs e) /本命令涉及到MapControl和PageLayoutControl同步問題,將在下一講中實(shí)現(xiàn) / <summary> / 翻開地圖文檔Mxd命令 / </summa
12、ry> private void Open_Click(object sender, EventArgs e) /本命令涉及到MapControl和PageLayoutControl同步問題,將在下一講中實(shí)現(xiàn) / <summary> / 添加數(shù)據(jù)命令 / </summary> private void AddData_Click(object sender, EventArgs e) int currentLayerCount = this.axMapControl1.LayerCount; ICommand pCommand = new ControlsAddD
13、ataCommandClass(); pCommand.OnCreate(this.axMapControl1.Object); pCommand.OnClick(); / <summary> / 保存地圖文檔命令 / </summary> private void Save_Click(object sender, EventArgs e) /首先確認(rèn)當(dāng)前地圖文檔是否有效 if (null != m_pageLayoutControl.DocumentFilename && m_mapControl.CheckMxFile(m_pageLayoutCo
14、ntrol.DocumentFilename) /創(chuàng)立一個(gè)新的地圖文檔實(shí)例 IMapDocument mapDoc = new MapDocumentClass(); /翻開當(dāng)前地圖文檔 mapDoc.Open(m_pageLayoutControl.DocumentFilename, string.Empty); /用PageLayout中的文檔替換當(dāng)前文檔中的PageLayout局部 mapDoc.ReplaceContents(IMxdContents)m_pageLayoutControl.PageLayout); /保存地圖文檔 mapDoc.Save(mapDoc.UsesRela
15、tivePaths, false); mapDoc.Close(); / <summary> / 另存為地圖文檔命令 / </summary> private void SaveAs_Click(object sender, EventArgs e) /調(diào)用另存為命令 ICommand command = new ControlsSaveAsDocCommandClass(); command.OnCreate(m_controlsSynchronizer.ActiveControl); command.OnClick(); / <summary> / 退出
16、程序 / </summary> private void Exit_Click(object sender, EventArgs e) Application.Exit(); 3、 編譯運(yùn)行按F5編譯運(yùn)行程序。也許你會發(fā)現(xiàn),菜單命令的實(shí)現(xiàn)方式都是類型的。沒錯(cuò),在AE9.2中,內(nèi)置了許多常用的Command和Tool,如ControlsAddDataCommandClass、ControlsMapZoomInToolClass、ControlsMapPanToolClass等等,這些內(nèi)置對象在ESRI.ArcGIS.Controls命名空間中,你可以對象瀏覽器中查看。而且這些內(nèi)置對象
17、的調(diào)用方式都類似,如下所示:/定義ICommand command = new ControlsSaveAsDocCommandClass();/創(chuàng)立 command.OnCreate(m_controlsSynchronizer.ActiveControl);/調(diào)用 command.OnClick();希望你可以舉一反三,去實(shí)現(xiàn)更多的你想要的功能。第三講 MapControl與PageLayoutControl同步在ArcMap中,能夠很方面地進(jìn)行MapView和Layout View兩種視圖的切換,而且二者之間的數(shù)據(jù)是同步顯示的。關(guān)于兩種視圖同步的實(shí)現(xiàn)方法有多種,可以使用ObjectCop
18、y對象進(jìn)行數(shù)據(jù)硬拷貝,而比較簡單的方法莫過于二者共享一份地圖了,這也是最常用的方法。1、 新建同步類ControlsSynchronizer在解決方案面板中右擊工程名,選擇“添加|類,在類別中選擇“Visual C#工程項(xiàng),在模板中選擇“類,輸入類名“ControlsSynchronizer.cs,將以下代碼覆蓋自動(dòng)生成的代碼:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.IO;using
19、 System.Runtime.InteropServices;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.SystemUI;namespace _sdnMap / <summary> / This class is used to synchronize a gven PageLayoutControl and a MapControl. / When initialized, the user must pass the ref
20、erence of these control to the class, bind / the control together by calling 'BindControls' which in turn sets a joined Map referenced / by both control; and set all the buddy controls joined between these two controls. / When alternating between the MapControl and PageLayoutControl, you sho
21、uld activate the visible control / and deactivate the other by calling ActivateXXX. / This calss is limited to a situation where the controls are not simultaneously visible. / </summary> public class ControlsSynchronizer #region class members private IMapControl3 m_mapControl = null; private I
22、PageLayoutControl2 m_pageLayoutControl = null; private ITool m_mapActiveTool = null; private ITool m_pageLayoutActiveTool = null; private bool m_IsMapCtrlactive = true; private ArrayList m_frameworkControls = null; #endregion #region constructor / <summary> / 默認(rèn)構(gòu)造函數(shù) / </summary> public C
23、ontrolsSynchronizer() /初始化ArrayList m_frameworkControls = new ArrayList(); / <summary> / 構(gòu)造函數(shù) / </summary> public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl) : this() /為類成員賦值 m_mapControl = mapControl; m_pageLayoutControl = pageLayoutControl; #end
24、region #region properties / <summary> / 取得或設(shè)置MapControl / </summary> public IMapControl3 MapControl get return m_mapControl; set m_mapControl = value; / <summary> / 取得或設(shè)置PageLayoutControl / </summary> public IPageLayoutControl2 PageLayoutControl get return m_pageLayoutControl
25、; set m_pageLayoutControl = value; / <summary> / 取得當(dāng)前ActiveView的類型 / </summary> public string ActiveViewType get if (m_IsMapCtrlactive) return "MapControl" else return "PageLayoutControl" / <summary> / 取得當(dāng)前活動(dòng)的Control / </summary> public object ActiveContro
26、l get if (m_mapControl = null | m_pageLayoutControl = null) throw new Exception("ControlsSynchronizer:ActiveControl:rnEither MapControl or PageLayoutControl are not initialized!"); if (m_IsMapCtrlactive) return m_mapControl.Object; else return m_pageLayoutControl.Object; #endregion #region
27、 Methods / <summary> / 激活MapControl并解除the PagleLayoutControl / </summary> public void ActivateMap() try if (m_pageLayoutControl = null | m_mapControl = null) throw new Exception("ControlsSynchronizer:ActivateMap:rnEither MapControl or PageLayoutControl are not initialized!"); /
28、緩存當(dāng)前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,作為活動(dòng)的tool,賦給MapCont
29、rol的CurrentTool if (m_mapActiveTool != null) m_mapControl.CurrentTool = m_mapActiveTool; m_IsMapCtrlactive = true; /為每一個(gè)的framework controls,設(shè)置Buddy control為MapControl this.SetBuddies(m_mapControl.Object); catch (Exception ex) throw new Exception(string.Format("ControlsSynchronizer:ActivateMap:r
30、n0", ex.Message); / <summary> / 激活PagleLayoutControl并減活MapCotrol / </summary> public void ActivatePageLayout() try if (m_pageLayoutControl = null | m_mapControl = null) throw new Exception("ControlsSynchronizer:ActivatePageLayout:rnEither MapControl or PageLayoutControl are not
31、 initialized!"); /緩存當(dāng)前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最后使用的to
32、ol,作為活動(dòng)的tool,賦給PageLayoutControl的CurrentTool if (m_pageLayoutActiveTool != null) m_pageLayoutControl.CurrentTool = m_pageLayoutActiveTool; m_IsMapCtrlactive = false; /為每一個(gè)的framework controls,設(shè)置Buddy control為PageLayoutControl this.SetBuddies(m_pageLayoutControl.Object); catch (Exception ex) throw new
33、 Exception(string.Format("ControlsSynchronizer:ActivatePageLayout:rn0", ex.Message); / <summary> / 給予一個(gè)地圖, 置換PageLayoutControl和MapControl的focus map / </summary> public void ReplaceMap(IMap newMap) if (newMap = null) throw new Exception("ControlsSynchronizer:ReplaceMap:rnNe
34、w map for replacement is not initialized!"); if (m_pageLayoutControl = null | m_mapControl = null) throw new Exception("ControlsSynchronizer:ReplaceMap:rnEither MapControl or PageLayoutControl are not initialized!"); /create a new instance of IMaps collection which is needed by the Pa
35、geLayout /創(chuàng)立一個(gè)PageLayout需要用到的,新的IMaps collection的實(shí)例 IMaps maps = new Maps(); /add the new map to the Maps collection /把新的地圖加到Maps collection里頭去 maps.Add(newMap); bool bIsMapActive = m_IsMapCtrlactive; /call replace map on the PageLayout in order to replace the focus map /we must call ActivatePageLay
36、out, since it is the control we call 'ReplaceMaps' /調(diào)用PageLayout的replace map來置換focus map /我們必須調(diào)用ActivatePageLayout,因?yàn)樗悄莻€(gè)我們可以調(diào)用"ReplaceMaps"的Control this.ActivatePageLayout(); m_pageLayoutControl.PageLayout.ReplaceMaps(maps); /assign the new map to the MapControl /把新的地圖賦給MapControl
37、 m_mapControl.Map = newMap; /reset the active tools /重設(shè)active tools m_pageLayoutActiveTool = null; m_mapActiveTool = null; /make sure that the last active control is activated /確認(rèn)之前活動(dòng)的control被激活 if (bIsMapActive) this.ActivateMap(); m_mapControl.ActiveView.Refresh(); else this.ActivatePageLayout();
38、m_pageLayoutControl.ActiveView.Refresh(); / <summary> / bind the MapControl and PageLayoutControl together by assigning a new joint focus map / 指定共同的Map來把MapControl和PageLayoutControl綁在一起 / </summary> / <param name="activateMapFirst">true if the MapControl supposed to be a
39、ctivated first,如果MapControl被首先激活,那么為true</param> public void BindControls(IMapControl3 mapControl, IPageLayoutControl2 pageLayoutControl, bool activateMapFirst) if (mapControl = null | pageLayoutControl = null) throw new Exception("ControlsSynchronizer:BindControls:rnEither MapControl or
40、PageLayoutControl are not initialized!"); m_mapControl = MapControl; m_pageLayoutControl = pageLayoutControl; this.BindControls(activateMapFirst); / <summary> / bind the MapControl and PageLayoutControl together by assigning a new joint focus map / 指定共同的Map來把MapControl和PageLayoutControl綁在
41、一起 / </summary> / <param name="activateMapFirst">true if the MapControl supposed to be activated first,如果MapControl被首先激活,那么為true</param> public void BindControls(bool activateMapFirst) if (m_pageLayoutControl = null | m_mapControl = null) throw new Exception("Control
42、sSynchronizer:BindControls:rnEither MapControl or PageLayoutControl are not initialized!"); /create a new instance of IMap /創(chuàng)造IMap的一個(gè)實(shí)例 IMap newMap = new MapClass(); newMap.Name = "Map" /create a new instance of IMaps collection which is needed by the PageLayout /創(chuàng)造一個(gè)新的IMaps collectio
43、n的實(shí)例,這是PageLayout所需要的 IMaps maps = new Maps(); /add the new Map instance to the Maps collection /把新的Map實(shí)例賦給Maps collection maps.Add(newMap); /call replace map on the PageLayout in order to replace the focus map /調(diào)用PageLayout的replace map來置換focus map m_pageLayoutControl.PageLayout.ReplaceMaps(maps); /
44、assign the new map to the MapControl /把新的map賦給MapControl m_mapControl.Map = newMap; /reset the active tools /重設(shè)active tools m_pageLayoutActiveTool = null; m_mapActiveTool = null; /make sure that the last active control is activated /確定最后活動(dòng)的control被激活 if (activateMapFirst) this.ActivateMap(); else th
45、is.ActivatePageLayout(); / <summary> /by passing the application's toolbars and TOC to the synchronization class, it saves you the /management of the buddy control each time the active control changes. This method ads the framework /control to an array; once the active control changes, the
46、 class iterates through the array and /calles SetBuddyControl on each of the stored framework control. / </summary> / <param name="control"></param> public void AddFrameworkControl(object control) if (control = null) throw new Exception("ControlsSynchronizer:AddFrameworkControl:rnAdded control is not initialized!"); m_frameworkControls.Add(control); / <summary> / Remove a framework control from the managed list of controls / </summary> / <param name="control"><
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T-ZGTX 27-2025 原生態(tài)雪域滑雪能力要求規(guī)范
- T-ZSM 0059-2024“領(lǐng)跑者”評價(jià)技術(shù)要求 數(shù)控圓鋸床
- 二零二五年度房屋租賃合同租賃雙方租賃期間租賃物租賃權(quán)法律適用協(xié)議
- 2025年度汽車行業(yè)代理招聘人才合作協(xié)議
- 2025年度餐廳員工勞動(dòng)合同試用期規(guī)定
- 鋼結(jié)構(gòu)合同補(bǔ)充協(xié)議(2025年度)安裝工程
- 二零二五年度危險(xiǎn)品車輛運(yùn)輸司機(jī)安全責(zé)任協(xié)議
- 2025年度食品飲料經(jīng)銷商授權(quán)及市場開發(fā)協(xié)議
- 二零二五年度借車車輛損失免責(zé)合同
- 二零二五年度雙方個(gè)人教育培訓(xùn)合作協(xié)議
- 2024-2025年中國鋰電池隔膜行業(yè)未來發(fā)展趨勢分析及投資規(guī)劃建議研究報(bào)告
- 2024年南昌健康職業(yè)技術(shù)學(xué)院高職單招職業(yè)技能測驗(yàn)歷年參考題庫(頻考版)含答案解析
- 2025浙江中煙招聘高頻重點(diǎn)提升(共500題)附帶答案詳解
- 月子會所護(hù)理人員禮儀
- 校園安全隱患排查培訓(xùn)
- 《化妝品包裝材料相容性試驗(yàn)評估指南》
- 無人機(jī)行業(yè)調(diào)查研究報(bào)告
- 2022版藝術(shù)新課標(biāo)解讀心得(課件)小學(xué)美術(shù)
- 四川政采評審專家入庫考試基礎(chǔ)題復(fù)習(xí)試題
- 鋰離子電池失效分析及后果PFMEA-電子表格版
- 2024解析:第十九章生活用電-基礎(chǔ)練(解析版)
評論
0/150
提交評論