實(shí)例開發(fā)教程添加標(biāo)注_第1頁(yè)
實(shí)例開發(fā)教程添加標(biāo)注_第2頁(yè)
實(shí)例開發(fā)教程添加標(biāo)注_第3頁(yè)
實(shí)例開發(fā)教程添加標(biāo)注_第4頁(yè)
實(shí)例開發(fā)教程添加標(biāo)注_第5頁(yè)
已閱讀5頁(yè),還剩73頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、第一講 桌面GIS應(yīng)用程序框架的建立本講主要是使用MapControl、PageLayoutControl、ToolbarControl、TOCControl四個(gè)控件建立起基本的桌面GIS應(yīng)用程序框架。最終成果預(yù)覽如下:1、新建項(xiàng)目啟動(dòng)VS2005,選擇“文件|新建|項(xiàng)目”,在項(xiàng)目類型中選擇Visual C#,再選擇Windows應(yīng)用程序模板,輸入名稱“3sdnMap”,點(diǎn)擊確定。 在解決方案管理器中將“Form1.cs”重命名為“3sdnMap.cs”,在設(shè)計(jì)視圖中,選中窗體,將其屬性中的“Text”改為“3sdnMap”。2、添加控件選擇工具箱中的“菜單和工具欄|MenuStrip”,將其

2、拖入窗體。選擇工具箱中的“ArcGIS Windows Forms”節(jié),將“ToolbarControl”控件拖入窗體,并將其屬性中的Dock設(shè)置為Top。選擇工具箱中的“菜單和工具欄|StatusStrip”,將其拖入到窗體。選擇工具箱中的“容器|SplitContainer”容器拖入窗體,并將其屬性中的Dock設(shè)置為Fill。將TabControl控件拖入Panel1,將Alignment屬性設(shè)置為Bottom,Dock屬性設(shè)置為Fill。點(diǎn)擊TabPages屬性右邊的按鈕,彈出TabPage集合編輯器,將tabPage1的Name設(shè)置為tabPageLayer,Text設(shè)置為圖層,將ta

3、bPage2的Name設(shè)置為tabPageProperty,Text設(shè)置為屬性。如下所示。 選擇“圖層”選項(xiàng)卡,拖入TOCControl控件,設(shè)置Dock屬性為Fill。選擇“屬性”選項(xiàng)卡,拖入DataGridView控件,設(shè)置Dock屬性為Fill。拖入TabControl控件到Panel2,設(shè)置Dock屬性為Fill。并上述類似的方法,將兩個(gè)選項(xiàng)卡的Name和Text分別設(shè)置為:(tabPageMap、地圖),(tabPageLayout,制版)。選擇“地圖”選項(xiàng)卡,拖入MapControl控件,設(shè)置Dock屬性為Fill。選擇“制版”選項(xiàng)卡,拖入PageLayoutControl控件,設(shè)

4、置Dock屬性為Fill。最后將LicenseControl控件拖入到窗體的任意地方。按F5編譯運(yùn)行,可以看到剛才布局好的程序界面了。3、控件綁定通過以上步驟添加的控件還只是單獨(dú)存在,而我們的程序需要各控件間協(xié)同工作,因此要進(jìn)行控件綁定。分別右擊ToolbarControl、TOCControl控件,將Buddy設(shè)置為axMapControl1,如下圖所示。 這樣,工具條和圖層控件就與地圖控件關(guān)聯(lián)了。4、添加工具此時(shí),工具條中還沒有任何工具,添加的方法也很簡(jiǎn)單。右擊ToolbarControl,選擇“屬性|Items”,點(diǎn)擊Add,選擇Commands選項(xiàng)卡中的Generic,雙擊Open、S

5、aveAs、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)搭建好了,你可以通過工具條的工具打開地圖文檔,瀏覽地圖了,效果如開篇所示。下一講中將大家?guī)淼氖遣藛蔚奶砑蛹捌鋵?shí)現(xiàn),敬請(qǐng)關(guān)注3SDN.NET。第二講 菜單的添加及其實(shí)現(xiàn)在上一講中,我們實(shí)現(xiàn)了應(yīng)用程序基本框架,其中有個(gè)小錯(cuò)誤,在此先跟大家說明下。在“屬性”選項(xiàng)卡中,我們當(dāng)時(shí)添加的是 DataGrid

