




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第一講桌面GIS應(yīng)用程序框架的建立
本講主要是使用MapControRPageLayoutControl>ToolbarControl,TOCControl四個(gè)控件建
立起基本的桌面GIS應(yīng)用程序框架。最終成果預(yù)覽如下:
1、新建項(xiàng)目
啟動(dòng)VS2005,選擇''文件I新建I項(xiàng)目”,在項(xiàng)目類型中選擇VisualC#,再選擇Windows應(yīng)用
程序模板,輸入名稱“3sdnMap”,點(diǎn)擊確定。
在解決方案管理器中將“Forml.cs”重命名為“3sdnMap.cs”,在設(shè)計(jì)視圖中,選中窗體,
將其屬性中的“Text”改為“3sdnMap”。
2、添加控件
選擇工具箱中的“菜單和工具欄IMenuStrip”,將其拖入窗體。
選擇工具箱中的“ArcGISWindowsForms”節(jié),將“ToolbarControl”控件拖入窗體,并將
其屬性中的Dock設(shè)置為T(mén)op。
選擇工具箱中的“菜單和工具欄IStatusStrip”,將其拖入到窗體。
選擇工具箱中的“容器ISplitContainer”容器拖入窗體,并將其屬性中的Dock設(shè)置為Fill。
將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。并上述類似的方法,將兩個(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、控件綁定
通過(guò)以上步驟添加的控件還只是單獨(dú)存在,而我們的程序需要各控件間協(xié)同工作,因此要進(jìn)
行控件綁定。
分別右擊ToolbarControl、TOCControl控件,將Buddy設(shè)置為axMapControl1,如下圖所示。
這樣,工具條和圖層控件就與地圖控件關(guān)聯(lián)了。
4、添加工具
此時(shí),工具條中還沒(méi)有任何工具,添加的方法也很簡(jiǎn)單。右擊ToolbarControl,選擇“屬性
Iltems”,點(diǎn)擊Add,選擇Commands選項(xiàng)卡中的Generic,雙擊Open、SaveAs>Redo、Undo
即可將相應(yīng)工具添加到工具條。
常見(jiàn)的工具有:
MapNavigation中的導(dǎo)航工具,MapInquiry中的查詢工具,F(xiàn)eatureSelection中的選擇工具,
你可以根據(jù)需要酌情添加工具。
5、編譯運(yùn)行
按F5即可編譯運(yùn)行程序,至此桌面GIS應(yīng)用程序框架基本框架已經(jīng)搭建好了,你可以通過(guò)
工具條的工具打開(kāi)地圖文檔,瀏覽地圖了,效果如開(kāi)篇所示。
第二講菜單的添加及其實(shí)現(xiàn)
在GISEngine+C#實(shí)例開(kāi)發(fā)教程》第一講桌面G1S應(yīng)用程序框架的建立"
href="/gis2dev/ae/2009-03-30/429.hlml">上一講中,我們實(shí)現(xiàn)了應(yīng)用程序基本框架,其中有個(gè)
小錯(cuò)誤,在此先跟大家說(shuō)明下。在“屬性”選項(xiàng)卡中,我們當(dāng)時(shí)添加的是DataGridView控
件,這個(gè)控件是用來(lái)顯示數(shù)據(jù)表的,而專門(mén)用于屬性的查詢和設(shè)置的控件是PropertyGrid控
件.因此請(qǐng)你刪除“屬性”選項(xiàng)卡中的DataGridView控件,再把位于“工具箱I所有
Windows窗體IPropertyGrid"(如果沒(méi)有,右擊選擇"選擇項(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屬性,而且不要為中文,因此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”,帶
下劃線的0表示此項(xiàng)菜單的快捷鍵是字母0,設(shè)置方法是在相應(yīng)字母前加上“&”字
符,如''打開(kāi)&Open但這種快捷鍵只在打開(kāi)此下拉菜單時(shí)才有效,即當(dāng)你單擊“文件”
菜單彈出下拉菜單時(shí),按下字母O就可以定位到“打開(kāi)”菜單。
還有一種在程序運(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)菜單
首先定義指針(寫(xiě)在publicpartialclassForml:Form下面即可):
privateESRLArcGIS.Controls.IMapControl3m_mapControl=null;
privateESRI.ArcGIS.Controls.IPageLayoutControl2m_pageLayoutControl=null;
privateIMapDocumentpMapDocument;
若以上指針無(wú)效,請(qǐng)?zhí)砑右韵乱茫?/p>
usingESRI.ArcGIS.Carto;
usingESRI.ArcGIS.Controls;
usingESRI.ArcGIS.esriSystem;
usingESRI.ArcGIS.Display;
usingESRI.ArcGIS.Geometry;
usingESRLArcGIS.SystemUI;
在設(shè)計(jì)視圖中的屬性面板中,選擇Forml,即主窗體,單擊事件按鈕(閃電形狀的那個(gè)按
鈕),打到“Load”事件并雙擊,添加此事件。
在Forml_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)代碼如下:
III〈summary〉
///新建地圖命令
///</summary>
III<paramname=,,sender',x/param>
///<paramname=Me"></param>
privatevoidNew_Click(objectsender,EventArgse)
(
//本命令涉及到MapControl和PageLayoutControl同步問(wèn)題,將在下一講中實(shí)現(xiàn)
)
///<summary>
///打開(kāi)地圖文檔Mxd命令
III</summary>
III<paramname=Msender,,x/param>
///<paramname=ne"></param>
privatevoidOpen_Click(objectsender,EventArgse)
//本命令涉及到MapControl和PageLayoutControl同步問(wèn)題,將在下一講中實(shí)現(xiàn)
)
III<summary>
III添加數(shù)據(jù)命令
III</summary>
///<paramname=nsendernx/param>
///<paramname="e,'></param>
privatevoidAddData_Click(objectsender,EventArgse)
(
intcurrentLayerCount=this.axMapControl1.LayerCount;
ICommandpCommand=newControlsAddDataCommandClass();
pCommand.OnCreate(this.axMapControl1.Object);
pCommand.OnClick();
}
///〈summary〉
///保存地圖文檔命令
///</summary>
///<paramname=Msendern></paraTn>
///<paramname=Me"></param>
privatevoidSave_Click(objectsender,EventArgse)
//首先確認(rèn)當(dāng)前地圖文檔是否有效
if(null!=m_pageLayoutControl.DocumentFilename&&
m_mapControl.CheckMxFile(m_pageLayoutControl.DocumentFilename))
(
//創(chuàng)建一個(gè)新的地圖文檔實(shí)例
IMapDocumentmapDoc=newMapDocumentClass();
//打開(kāi)當(dāng)前地圖文檔
mapDoc.Open(m_pageLayoutControl.DocumentFilename,string.Empty);
//用PageLayout中的文檔替換當(dāng)前文檔中的PageLayout部分
mapDoc.ReplaceContents((IMxdContents)m_pageLayoutControLPageLayout);
〃保存地圖文檔
mapDoc.Save(mapDoc.UsesRelativePaths,false);
mapDoc.CloseO;
)
)
///<summary>
III另存為地圖文檔命令
III</summary>
III<paramname=,'sender,,x/param>
III<paramname="e"x/param>
privatevoidSaveAs_Click(objectsender,EventArgse)
//調(diào)用另存為命令
ICommandcommand=newControlsSaveAsDocCommandClass();
command.OnCreate(m_controlsSynchronizer.ActiveControl);
command.OnClick();
)
///<summary>
///退出程序
III</summary>
///<paramname=,'sender',x/param>
III<paramname=ne"x/param>
privatevoidExit_Click(objectsender,EventArgse)
(
Application.Exit();
)
3、編譯運(yùn)行
按F5編譯運(yùn)行程序。也許你會(huì)發(fā)現(xiàn),菜單命令的實(shí)現(xiàn)方式都是類型的。沒(méi)錯(cuò),在AE9.2中,
內(nèi)置了許多常用的Command和Tool,如ControlsAddDataCommandClass、
ControlsMapZoomlnToolClass、ControlsMapPanToolClass等等,這些內(nèi)置對(duì)象在
ESRLArcGIS.Controls命名空間中,你可以對(duì)象瀏覽器中查看。而且這些內(nèi)置對(duì)象的調(diào)用方
式都類似,如下所示:
//定義
ICommandcommand=newControlsSaveAsDocCommandClass();
//創(chuàng)建
command.OnCreate(m_controlsSynchronizer.ActiveControl);
//調(diào)用
command.OnClick();
第三講MapControl與PageLayoutControl同步
在ArcMap中,能夠很方面地進(jìn)行MapView和LayoutView兩種視圖的切換,而且二者之間
的數(shù)據(jù)是同步顯示的。
關(guān)于兩種視圖同步的實(shí)現(xiàn)方法有多種,可以使用ObjectCopy對(duì)象進(jìn)行數(shù)據(jù)硬拷貝,而比較
簡(jiǎn)單的方法莫過(guò)于二者共享一份地圖了,這也是最常用的方法。
1>新建同步類ControlsSynchronizer
在解決方案面板中右擊項(xiàng)目名,選擇“添加I類”,在類別中選擇“VisualC#項(xiàng)目項(xiàng)",在模
板中選擇“類”,輸入類名“ControlsSynchronizer.es",將以下代碼覆蓋自動(dòng)生成的代碼:
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.
IllWheninitialized,theusermustpassthereferenceofthesecontroltotheclass,bind
///thecontroltogetherbycalling'BindControls*whichinturnsetsajoinedMapreferenced
///bybothcontrol;andsetallthebuddycontrolsjoinedbetweenthesetwocontrols.
///WhenalternatingbetweentheMapControlandPageLayoutControl,youshouldactivate
thevisiblecontrol
///anddeactivatetheotherbycallingActivateXXX.
///Thiscalssislimitedtoasituationwherethecontrolsarenotsimultaneouslyvisible.
Ill</summary>
publicclassControlsSynchronizer
{
#regionclassmembers
privateIMapControl3m_mapControl=null;
privateIPageLayoutControl2m_pageLayoutControl=null;
privateIToolm_mapActiveTool=null;
privateIToolm_pageLayoutActiveTool=null;
privateboolm_IsMapCtrlactive=true;
privateArrayListm_frameworkControls=null;
#endregion
#regionconstructor
III<summary>
///默認(rèn)構(gòu)造函數(shù)
///</summary>
publicControlsSynchronizer()
(
〃初始化ArrayList
m_frameworkControls=newArrayList();
}
///<summary>
///構(gòu)造函數(shù)
///</summary>
///<paramname="mapContror,x/param>
///<paramname=,'pageLayoutContror,></param>
publicControlsSynchronizer(IMapControl3mapControl,IPageLayoutControl2
pageLayoutControl)
:this()
(
〃為類成員賦值
m_mapControl=mapControl;
m_pageLayoutControl=pageLayoutControl;
)
#endregion
#regionproperties
///<summary>
///取得或設(shè)置MapControl
///</summary>
publicIMapControl3MapControl
(
get{returnm_mapControl;}
set{m_mapControl=value;}
)
///<summary>
///取得或設(shè)置PageLayoutControl
///</summary>
publicIPageLayoutControl2PageLayoutControl
(
get{returnm_pageLayoutControl;}
set{m_pageLayoutControl=value;}
)
///<summary>
///取得當(dāng)前ActiveView的類型
///</summary>
publicstringActiveViewType
(
get
(
if(m_IsMapCtrlactive)
return"MapConlrol”;
else
return"PageLayoutControl;
///<summary>
III取得當(dāng)前活動(dòng)的Control
///</summary>
publicobjectActiveControl
(
get
(
if(m_mapControl==nullIIm_pageLayoutControl=null)
thrownewException("ControlsSynchronizer::ActiveControl:\r\nEither
MapControlorPageLayoutControlarenotinitialized!");
if(mJsMapCtrlactive)
returnm_mapControl.Object;
else
returnm_pageLayoutControl.Object;
}
)
#endregion
#regionMethods
///<summary>
///激活MapControl并解除thePagleLayoutControl
III</summary>
publicvoidActivateMapO
(
try
(
if(m_pageLayoutControl==nullIIm_mapControl==null)
thrownewException("ControlsSynchronizer::ActivateMap:\r\nEither
MapControlorPageLayoutControlarenotinitialized!'1);
〃緩存當(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,賦給M叩Control
的CurrentTool
if(m_mapActiveTool!=null)m_mapControl.CurrentTool=
m_mapActiveTool;
m_IsMapCtrlactive=true;
〃為每一個(gè)的frameworkcontrols,設(shè)置Buddycontrol為MapControl
this.SetBuddies(m_mapControl.Object);
)
catch(Exceptionex)
(
thrownew
Exception(string.FormatC'ControlsSynchronizer::ActivateMap:\r\n{0}",ex.Message));
///<summary>
///激活PagleLayoutControl并減活MapCotrol
///</summary>
publicvoidActivatePageLayout()
try
if(m_pageLayoutControl==nullIIm_mapControl==null)
thrownew
Exception(uControlsSynchronizer::ActivatePageLayout:\r\nEitherMapControlor
PageLayoutControlarenotinitialized!0);
〃緩存當(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最后使用的tool,作為活動(dòng)的tool,賦給
PageLayoutControl的CurrentTool
if(m_pageLayoutActiveTool!=null)m_pageLayoutControLCurrentTool=
m_pageLayoutActiveTool;
m_IsMapCtrlactive=false;
〃為每個(gè)的frameworkcontrols,設(shè)置Buddycontrol為PageLayoutControl
this.SetBuddies(m_pageLayoutControl.Object);
)
catch(Exceptionex)
(
thrownew
Exception(string.Format(nControlsSynchronizer::ActivatePageLayout:\r\n{0}n,ex.Message));
///<summary>
///給予一個(gè)地圖,置換PageLayoutControl和M叩Control的focusm叩
///</summary>
III<paramname="newMap"></param>
publicvoidReplaceMap(IMapnewMap)
(
if(newMap==null)
thrownewException("ControlsSynchronizer::ReplaceMap:\r\nNewmapfor
replacementisnotinitialized!");
if(m_pageLayoutControl==nullIIm_mapControl==null)
thrownewException("ControlsSynchronizer::ReplaceMap:\r\nEither
MapControlorPageLayoutControlarenotinitialized!");
//createanewinstanceofIMapscollectionwhichisneededbythePageLayout
〃創(chuàng)建一個(gè)PageLayout需要用至U的,新的IMapscollection的實(shí)例
IMapsmaps=newMaps();
//addthenewmaptotheMapscollection
〃把新的地圖加到Mapscollection里頭去
maps.Add(newMap);
boolblsMapActive=m_IsMapCtrlactive;
//callreplacemaponthePageLayoutinordertoreplacethefocusmap
//wemustcallActivatePageLayout,sinceitisthecontrolwecall'ReplaceMaps1
〃調(diào)用PageLayout的replacemap來(lái)置換focusmap
〃我們必須調(diào)用ActivatePageLayout,因?yàn)樗悄莻€(gè)我們可以調(diào)用"Rep為ceMaps"
的Control
this.ActivatePageLayout();
m_pageLayoutControl.PageLayout.ReplaceMaps(maps);
//assignthenewmaptotheMapControl
〃把新的地圖賦給MapControl
m_mapControl.Map=newMap;
//resettheactivetools
〃重設(shè)activetools
m_pageLayoutActiveTool=null;
m_mapActiveTool=null;
//makesurethatthelastactivecontrolisactivated
〃確認(rèn)之前活動(dòng)的control被激活
if(blsM叩A(chǔ)ctive)
(
this.ActivateMapO;
m_mapControl.ActiveView.Refresh();
)
else
{
this.ActivatePageLayout();
m_pageLayoutControl.ActiveView.Refresh();
///<summary>
///bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocus
map
///指定共同的Map來(lái)把MapControl和PageLayoutControl綁在一起
III</summary>
///<paramname="mapContror,x/param>
///<paramname=npageLayoutControl"></param>
///<paramname=,,activateMapFirst,'>trueiftheMapControlsupposedtobeactivated
first,如果MapControl被首先激活,則為true</param>
publicvoidBindControls(IMapControl3mapControl,IPageLayoutControl2
pageLayoutControl,boolactivateMapFirst)
(
if(mapControl==nullIIpageLayoutControl==null)
thrownewException(nControlsSynchronizer::BindControls:\r\nEither
MapControlorPageLayoutControlarenotinitialized!");
m_mapControl=MapControl;
m_pageLayoutControl=pageLayoutControl;
this.BindControls(activateMapFirst);
)
///<summary>
///bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocus
map
///指定共同的Map來(lái)把MapControl和PageLayoutControl綁在?起
///</summary>
///<paramname=nactivateMapFirstn>trueiftheMapControlsupposedtobeactivated
first,如果MapControl被首先激活,則為true</param>
publicvoidBindControls(boolactivateMapFirst)
(
if(m_pageLayoutControl==nullIIm_mapControl==null)
thrownewException(HControlsSynchronizer::BindControls:\r\nEither
MapControlorPageLayoutControlarenotinitialized!");
//createanewinstanceofIMap
〃創(chuàng)造IMap的一個(gè)實(shí)例
IMapnewMap=newMapClass();
newMap.Name="Map”;
//createanewinstanceofIMapscollectionwhichisneededbythePageLayout
〃創(chuàng)造一個(gè)新的IMapscollection的實(shí)例,這是PageLayout所需要的
IMapsmaps=newMaps();
//addthenewMapinstancetotheMapscollection
//把新的M叩實(shí)例賦給Mapscollection
maps.Add(newMap);
//callreplacemaponthePageLayoutinordertoreplacethefocusmap
〃調(diào)用PageLayout的replacemap來(lái)置換focusmap
m_pageLayoutControl.PageLayout.ReplaceMaps(maps);
//assignthenewmaptotheMapControl
〃把新的m叩賦給MapControl
m_mapControI.Map=newMap;
//resettheactivetools
〃重設(shè)activetools
m_pageLayoutActiveTool=null;
m_mapActiveTool=null;
//makesurethatthelastactivecontrolisactivated
〃確定最后活動(dòng)的control被激活
if(activateMapFirst)
this.ActivateMapO;
else
this.ActivatePageLayout();
}
///<summary>
///bypassingtheapplication'stoolbarsandTOCtothesynchronizationclass,itsaves
youthe
///managementofthebuddycontroleachtimetheactivecontrolchanges.Thismethod
adstheframework
///controltoanarray;oncetheactivecontrolchanges,theclassiteratesthroughthearray
and
///callesSetBuddyControloneachofthestoredframeworkcontrol.
///</summary>
///<paramname=,'control"></param>
publicvoidAddFrameworkControl(objectcontrol)
(
if(control==null)
thrownew
Exception(uControlsSynchronizer::AddFrameworkControl:\r\nAddedcontrolisnotinitialized!11);
m_frameworkControls.Add(control);
)
///<summary>
IIIRemoveaframeworkcontrolfromthemanagedlistofcontrols
///</summary>
///<paramname=ncontror'></param>
publicvoidRemoveFrameworkControl(objectcontrol)
(
if(control==null)
thrownew
Exception(℃ontrolsSynchronizer::RemoveFrameworkControl:\r\nControltoberemovedisnot
initialized!");
m_frameworkControls.Remove(control);
}
///<summary>
///Removeaframeworkcontrolfromthemanagedlistofcontrolsbyspecifyingits
indexinthelist
///</summary>
///<paramname="index,'></param>
publicvoidRemoveFrameworkControlAt(intindex)
(
if(m_frameworkControls.Count<index)
thrownew
Exception(uControlsSynchronizer::RemoveFrameworkControlAt:\r\nIndexisoutofrange!");
m_frameworkControls.RemoveAt(index);
)
III<summary>
///whentheactivecontrolchanges,theclassiteratesthroughthearrayoftheframework
controls
///andcallesSetBuddyControloneachofthecontrols.
///</summary>
III<paramname="buddyM>theactivecontrol</parain>
privatevoidSetBuddies(objectbuddy)
(
try
(
if(buddy==null)
thrownewException(MControlsSynchronizer::SetBuddies:\r\nTarget
BuddyControlisnotinitialized!");
foreach(objectobjinm_frameworkControls)
(
if(objisIToolbarControl)
((IToolbarControl)obj).SetBuddyControl(buddy);
elseif(objisITOCControl)
(
((ITOCControl)obj).SetBuddyControl(buddy);
(
)
)
catch(Exceptionex)
{
thrownew
Exception(string.Format(nControlsSynchronizer::SetBuddies:\r\n{0}",ex.Message));
)
}
#endregion
2、新建Maps類
在同步類中,要用到Maps類,用于管理地圖對(duì)象。與新建同步類ControlsSynchronizer類似,
我們新建一Maps類,其所有代碼如下所示:
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Runtime.InteropServices;
usingESRI.ArcGIS.Carto;
namespace_sdnMap
(
lGuid("f27d8789-fbbc-4801-be78-0e3cd8fff9d5")J
[ClassInterface(ClassInterfaceType.None)]
[ProgId('LsdnMap.Mapsn)]
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>
///<paramnaTne=nIndexn></param>
publicvoidRemoveAt(intIndex)
(
if(Index>m_array.CountIIIndex<0)
thrownewException(nMaps::RemoveAt:\r\nIndexisoutofrange!1*);
m_array.RemoveAt(Index);
)
///<summary>
///ResettheMapsarray
///</summary>
publicvoidReset()
(
m_array.Clear();
)
///<summary>
///GetthenumberofMapsinthecollection
///</summary>
publicintCount
(
get
(
returnm_array.Count;
///<summary>
///ReturntheMapatthegivenindex
///</summary>
///<paramname=HIndex"></param>
///<returnsx/retums>
publicIMapget_Item(intIndex)
(
if(Index>m_array.CountIIIndex<0)
thrownewException(nMaps::get_Item:\r\nIndexisoutofrange!");
returnm_array[Index]asIMap;
)
///<summary>
///RemovetheinstanceofthegivenMap
///</summary>
///<paramname=',Map,'x/param>
publicvoidRemove(IMapMap)
(
m_array.Remove(Map);
)
///<summary>
///CreateanewMap,addittothecollectionandreturnittothecaller
///</summary>
///<returnsx/returns>
publicIMapCreate()
(
IMapnewMap=newMapClass();
m_array.Add(newMap);
returnnewMap;
///<summary>
///AddthegivenMaptothecollection
///</summary>
///<paramname=,'Map,'x/param>
publicvoidAdd(IMapMap)
(
if(M叩==null)
thrownewException("Maps::Add:\r\nNewMapismotinitialized!1');
m_aiTay.Add(Map);
}
#endregion
3、新建打開(kāi)文檔類OpenNewMapDocument
山于從工具欄自帶的打開(kāi)按鈕打開(kāi)地圖文檔的時(shí)候,不會(huì)自動(dòng)進(jìn)行兩種視圖之間的同步,所
以我們要自己派生?個(gè)OpenNewMapDocument類,用于打開(kāi)地圖文檔。
右擊項(xiàng)目名,選擇“添加I類”,再選擇ArcGIS類別中的BaseCommand模板,輸入類名為
OpenNewMapDocument.cs
首先添加引用:
usingSystem.Windows.Forms;
usingESRI.ArcGIS.Carto;
再添加如下成員變量:
privateControlsSynchronizerm_controlsSynchronizer=null;
修改默認(rèn)的構(gòu)造函數(shù)如下所示:
〃添加參數(shù)
publicOpenNewMapDocument(ControlsSynchronizerControlsSynchronizer)
(
//
//TODO:Definevaluesforthepublicproperties
//
〃設(shè)定相關(guān)屬性值
base.m_category="Generic1';//localizabletext
base.m_caption="Open";//localizabletext
base.m_message="ThisshouldworkinArcMap/MapControl/PageLayoutControln;
//localizabletext
base.m_toolTip="Open";//localizabletext
base.m_name=HGeneric_Openn;//uniqueid,non-localizable(e.g.
HMyCategory_MyCommandu)
〃初始化m_controlsSynchronizer
m_controlsSynchronizer=controlsSynchronizer;
try
(
//
//TODO:changebitmapnameifnecessary
//
stringbitmapResourceName=GetType().Name+”.bmp";
base.m_bitmap=newBitmap(GetType(),bitmapResourceName);
i
catch(Exceptionex)
{
System.Diagnostics.Trace.WriteLine(ex.Message,"InvalidBitmap");
再在OnClick函數(shù)中添加如下代碼:
publicoverridevoidOnClick()
(
//TODO:AddOpenNewMapDocument.OnClickimplementation
OpenFileDialogdig=newOpenFileDialogO;
dlg.Filter="MapDocuments(*.mxd)l*.mxd";
dig.Multiselect=false;
dlg.Title=MOpenMapDocument";
if(dlg.ShowDialogO==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();
在添加類時(shí),模板會(huì)自動(dòng)添加一個(gè)名為"OpenNewMapDocument.bmp”的圖標(biāo),你可以自
己修改或者替換為打開(kāi)的文件夾的圖標(biāo)。
4、兩種視圖的同步
在3sdnMap.cs中添加成員變量,即同步類對(duì)象:
privateControlsSynchronizerm_controlsSynchronizer=null;
在Forml_Load函數(shù)中進(jìn)行初始化工作:
〃初始化controlssynchronizationcalss
m_controlsSynchronizer=new
ControlsSynchronizer(m_mapControl,m_pageLayoutControl);
〃把MapControl和PageLayoutControl綁定起來(lái)(兩個(gè)都指向同一個(gè)Map),然后設(shè)置
M叩Control為活動(dòng)的Control
m_controlsSynchronizer.BindControls(true);
〃為了在切換MapControl和PageLayoutControl視圖同步,要添加FrameworkControl
m_controlsSynchronizer.AddFrameworkControl(axToolbarControll.Object);
m_controlsSynchronizer.AddFrameworkControl(this.axTOCControl1.Object);
//添加打開(kāi)命令按鈕到工具條
OpenNewMapDo
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年室內(nèi)植物合作協(xié)議書(shū)
- 科技展覽館光影運(yùn)用藝術(shù)實(shí)例探討
- 2025年放射源及生產(chǎn)裝置合作協(xié)議書(shū)
- 科技引領(lǐng)下的綠色辦公系統(tǒng)發(fā)展趨勢(shì)
- 旅游景區(qū)bot合同范本
- 20以內(nèi)乘法除法口算練習(xí)題庫(kù)1000道可打印
- 基于低共熔溶劑的玉米芯組分清潔高效分離及分級(jí)轉(zhuǎn)化研究
- 國(guó)有科技型企業(yè)股權(quán)激勵(lì)對(duì)創(chuàng)新績(jī)效的影響
- 社交平臺(tái)上的網(wǎng)游運(yùn)營(yíng)策略研究與分析
- 科技發(fā)展對(duì)電信保密工作的影響與對(duì)策
- GB/T 1345-2005水泥細(xì)度檢驗(yàn)方法篩析法
- 技術(shù)-tpu擠出加工注意事項(xiàng)
- 包扎(三角巾)課件
- 外科學(xué)第八版手外傷以及斷指再植
- 高校助學(xué)貸款結(jié)清憑證
- 產(chǎn)業(yè)園規(guī)劃建筑設(shè)計(jì)說(shuō)明
- 內(nèi)蒙體育職院《體育傳播學(xué)》教案第1章 傳播與傳播學(xué)
- 瑪莎拉蒂路演執(zhí)行手冊(cè)升級(jí)版
- 《建筑工程資料管理規(guī)程》DB34T918-2019
- 小班數(shù)學(xué)掛燈籠教案反思
- 美術(shù)課件:水印版畫(huà)
評(píng)論
0/150
提交評(píng)論