carcengine第一講桌面GIS應(yīng)用程序框架的建立_第1頁
carcengine第一講桌面GIS應(yīng)用程序框架的建立_第2頁
carcengine第一講桌面GIS應(yīng)用程序框架的建立_第3頁
carcengine第一講桌面GIS應(yīng)用程序框架的建立_第4頁
carcengine第一講桌面GIS應(yīng)用程序框架的建立_第5頁
已閱讀5頁,還剩60頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

GIS應(yīng)用程序框架。最終成果預(yù)覽如下:1VS2005,選擇“文件|新建|VisualC#Windows應(yīng)用程序模3sdnMap3sdnap.cs3sdnMap2 Strip,DockTop。|StatusStrip,選擇工具箱中的“容器|SplitContainer”容器拖入窗體,并將其屬性中的DockFillPanel1BottomDocktabPage2的NametabPageProperty,Text設(shè)置為屬性。如下所示。TOCControl控件,設(shè)置DockFill。選擇“屬性”選項卡,拖入DataGridViewDockFill。(tabPageMa(tabPageLayoutMapControlDockFill。PageLayoutControlDockFill。LicenseControl控件拖入到窗體的任意地方。3ToolbarControl、TOCControlBuddy設(shè)置為axMapControl14|ItemsAddCommands選項卡中的GenericOpen、SaveAs、Redo、Undo即可將相應(yīng)工具MapNavigation中的導(dǎo)航工具,MapInquiry中的查詢工具,F(xiàn)eatureSelection中的選擇工具,你可以5單的添加及其實|Windows|PropertyGrid(如果沒有,右擊選擇“選擇項”以添加此控1TipsNameName值將是此菜單本講中,添加新建(New、打開(Open、添加數(shù)據(jù)(AddData、保存(Save、另存為(SaveAs、退出(Exit)()內(nèi)為相應(yīng)的Name屬性值。TextOpenO表示此項菜單的快捷鍵是字母O&&OpenShortCutKeysImageImage2首先定義指針(publicpartialclassForm1Form下面即可privateESRI.ArcGIS.Controls.IMapControl3m_mapControl=privateESRI.ArcGIS.Controls.IPageLayoutControl2m_pageLayoutControl=null;privateIMap 若以上指針無效,請?zhí)砑右韵聈singESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.Display;usingESRI.ArcGIS.Geometry;usingESRI.ArcGIS.SystemUI;到“Load”事件并雙擊,添加此事件。Form1_Load//MapControlPageLayoutControl的m_mapControl=(IMapControl3)this.axMapControl1.Object;m_pageLayoutControl(IPageLayoutControl2)this.axPageLayoutControl1.Object;////////////<param///<paramprivatevoidNew_Click(objectsender,EventArgs{//MapControlPageLayoutControl}//////Mxd//////<param///<paramprivatevoidOpen_Click(objectsender,EventArgs{//MapControlPageLayoutControl}////////////<param///<paramprivatevoidAddData_Click(objectsender,EventArgs{intcurrentLayerCount=this.axMapControl1.LayerCount;ICommandpCommand=new }////////////<param///<paramprivatevoidSave_Click(objectsender,EventArgs{// {//IMapmapDoc=newMap////PageLayoutPageLayout//保存地圖文檔mapDoc.Save(mapDoc.UsesRelativePaths,false);}}////////////<param///<paramprivatevoidSaveAs_Click(objectsender,EventArgs{//ICommandcommand=newCo }////////////<param///<paramprivatevoidExit_Click(objectsender,EventArgs{}3按F5編譯運行程序。也許你會發(fā)現(xiàn),菜單命令的實現(xiàn)方式都是類型的。沒錯,在AE9.2中,內(nèi)置了許多常用的 ,如 等等,這些內(nèi)置對象在ESRI.ArcGIS.Controls命名空間中你可以對象瀏覽器中查看而且這些內(nèi)置對象的調(diào)用方式都類似,//ICommandcommand=new ////MapControlPageLayoutControl同ArcMapMapViewLayoutView兩種視圖的切換,而且二者之間的數(shù)據(jù)ObjectCopy對象進行數(shù)據(jù)硬拷貝,而比較簡單的方在解決方案面板中右擊項目名,選擇“添加|VisualC#ConrolsSyncronize.csusingusingSystem.Drawing;usingSystem.Collections; usingSystem.Windows.Forms;usingSystem.IO;usingSystem.Runtime.InteropServices;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.SystemUI;namespace_sdnMap{//////ThisclassisusedtosynchronizeagvenPageLayoutControlanda///Wheninitialized,theusermustpassthereferenceofthesecontroltotheclass,///thecontroltogetherbycalling'BindControls'whichinturnsetsajoinedMap///bybothcontrol;andsetallthebuddycontrolsjoinedbetweenthesetwo///WhenalternatingbetweentheMapControlandPageLayoutControl,youshouldactivatethevisible///anddeactivatetheotherbycalling///Thiscalssislimitedtoasituationwherethecontrolsarenotsimultaneously///publicclass{#regionclassprivateIMapControl3m_mapControl=privateIPageLayoutControl2m_pageLayoutControl=null;privateIToolm_mapActiveTool=null;privateIToolm_pageLayoutActiveTool=null;privateboolm_IsMapCtrlactive=true;privateArrayListm_frameworkControls=null;#region/////////public{//ArrayListm_frameworkControlsnewArrayList();}////////////<param///<param :{m_mapControlmapControl;m_pageLayoutControl=}#regionproperties/////////publicIMapControl3{get{returnm_mapControl;}set{m_mapControl=value;}}/////////publicIPageLayoutControl2{get{returnm_pageLayoutControl;}set{m_pageLayoutControl=value;}}//////ActiveView///publicstring{{