6、View 控件,這個(gè)控件是用來顯示數(shù)據(jù)表的,而專門用于屬性的查詢和設(shè)置的控件是 PropertyGrid 控件。因此請(qǐng)你刪除“屬性”選項(xiàng)卡中的 DataGridView 控件,再把位于“工具箱 | 所有 Windows 窗體 |PropertyGrid ”(如果沒有,右擊選擇“選擇項(xiàng)”以添加此控件)控件拖到該選項(xiàng)卡。 在這一講中,主要講解菜單的添加和實(shí)現(xiàn)。 1、 添加菜單 在設(shè)計(jì)視圖中,單擊菜單欄,會(huì)出現(xiàn)“請(qǐng)?jiān)诖颂庢I入”的提示,單擊提示就可以鍵入菜單名稱,如“文件”,再單擊“文件”,即可輸入其下拉子菜單,如下所示: Tips : 每創(chuàng)建一個(gè)菜單,請(qǐng)?jiān)谄鋵傩悦姘逯性O(shè)置 Name 屬性,而且不要為

7、中文,因此 Name 值將是此菜單響應(yīng)函數(shù)的函數(shù)名的一部分,帶中文的函數(shù)名,總是不好吧。 本講中,我們將添加新建( New )、打開( Open )、添加數(shù)據(jù)( AddData )、保存( Save )、另存為( SaveAs )、退出( Exit )這些菜單,()內(nèi)為相應(yīng)的 Name 屬性值。 Tips: 你可以在屬性面板中的 Text 屬性中,把菜單名設(shè)置為中英文形式,如“打開 O pen ”,帶下劃線的 O 表示此項(xiàng)菜單的快捷鍵是字母 O ,設(shè)置方法是在相應(yīng)字母前加上“ & ”字符,如“打開 &Open ”。但這種快捷鍵只在打開此下拉菜單時(shí)才有效,即當(dāng)你單擊“文件”菜單彈

8、出下拉菜單時(shí),按下字母 O 就可以定位到“打開”菜單。 還有一種在程序運(yùn)行時(shí)都有效的全局快捷鍵,可以在屬性面板中的“ ShortCutKeys ”中設(shè)置。 你還可以在屬性面板中的 Image 屬性中設(shè)置你喜歡的菜單圖標(biāo)。單擊 Image 那一行右邊的按鈕,彈出如下菜單。選擇“項(xiàng)目資源文件”,再單擊導(dǎo)入就可以選擇你的圖標(biāo)了。 最終效果如下所示。 注意,在解決方案面板中,選中剛才添加的所有圖標(biāo),在其屬性面板中將生成操作設(shè)置為“嵌入的資源”,這一點(diǎn)很重要! 2、 實(shí)現(xiàn)相關(guān)菜單 首先定義指針(寫在 public partial class Form1 : Form 下面即可): 3 m_mapCont

9、rol = null; 2 m_pageLayoutControl = null; private IMapDocument pMapDocument; 若以上指針無效,請(qǐng)?zhí)砑右韵乱茫?using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Geometry; using ESRI.ArcGIS.SystemUI; 在設(shè)計(jì)視圖中的屬性面板中,選擇 Form1 ,即主窗體,單擊事件按鈕(閃電形狀的

10、那個(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í)現(xiàn)代碼如下: / <summary> / 新建地圖命令 / </summary>

11、/ <param name="sender"></param> / <param name="e"></param> private void New_Click(object sender, EventArgs e) / 本命令涉及到 MapControl 和 PageLayoutControl 同步問題,將在下一講中實(shí)現(xiàn) / <summary> / 打開地圖文檔 Mxd 命令 / </summary> / <param name="sender">

12、</param> / <param name="e"></param> private void Open_Click(object sender, EventArgs e) / 本命令涉及到 MapControl 和 PageLayoutControl 同步問題,將在下一講中實(shí)現(xiàn) / <summary> / 添加數(shù)據(jù)命令 / </summary> / <param name="sender"></param> / <param name="e"

13、></param> private void AddData_Click(object sender, EventArgs e) int currentLayerCount = this.axMapControl1.LayerCount; ICommand pCommand = new ControlsAddDataCommandClass(); pCommand.OnCreate(this.axMapControl1.Object); pCommand.OnClick(); / <summary> / 保存地圖文檔命令 / </summary> /

14、<param name="sender"></param> / <param name="e"></param> private void Save_Click(object sender, EventArgs e) / 首先確認(rèn)當(dāng)前地圖文檔是否有效 if (null!= m_pageLayoutControl.DocumentFilename && m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename) / 創(chuàng)建一個(gè)新的

15、地圖文檔實(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.UsesRelativePaths, false); mapDo

16、c.Close(); / <summary> / 另存為地圖文檔命令 / </summary> / <param name="sender"></param> / <param name="e"></param> private void SaveAs_Click(object sender, EventArgs e) / 調(diào)用另存為命令 ICommand command = new ControlsSaveAsDocCommandClass(); command.OnCreate(

17、m_controlsSynchronizer.ActiveControl); command.OnClick(); / <summary> / 退出程序 / </summary> / <param name="sender"></param> / <param name="e"></param> private void Exit_Click(object sender, EventArgs e) Application.Exit(); 3、 編譯運(yùn)行 按 F5 編譯運(yùn)行程序。也許你

18、會(huì)發(fā)現(xiàn),菜單命令的實(shí)現(xiàn)方式都是類型的。沒錯(cuò),在 AE9.2 中,內(nèi)置了許多常用的 Command 和 Tool ,如 ControlsAddDataCommandClass 、 ControlsMapZoomInToolClass 、 ControlsMapPanToolClass 等等,這些內(nèi)置對(duì)象在 ESRI.ArcGIS.Controls 命名空間中,你可以對(duì)象瀏覽器中查看。而且這些內(nèi)置對(duì)象的調(diào)用方式都類似,如下所示: / 定義 ICommand command = new ControlsSaveAsDocCommandClass(); / 創(chuàng)建 command.OnCreate(m_

19、controlsSynchronizer.ActiveControl); / 調(diào)用 command.OnClick(); 希望你可以舉一反三,去實(shí)現(xiàn)更多的你想要的功能。 在下一講中,我將給大家?guī)淼氖?MapControl 與 PageLayoutControl 兩種視圖同步的實(shí)現(xiàn),敬請(qǐng)關(guān)注 3SDN.NET 。 第三講 MapControl與PageLayoutControl同步在ArcMap中,能夠很方面地進(jìn)行MapView和Layout View兩種視圖的切換,而且二者之間的數(shù)據(jù)是同步顯示的。關(guān)于兩種視圖同步的實(shí)現(xiàn)方法有多種,可以使用ObjectCopy對(duì)象進(jìn)行數(shù)據(jù)硬拷貝,而比較簡(jiǎn)單的方

20、法莫過于二者共享一份地圖了,這也是最常用的方法。1、新建同步類ControlsSynchronizer在解決方案面板中右擊項(xiàng)目名,選擇“添加|類”,在類別中選擇“Visual C#項(xiàng)目項(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 System.Runtime

21、.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 reference of thes

22、e 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 should activate t

23、he 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 IPageLayoutCont

24、rol2 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 ControlsSynchro

25、nizer() /初始化ArrayList m_frameworkControls = new ArrayList(); / <summary> / 構(gòu)造函數(shù) / </summary> / <param name="mapControl"></param> / <param name="pageLayoutControl"></param> public ControlsSynchronizer(IMapControl3 mapControl, IPageLayoutControl2

26、 pageLayoutControl) : this() /為類成員賦值 m_mapControl = mapControl; m_pageLayoutControl = pageLayoutControl; #endregion #region properties / <summary> / 取得或設(shè)置MapControl / </summary> public IMapControl3 MapControl get return m_mapControl; set m_mapControl = value; / <summary> / 取得或設(shè)置Pag

27、eLayoutControl / </summary> public IPageLayoutControl2 PageLayoutControl get return m_pageLayoutControl; set m_pageLayoutControl = value; / <summary> / 取得當(dāng)前ActiveView的類型 / </summary> public string ActiveViewType get if (m_IsMapCtrlactive) return "MapControl" else return &

28、quot;PageLayoutControl" / <summary> / 取得當(dāng)前活動(dòng)的Control / </summary> public object ActiveControl get if (m_mapControl = null | m_pageLayoutControl = null) throw new Exception("ControlsSynchronizer:ActiveControl:rnEither MapControl or PageLayoutControl are not initialized!");

29、if (m_IsMapCtrlactive) return m_mapControl.Object; else return m_pageLayoutControl.Object; #endregion #region Methods / <summary> / 激活MapControl并解除the PagleLayoutControl / </summary> public void ActivateMap() try if (m_pageLayoutControl = null | m_mapControl = null) throw new Exception(&

30、quot;ControlsSynchronizer:ActivateMap:rnEither MapControl or PageLayoutControl are not initialized!"); /緩存當(dāng)前PageLayout的CurrentTool if (m_pageLayoutControl.CurrentTool != null) m_pageLayoutActiveTool = m_pageLayoutControl.CurrentTool; /解除PagleLayout m_pageLayoutControl.ActiveView.Deactivate();/激