if(m_IsMapCtrlactive)return"MapControl";}}

return/////////publicobject{{

if(m_mapControl==null||m_pageLayoutControl== MapControlorPageLayoutControlarenotifreturn}}

return#regionMethods//////MapControlthe///publicvoid{{if(m_pageLayoutControl==null||m_mapControl==thrownewException("ControlsSynchronizer::ActivateMap:\r\nEitherorPageLayoutControlarenot //MapControl//將之前MapControl最后使用的tool,作為活動的tool,賦給MapControl}

if(m_mapActiveTool!=null)m_mapControl.CurrentTool=m_mapActiveTool;m_IsMapCtrlactive=true;//frameworkcontrols,Buddycontrolcatch(Exception{ }}//////PagleLayoutControl///publicvoid{{if(m_pageLayoutControl==null||m_mapControl== MapControlorPageLayoutControlarenot//MapControl//PageLayoutControl將之前PageLayoutControl最后使用的tool,作為活動的toolPageLayoutControl m_IsMapCtrlactive=//frameworkcontrols,BuddycontrolPageLayoutControl}catch(Exception{ }}//////給予一個地圖,PageLayoutControlMapControlfocus//////<paramname="newMap"></param>publicvoidReplaceMap(IMapnewMap){if(newMap== replacementisnotinitialized!");if(m_pageLayoutControl==null||m_mapControl==thrownewException("ControlsSynchronizer::ReplaceMap:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");//createanewinstanceofIMapscollectionwhichisneededbythe//PageLayout需要用到的,新的IMapscollectionIMapsmaps=new//addthenewmaptotheMapsboolbIsMapActive=//callreplacemaponthePageLayoutinordertoreplacethefocus//wemustcallActivatePageLayout,sinceitisthecontrolwecall//PageLayout的replacemapfocus//須調(diào)用ActivatePageLayout,因為它是那個我們可以調(diào)用"ReplaceMaps"

//assignthenewmaptothe//MapControlm_mapControl.MapnewMap;//resettheactive//activetoolsm_pageLayoutActiveToolnull;m_mapActiveTool=null;//makesurethatthelastactivecontrolis//controlif{}{}}

//////bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocus///MapMapControlPageLayoutControl//////<param///<param///<paramname="activateMapFirst">trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,true</param>publicvoidBindControls(IMapControl3mapControl,IPageLayoutControl2pageLayoutControl,boolactivateMapFirst){if(mapControl==null||pageLayoutControl==thrownewException("ControlsSynchronizer::BindControls:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");m_mapControl=MapControl;m_pageLayoutControl=pageLayoutControl;}//////bindtheMapControlandPageLayoutControltogetherbyassigninganewjointfocus///MapMapControlPageLayoutControl//////<paramname="activateMapFirst">trueiftheMapControlsupposedtobeactivatedfirst,如果MapControl被首先激活,true</param>publicvoidBindControls(bool{if(m_pageLayoutControl==null||m_mapControl==thrownewException("ControlsSynchronizer::BindControls:\r\nEitherMapControlorPageLayoutControlarenotinitialized!");//createanewinstanceof//IMapIMapnewMap=newMapClass();newMap.Name="Map";//createanewinstanceofIMapscollectionwhichisneededbytheIMapsmaps=new//addthenewMapinstancetotheMaps//MapMapscollection//callreplacemaponthePageLayoutinordertoreplacethefocus//PageLayout的replacemapfocusmap//assignthenewmaptothe//mapMapControlm_mapControl.Map//resettheactive//activetoolsm_pageLayoutActiveToolnull;m_mapActiveTool=null;//makesurethatthelastactivecontrolis//controlif(activateMapFirst)}