31、活MapControl m_mapControl.ActiveView.Activate(m_mapControl.hWnd); /將之前MapControl最后使用的tool,作為活動(dòng)的tool,賦給MapControl的CurrentTool if (m_mapActiveTool != null) m_mapControl.CurrentTool = m_mapActiveTool; m_IsMapCtrlactive = true; /為每一個(gè)的framework controls,設(shè)置Buddy control為MapControl this.SetBuddies(m_mapCont

32、rol.Object); catch (Exception ex) throw new Exception(string.Format("ControlsSynchronizer:ActivateMap:rn0", ex.Message); / <summary> / 激活PagleLayoutControl并減活MapCotrol / </summary> public void ActivatePageLayout() try if (m_pageLayoutControl = null | m_mapControl = null) throw

33、new Exception("ControlsSynchronizer:ActivatePageLayout:rnEither MapControl or PageLayoutControl are not initialized!"); /緩存當(dāng)前MapControl的CurrentTool if (m_mapControl.CurrentTool != null) m_mapActiveTool = m_mapControl.CurrentTool; /解除MapControl m_mapControl.ActiveView.Deactivate(); /激活PageL

34、ayoutControl m_pageLayoutControl.ActiveView.Activate(m_pageLayoutControl.hWnd); /將之前PageLayoutControl最后使用的tool,作為活動(dòng)的tool,賦給PageLayoutControl的CurrentTool if (m_pageLayoutActiveTool != null) m_pageLayoutControl.CurrentTool = m_pageLayoutActiveTool; m_IsMapCtrlactive = false; /為每一個(gè)的framework controls,設(shè)

35、置Buddy control為PageLayoutControl this.SetBuddies(m_pageLayoutControl.Object); catch (Exception ex) throw new Exception(string.Format("ControlsSynchronizer:ActivatePageLayout:rn0", ex.Message); / <summary> / 給予一個(gè)地圖, 置換PageLayoutControl和MapControl的focus map / </summary> / <par

36、am name="newMap"></param> public void ReplaceMap(IMap newMap) if (newMap = null) throw new Exception("ControlsSynchronizer:ReplaceMap:rnNew map for replacement is not initialized!"); if (m_pageLayoutControl = null | m_mapControl = null) throw new Exception("Controls

37、Synchronizer:ReplaceMap:rnEither MapControl or PageLayoutControl are not initialized!"); /create a new instance of IMaps collection which is needed by the PageLayout /創(chuàng)建一個(gè)PageLayout需要用到的,新的IMaps collection的實(shí)例 IMaps maps = new Maps(); /add the new map to the Maps collection /把新的地圖加到Maps collecti

38、on里頭去 maps.Add(newMap); bool bIsMapActive = m_IsMapCtrlactive; /call replace map on the PageLayout in order to replace the focus map /we must call ActivatePageLayout, since it is the control we call 'ReplaceMaps' /調(diào)用PageLayout的replace map來置換focus map /我們必須調(diào)用ActivatePageLayout,因?yàn)樗悄莻€(gè)我們可以調(diào)用&qu

39、ot;ReplaceMaps"的Control this.ActivatePageLayout(); m_pageLayoutControl.PageLayout.ReplaceMaps(maps); /assign the new map to the MapControl /把新的地圖賦給MapControl m_mapControl.Map = newMap; /reset the active tools /重設(shè)active tools m_pageLayoutActiveTool = null; m_mapActiveTool = null; /make sure that

40、 the last active control is activated /確認(rèn)之前活動(dòng)的control被激活 if (bIsMapActive) this.ActivateMap(); m_mapControl.ActiveView.Refresh(); else this.ActivatePageLayout(); m_pageLayoutControl.ActiveView.Refresh(); / <summary> / bind the MapControl and PageLayoutControl together by assigning a new joint

41、focus map / 指定共同的Map來把MapControl和PageLayoutControl綁在一起 / </summary> / <param name="mapControl"></param> / <param name="pageLayoutControl"></param> / <param name="activateMapFirst">true if the MapControl supposed to be activated first,

42、如果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 PageLayoutContro

43、l 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綁在一起 / </summar

44、y> / <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("ControlsSynchronizer:Bin

45、dControls: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 collection的實(shí)例,這是PageLayout

46、所需要的 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); /assign the new ma

47、p 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 this.ActivatePageLa

48、yout(); / <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 class iterates t

49、hrough 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_f

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(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ì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論