//////bypassingtheapplication'stoolbarsandTOCtothesynchronizationclass,itsavesyou///managementofthebuddycontroleachtimetheactivecontrolchanges.Thismethodadsthe///controltoanarray;oncetheactivecontrolchanges,theclassiteratesthroughthearray///callesSetBuddyControloneachofthestoredframework//////<param{if(control==isnot}//////Removeaframeworkcontrolfromthemanagedlistof//////<parampublicvoidRemoveFrameworkControl(object{if(control==beremovedisnot}//////Removeaframeworkcontrolfromthemanagedlistofcontrolsbyspecifyingitsindexinthe//////<parampublicvoidRemoveFrameworkControlAt(int{if(m_frameworkControls.Count<outof}//////whentheactivecontrolchanges,theclassiteratesthroughthearrayoftheframework andcallesSetBuddyControloneachofthe//////<paramname="buddy">theactivecontrol</param>privatevoidSetBuddies(objectbuddy){{isnot

if(buddy==foreach(objectobjin{if(objis{}elseif(objis{}}}catch(Exception{ }}}}2MapsMapsControlsSynchronizer類似,我們Maps類,其所有代碼如下所示:usingusingusingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingESRI.ArcGIS.Carto;namespace{publicclassMaps:IMaps,{//classmember-usinginternallyanArrayListtomanagetheMapscollectionprivateArrayListm_array=null;#regionclassconstructorpublicMaps(){m_array=new}#regionIDisposable//////Disposethe///</summary>publicvoidDispose(){if(m_array!={m_array=null;}}#regionIMaps//////RemovetheMapatthegiven//////<paramname="Index"></param>publicvoidRemoveAt(intIndex){if(Index>m_array.Count||Index<thrownewException("Maps::RemoveAt:\r\nIndexisoutofrange!");}//////ResettheMaps///</summary>publicvoidReset(){}//////GetthenumberofMapsinthe///</summary>publicint{{}}

return//////ReturntheMapatthegiven//////<param///publicIMapget_Item(int{if(Index>m_array.Count||Index<thrownewException("Maps::get_Item:\r\nIndexisoutofrange!");returnm_array[Index]asIMap;}//////Removetheinstanceofthegiven//////<paramname="Map"></param>publicvoidRemove(IMapMap){}//////CreateanewMap,addittothecollectionandreturnittothe//////<returns></returns>publicIMapCreate(){IMapnewMap=newreturnnewMap;}//////AddthegivenMaptothe//////<paramname="Map"></param>publicvoidAdd(IMapMap){if(Map==thrownewException("Maps::Add:\r\nNewMapismotinitialized!");}}}3、新建打開文檔類由于從自帶的打開按鈕打開地圖文檔的時候,不會自動進行兩種視圖之間的同步,所以我們要自己派生一個OpenNewMap 右擊項目名,選擇“添加|類”,再選擇ArcGIS類別中的 mand模板,輸入類名為 .cs首先添加usingSystem.Windows.Forms;usingESRI.ArcGIS.Carto;privateControlsSynchronizerm_controlsSynchronizer={//TODO:Definevaluesforthepublicbase.m_category="Generic";//localizabletextbase.m_caption="Open"; //localizabletextbase.m_message="Thisshouldworkin//localizablebase.m_toolTip= //localizable m_controlsSynchronizer=controlsSynchronizer;{//TODO:changebitmapnameifstringbitmapResourceName=GetType().Name+".bmp";base.m_bitmap=newBitmap(GetType(),bitmapResourceName);}catch(Exception{ ine(ex.Message,"Invalid}}publicoverridevoid{//TODO:AddOpenNewMap .OnClickimplementationOpenFileDialogdlg=newOpenFileDialog();dlg.Filter="Map s(*.mxd)|*.mxd";dlg.Multiselect=false;dlg.Title="Open if(dlg.ShowDialog()=={ ame=IMapmapDoc=newMapif ame)&& { ame,string.Empty);IMapmap=mapDoc.get_Map(0);}}} privateControlsSynchronizerm_controlsSynchronizer=//controlssynchronizationcalssm_controlsSynchronizer=newControlsSynchronizer(m_mapControlm_pageLayoutControl);//MapControlPageLayoutControl綁定起來(Map),//MapControlPageLayoutControlFrameworkControl// openMapDoc=newOpenNewMap ToolbarControl的屬性中將其刪除。////////////<param///<paramprivatevoidNew_Click(objectsender,EventArgs{DialogResultres=MessageBox.Show("?","",MessageBoxButtons.YesNo,MessageBoxIcon.Question);if(res=={ICommandcommand=newCo if(m_mapControl!=null)}IMapmap=newMapClass();map.Name="Map"; Filename=}//////Mxd//////<param///<paramprivatevoidOpen_Click(objectsender,EventArgs{if(this.axMapControl1.LayerCount>{DialogResultresultMessageBox.Show("是否保存當(dāng)前地圖?警告",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);if(result==DialogResult.Cancel)} openMapDoc=new }在添加數(shù)據(jù)AddDataAddData_ClickIMappMap=this.axMapControl1.Map;////////////<param///<paramprivatevoidSaveAs_Click(objectsender,EventArgs{{if(MessageBox.Show("另存為地圖文檔將丟失制版視圖的設(shè)置\r\n您要繼續(xù)嗎提示MessageBoxButtons.YesNo,MessageBoxIcon.Question)DialogResult.No)}ICommandcommand=newCo }tabControl2控件,再選擇////////////<param///<param{if(this.tabControl2.SelectedIndex=={}5

}{}

//MapControl//PageLayoutControlF5MapControl和PageLayoutControl第四講狀態(tài)欄信息的添加與實在上一講中,我們完成了MapControl和PageLayoutControl兩種視圖的同步工作,本講完成GIS應(yīng)用程序一般也在狀態(tài)1在設(shè)計視圖中,點擊窗體中的狀態(tài)欄,在其屬性面板中找到“Items”項,單擊其右邊的按鈕,在StatusLabelStatusLabel,依次修改屬性參數(shù)NameTextSpring1234Tips(BlankSpring屬性表示可以按狀態(tài)欄剩余空間自動伸縮。所以加入Blank項目,只是為了占個位子,以達(dá)到ScaleLabel和Coordina abel項目右對齊而MessageLabel項目左對齊的目的。2首先添加axToolbarControl1的OnMouseMove事件(相信大家看了以上的,已經(jīng)知道怎么添加事件了吧,還不知道的建議再溫習(xí)下前幾講的內(nèi)容)。在其事件響應(yīng)函數(shù)代碼如下: IToolbarControlEvents_OnMouseMoveEvente){//intindex=axToolbarControl1.HitTest(e.x,e.y,false);if(index!=-1){//IToolbarItemtoolbarItem=// }{MessageLabel.Text="}}3axMapControl1OnMouseMove{//ScaleLabel.Text="1}4顯示當(dāng)前坐標(biāo)也 axMapControl1 OnMouseMove事件中響應(yīng),故只要// abel.Text="X="+e.mapX.ToString()+"Y="+e.mapY.ToString()+""+F5編譯運行,可以看到,我們的程序已經(jīng)能夠正常工作了。但是細(xì)心的你可能會發(fā)現(xiàn),當(dāng)前坐esriUnknownUnitsesriMeters”之類,即系統(tǒng)在正常單位的前面加上了“esrisMapUnitsprivatestringForm1_LoadsMapUnits=添加axMapControl1控件的OnMapReplaced事件,在事件響應(yīng)函數(shù)中進行坐標(biāo)單位替換,代碼如privatevoidaxMapControl1_OnMapReplaced(objectsender,IMapControlEvents2_OnMapReplacedEvent{esriUnitsmapUnits=axMapControl1.MapUnits;switch(mapUnits){caseesriUnits.esriCentimeters:sMapUnits="Centimeters";caseesriUnits.esriDecimalDegrees:sMapUnits="DecimalDegrees";caseesriUnits.esriDecimeters:sMapUnits="Decimeters";caseesriUnits.esriFeet:sMapUnits="Feet";caseesriUnits.esriInches:sMapUnits="Inches";caseesriUnits.esriKilometers:sMapUnits="Kilometers";caseesriUnits.esriMeters:sMapUnits="Meters";caseesriUnits.esri:sMapUnits="";caseesriUnits.esriMillimeters:sMapUnits="Millimeters";caseesriUnits.esriNautical sMapUnits="Nautical caseesriUnits.esriPoints:sMapUnits="Points";caseesriUnits.esriUnknownUnits:sMapUnits="Unknown";sMapUnits="Yards";}}5F5StatusStripAE中當(dāng)所用工具信息、當(dāng)前比例尺和當(dāng)前坐眼的MapControlMapControlMapControl互動。本講最終效果1由于本在第一講中沒有預(yù)先考慮到鷹眼所放的位置,故我們要先稍微調(diào)整一下程序框架,并添加一個MapControl用于顯示鷹眼。(1)tabControl1Dock屬性設(shè)為None,并用鼠標(biāo)拖拽將其縮小。把工具箱中的SplitContainer控件拖到窗體的左窗格,即放在tabControl1控件的旁邊。并將其Orientation屬性設(shè)置為Horizontal。(2tabControl1Ctrl+XSplitContainer2Panel1中,如圖2所示。操作完成后效果如圖3所示。圖圖(3)SplitContainer2控件(SplitContainer2DockFilltabControl1DockFill(4)MapControlSplitContainer2Panel2,作為鷹眼控件。最終效果如圖4所示。圖(1)MapaxMapControl1_OnMapReplaced事件響{////this.axMapControl2.Map=new//for(inti=1;i<=this.axMapControl1.LayerCount;{}//MapControlthis.axMapControl2.Extent=//}(2)MapControl1OnExtentUpdatedMap控件的顯示范圍改變時 IMapControlEvents2_OnExtentUpdatedEvente){//IEnvelopepEnv=IGraphicsContainerpGra=axMapControl2.MapasIGraphicsContainer;IActiveViewpAv=pGraasIActiveView;//axMapControl2IRectangleElementpRectangleEle=newRectangleElementClass();IElementpEle=pRectangleEleasIElement;pEle.Geometry=//IRgbColorpColor=newRgbColorClass();pColor.Red=255;pColor.Green=pColor.Blue=//ILineSymbolpOutline=newSimpleLineSymbolClass();pOutline.Width=2;pOutline.Color=//pColor=newRgbColorClass();pColor.Red=255;pColor.Green=pColor.Blue=//IFillSymbolpFillSymbol=newSimpleFillSymbolClass();pFillSymbol.Color=pColor;pFillSymbol.Outline=IFillShapeElementpFillShapeEle=pEleasIFillShapeElement;pFillShapeEle.Symbol=pFillSymbol;pGra.AddElement((IElement)pFillShapeEle,0);//}(3)MapMapControl2OnMouseDown{if(this.axMapControl2.Map.LayerCount!={//if(e.button=={IPointpPoint=newPointClass();pPoint.PutCoords(e.mapX,e.mapY);IEnvelopepEnvelope=this.axMapControl1.Extent;this.axMapControl1.Extent=pEnvelope;}//elseif(e.button=={IEnvelopepEnvelop=this.axMapControl2.TrackRectangle();this.axMapControl1.Extent=pEnvelop;}}}MapControl2OnMouseMove事件,主要實現(xiàn)按下鼠標(biāo)左鍵的時候移動矩形框,同{//if(e.button!=1)IPointpPoint=newPointClass();pPoint.PutCoords(e.mapX,e.mapY);}1F5鍵菜單添加與實privateITOCControl2m_tocControl=//TOCControlMapprivate Map=private Layer= Map=newToolbar Layer=newToolbar 單容器才能工作一般情況下啟動程序就要完成菜單項的添加故此工作在Form1_Load函數(shù)完成。義的。AE內(nèi)置了許多可以直接調(diào)用的常用命令和工具,如 ESRI.ArcGIS.Controls命名空間中,大家可以對象瀏覽器中查看。當(dāng)然,這里也可以直接調(diào)用AE內(nèi)置的菜單,如ControlsFeatureSelection。另外,本講也實現(xiàn)三自定義命令,以做示范。它們分別,LayerVisibility代碼:usingESRI.ArcGIS.ADF.BaseClasses;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.SystemUI;namespace_sdnMap{/////////publicsealedclassLayerVisibility mand,{privateIHookHelperm_hookHelper=newHookHelperClass();privatelongm_subType;public{}publicoverridevoid{for(inti=0;i<=m_hookHelper.FocusMap.LayerCount-1;{

if(m_subType==1)m_hookHelper.FocusMap.get_Layer(i).Visible=if(m_subType==2)m_hookHelper.FocusMap.get_Layer(i).Visible}}publicoverridevoidOnCreate(object{}publicint{return}publicvoidSetSubType(int{m_subType=}publicoverridestring{{if(m_subType==1)return"TurnAllLayersOn"; return"TurnAllLayersOff";}}publicoverridebool{{boolenabled=false;inti;if(m_subType==1){{{}}{

enabled=true;}{{enabled=true;}}}return}}}}usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;namespace_sdnMap{/////////publicsealedclassRemoveLayer {privateIMapControl3m_mapControl;publicRemoveLayer(){base.m_caption="Remove}publicoverridevoid{ILayerlayer=(ILayer)m_mapControl.CustomProperty;}publicoverridevoidOnCreate(object{m_mapControl=}}}放大至整個圖層類usingESRI.ArcGIS.Carto;usingESRI.ArcGIS.Controls;namespace_sdnMap{/////////publicsealedclassZoomToLayer {privateIMapControl3public{base.m_caption="ZoomTo}publicoverridevoid{ILayerlayer=(ILayer)m_mapControl.CustomProperty;m_mapControl.Extent=layer.AreaOfInterest;}publicoverridevoidOnCreate(object{m_mapControl=}}}Form1_Load函數(shù)中進行菜單項的添加,代碼如下: ",4, ",5, Layer=new 顧名思義,右鍵菜單是在鼠標(biāo)右鍵按下的時候彈出,所以我們要添加TOCControl1控件的 ITOCControlEvents_OnMouseDownEvente){if(e.button!=2)return;esriTOCControlItemitem=esriTOCControlItem.esriTOCControlItemNone;IBasicMapmap=null;ILayerlayer=null;objectother=null;objectindex=m_tocControl.HitTest(e.x,e.y,refitem,refmap,reflayer,refother,refif(item==esriTOCControlItem.esriTOCControlItemMap)m_tocControl.SelectItem(map,null);//CustomPropertylayer用于自定義的Layer命令)m_mapControl.CustomProperty=layer;if(item== Map.Popup(e.x,e.y,m_tocControl.hWnd);if(item==esriTOCControlItem.esriTOCControlItemLayer) }////////////<param///<param{if(e.button=={ }}F5編譯運行程序,你會發(fā)現(xiàn),原來右鍵菜單實現(xiàn)起來是這么的簡單??!到第六講為止已經(jīng)發(fā)現(xiàn)的Bug及解決方法如下:m_tocControlm_tocControlaxTOCControl1控件綁定,導(dǎo)致調(diào)用m_Map.Popup(e.x,e.y,m_tocControl.hWnd);時m_tocControl.hWnd為NULL,故無Form1_Load()函數(shù)中,添加如下代碼:m_tocControl(ITOCControl2)this.axTOCControl1.Object;Coordinaabel.Text="當(dāng)前坐標(biāo)X="+e.mapX.ToString()+"Y="+e.mapY.ToString()+""+2、第四講,固定狀態(tài)欄中的比例尺和當(dāng)前坐標(biāo)項目的寬度以防止閃爍。autoSizeFalseSize屬性里設(shè)置寬度。150400比較合適。第七講圖層符號選擇器的實現(xiàn)在上一講中,我們實現(xiàn)了右鍵菜單(Context)的添加與實現(xiàn),在最后我預(yù)留給下一講的問題是TOCControlTOCControl的屬性頁中,勾選“EnableLayerDragandDrop”即可。ArcMapSymbolSelector的類似。本講較前幾講1通過對《ArcGISEngine+C#實例開發(fā)》前六講的學(xué)習(xí),我已經(jīng)假定你已經(jīng)基本熟悉C#語言和VS2005的操作故在下面的中我備說明每一步驟的具體操作方法而只是說明操作步驟,直接調(diào)用ArcMap添加ESRI.ArcGIS.DisplayUI的。分別在解決方案管理器和代碼中添加。TOCControl的Double_ClickTOCControl的Double_Click因為種方法不是本講的重點,故不對代碼進行分析,有的讀者請自行理解或結(jié)合后面的內(nèi)容理{esriTOCControlItemtoccItem=esriTOCControlItem.esriTOCControlItemNone;ILayeriLayer=null;IBasicMapiBasicMap=null;objectunk=null;objectdata=null;if(e.button==1){axTOCControl1.HitTest(e.x,e.y,reftoccItem,refiBasicMap,refiLayer,refunk,refdata);System.Drawing.Pointpos=newSystem.Drawing.Point(e.x,if(toccItem=={ESRI.ArcGIS.Carto.ILegendClasspLC=newLegendClassClass();ESRI.ArcGIS.Carto.ILegendGrouppLG=newLegendGroupClass();if(unkisILegendGroup){pLG=}pLC=pLG.get_Class((int)data);ISymbolpSym;pSym=pLC.Symbol;ESRI.ArcGIS.DisplayUI.ISymbolSelectorpSS=newboolbOK=false;bOK=pSS.SelectSymbol(0);if(bOK){pLC.Symbol=}}}}AE9.2SymbologyControl控件,極大的方便了圖層符號選擇器的制作。本講實現(xiàn)的符號選擇符(*.ServerStyle,加載的符號。新建Winodws窗體,命名為SymbolSelectorFrm,修改窗體的TextContextStrip控件??丶季秩缦滤荆篈rcGISEngine的ESRI.ArcGIS.GeodatabaseSymbolSelectorFrm.cs文件中添加如下代碼usingusingESRI.ArcGIS.Display;usingESRI.ArcGIS.esriSystem;usingESRI.ArcGIS.SystemUI;usingESRI.ArcGIS.Controls;usingESRI.ArcGIS.Geodatabase;privateIStyleGalleryItempStyleGalleryItem;privateILegendClasspLegendClass;privateILayerpLayer;publicISymbolpSymbol;publicImage//////構(gòu)造函數(shù),///<paramname="tempLegendClass">TOC圖例<paramname="tempLayer">圖層{this.pLegendClass=tempLegendClass;this.pLayer=tempLayer;}添加SymbolControl的SymbologyStyleClass設(shè)置函數(shù)SetFeatureClassStyle()//////初始化SymbologyControl的StyleClass,圖層如果已有符號,SymbologyControl中的//////<param{ if(this.pLegendClass!={IStyleGalleryItemcurrentStyleGalleryItemnewServerStyleGalleryItem();currentStyleGalleryItem.Name="當(dāng)前符號";currentStyleGalleryItem.Item=pLegendClass.Symbol;this.pStyleGalleryItem=currentStyleGalleryItem;}}//////從表中取得指定軟件的路//////<param///privatestringReadRegistry(string{//Openthesubkeyfor.Win32.RegistryKeyrk= if(rk==null)return"";//Getthedatafromaspecifiediteminthekey.return(string)rk.GetValue("InstallDir");}privatevoidSymbolSelectorFrm_Load(objectsender,EventArgs{//ArcGISstringsInstall=//ESRI.ServerStyleSymbologyControlthis.axSymbologyControl.LoadStyleFile(sInstallStyles\\ESRI.ServerStyle");//確定圖層的類型(點線面),SymbologyControlStyleClass,設(shè)置好各控件的可見性(visible)IGeoFeatureLayerpGeoFeatureLayer=(IGeoFeatureLayer)pLayer;switch{caseESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:this.lblAngle.Visible=true;this.nudAngle.Visible=true;this.lblSize.Visible=true;this.nudSize.Visible=true;this.lblWidth.Visible=false;this.nudWidth.Visible=false;this.lblOutlineColor.Visible=false;caseESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:this.lblAngle.Visible=false;this.nudAngle.Visible=false;this.lblSize.Visible=false;this.nudSize.Visible=false;this.lblWidth.Visible=true;this.nudWidth.Visible=true;this.lblOutlineColor.Visible=false;caseESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:this.lblAngle.Visible=false;this.nudAngle.Visible=false;this.lblSize.Visible=false;this.nudSize.Visible=false;this.lblWidth.Visible=true;this.nudWidth.Visible=true;this.lblOutlineColor.Visible=true;this.btnOutlineColor.Visible=true;caseESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultiPatch:this.lblAngle.Visible=false;this.nudAngle.Visible=false;this.lblSize.Visible=false;this.nudSize.Visible=false;this.lblWidth.Visible=true;this.nudWidth.Visible=true;this.lblOutlineColor.Visible=true;this.btnOutlineColor.Visible=true;}}////////////<param///<paramprivatevoidbtnOK_Click(objectsender,EventArgs{this.pSymbol=this.pSymbolImage=}////////////<param///<paramprivatevoidbtnCancel_Click(objectsender,EventArgs{}////////////<param///<param ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnDoubleClickEvente){}//////picturebox///privatevoid{ tem,this.ptbPreview.Width,this.ptbPreview.Height); }SymbologyControl的樣式改變時,需要重新設(shè)置符號參數(shù)調(diào)整控件的可視性,故要SymbologyControlOnStyleClassChangedLoad事件類似,如下://////當(dāng)樣式(Style)//////<param///<param ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnStyleClassChangedEvente){switch{caseesriSymbologyStyleClass.esriStyleClassMarkerSymbols:this.lblAngle.Visible=true;this.nudAngle.Visible=true;this.lblSize.Visible=true;this.nudSize.Visible=true;this.lblWidth.Visible=false;this.nudWidth.Visible=false;this.lblOutlineColor.Visible=false;this.btnOutlineColor.Visible=false;caseesriSymbologyStyleClass.esriStyleClassLineSymbols:this.lblAngle.Visible=false;this.nudAngle.Visible=false;this.lblSize.Visible=false;this.nudSize.Visible=false;this.lblWidth.Visible=true;this.nudWidth.Visible=true;this.lblOutlineColor.Visible=false;this.btnOutlineColor.Visible=false;caseesriSymbologyStyleClass.esriStyleClassFillSymbols:this.lblAngle.Visible=false;this.nudAngle.Visible=false;this.lblSize.Visible=false;this.nudSize.Visible=false;this.lblWidth.Visible=true;this.nudWidth.Visible=true;this.lblOutlineColor.Visible=true;this.btnOutlineColor.Visible=true;}}通過以上操作,本符號選擇器雛形已經(jīng)完成,我們可以3sdnMap主窗體中調(diào)用并進試。如果您axTOCControl1控件的OnDoubleClick事件,再添加如下事件響應(yīng)函數(shù)代碼://////TOCControl//////<param///<param{esriTOCControlItemitemType=esriTOCControlItem.esriTOCControlItemNone;IBasicMapbasicMap=null;ILayerlayer=null;objectunk=null;objectdata=null;axTOCControl1.HitTest(e.x,e.y,refitemType,refbasicMap,reflayer,refunk,refdata);if(e.button==1){{

ILegendClasspLegendClass=SymbolSelectorFrmSymbolSelectorFrm=new{pLegendClass.Symbol=//Map控件和圖層控件}}}SymbologyControlOnItemSelected事件,此事件在鼠標(biāo)選中符號時觸發(fā),此時////////////<param///<param ESRI.ArcGIS.Controls.ISymbologyControlEvents_OnItemSelectedEvente){pStyleGalleryItem=(IStyleGalleryItem)e.styleGalleryItem;Colorcolor;switch{casecolor=this.ConvertIRgbColorToColor(((IMarkerSymbol)pStyleGalleryItem.Item).Color

this.nudSize.Value=(decimal)((IMarkerSymbol)this.pStyleGalleryItem.Item).Size;casecolor=this.ConvertIRgbColorToColor(((ILineSymbol)pStyleGalleryItem.Item).Color

case color=Color.Black;}this.btnColor.BackColorcolor;}nudSizeValueChanged事件,即在控件的值改變時響應(yīng)此事件,然后重新設(shè)置點符號的//////調(diào)整符號大小-//////<param///<param{}//////調(diào)整符號角度-//////<param///<param{((IMarkerSymbol)this.pStyleGalleryItem.Item).Angle=(double)this.nudAngle.Value;}//////調(diào)整符號寬度-//////<param///<param{switch{caseesriSymbologyStyleClass.esriStyleClassLineSymbols: caseILineSymbolpLineSymbol=((IFillSymbol)this.pStyleGalleryItem.Item).Outline;pLineSymbol.Width=Convert.ToDouble(this.nudWidth.Value);((IFillSymbol)this.pStyleGalleryItem.Item).Outline=pLineSymbol;}}在調(diào)整顏色參數(shù)之前,須完成以上兩種不同顏色表示方式的轉(zhuǎn)換。關(guān)于這兩種顏色結(jié)構(gòu)的具ArcGISEngineIRgbColor接口轉(zhuǎn)換至.NETColor//////ArcGISEngineIRgbColor接口轉(zhuǎn)換至.NETColor//////<paramreturns>.NETSystem.Drawing.ColorARGB顏色</returns>publicColorConvertIRgbColorToColor(IRgbColorpRgbColor){return}.NETColorArcGISEngine中的IColor//////將.NETColorArcGISEngineIColor///<paramname="color">.NETSystem.Drawing.ColorARGB顏色///{IColorpColor=newpColor.RGB=color.B*65536+color.G*256+color.R;returnpColor;}btnColor按鈕,添加如下代碼:////////////<param///<paramprivatevoidbtnColor_Click(objectsender,EventArgs{if(this.colorDialog.ShowDialog()=={this.btnColor.BackColor=this.colorDialog.Color;switch{caseesriSymbologyStyleClass.esriStyleClassMarkerSymbols: caseesriSymbologyStyleClass.esriStyleClassLineSymbols: caseesriSymbologyStyleClass.esriStyleClassFillSymbols: }}}同上一步類似,雙擊btnOutlineColor////////////<param///<paramprivatevoidbtnOutlineColor_Click(objectsender,EventArgs{if(this.colorDialog.ShowDialog()=={ILineSymbolpLineSymbol=pLineSymbol.Color=((IFillSymbol)this.pStyleGalleryItem.Item).OutlinepLineSymbol;}}已經(jīng)注意到,ArcMapSymbolSelector有一個“MoreSymbols”按鈕,可以加載其它的符號和ServerStyle文件。3sdnMap

功能是:單擊“符號”彈出菜單(Context菜單中列出了ArcGIS自帶的其它符號,勾選boolcontextMoreSymbolInitiated=在此事件響應(yīng)函數(shù)中,我們要完成ServerStyle文件的,將其文件名作為菜單項名稱生成菜單并//////“符號”按下時觸發(fā)的事//////<param///<paramprivatevoidbtnMoreSymbols_Click(objectsender,EventArgs{if MoreSymbolInitiated=={

stringsInstall=ReadRegistry("SOFTWARE\\ESRI\\CoreRuntime");stringpath= bine(sInstall,"Styles"); Item=new Item[styleNames.Lengthfor(inti=0;i<styleNames.Length;{symbolContextItem[i]=newToolStrip symbolContextItem[i].CheckOnClick=true; if(symbolContextItem[i].Text=="ESRI"){ Item[i].Checked=}symbolContextItem[i].Name=}symbolContextItem[styleNames.Length]=newToolStripItem();symbolContextItem[styleNames.Length].Text="添加符號";symbolContextItem[styleNames.Length].Name="AddMoreSymbol"; this.contextMoreSymbolInitiated=true;} }添加

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論