GIS二次開發(fā)第3課_第1頁
GIS二次開發(fā)第3課_第2頁
GIS二次開發(fā)第3課_第3頁
GIS二次開發(fā)第3課_第4頁
GIS二次開發(fā)第3課_第5頁
已閱讀5頁,還剩112頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

GIS二次開發(fā)主講:張云鵬第三課使用ArcObjects控件編程MapControl控件PageLayoutControl控件TOCControl控件ToolbarControl及相關(guān)對象ControlCommandsMapControl控件MapControl對應(yīng)于ArcMap中的數(shù)據(jù)視圖,它封裝了Map對象,并提供了額外的屬性、方法、事件用于:管理控件的外觀、顯示屬性和地圖屬性;添加并管理控件中的數(shù)據(jù)層(datalayers);裝載Map文檔(mxd)到控件中;從其它應(yīng)用程序拖放數(shù)據(jù)到控件中;trackingshapesanddrawingtothedisplay.在可視化環(huán)境中,可以通過控件的“屬性”頁設(shè)置控件的相關(guān)屬性,也可以通過編程來設(shè)置。MapControl實現(xiàn)的主要接口有:IMapControlDefaultIMapControl2

IMapControl3IMapControl4IMapControlEvents2事件接口ITOCBuddyIToolbarBuddyMapControl控件主要接口IMapControlDefault接口IMapControlDefault接口是地圖控件缺省接口,多數(shù)開發(fā)環(huán)境自動使用這個接口定義的屬性、方法。由于MapControl是一個自動化控件,當(dāng)它被放到一個容器--如窗體上后,它會自動產(chǎn)生一個被稱為axMapControl1的對象,這個對象可以直接使用缺省接口定義的屬性和方法。這個接口也代表了控件最新版本的接口,MapControl當(dāng)前最新版本接口為IMapControl4.當(dāng)需要使用這個接口的時候,可以使用下面的代碼:

IMapControlDefaultpMapControl=axMapControl1.ObjectasIMapControlDefault;IMapControl2接口這個接口是任何一個與MapControl相關(guān)的任務(wù)的出發(fā)點,如設(shè)置控件外觀,設(shè)置Map對象或控件的顯示屬性,添加或者管理數(shù)據(jù)圖層、地圖文檔,在控件上繪制圖形和返回Geometry等。IMapControl3與IMapControl4IMapControl3接口繼承IMapControl2,并增加了以下8個屬性和一個方法:CustomProperty:使用該屬性關(guān)聯(lián)有用信息DocumentFilename:返回MapControl裝入的地圖文檔的文件名DocumentMap:返回MapControl最后裝入的地圖(Map)名稱KeyIntercept:返回或設(shè)置MapControl截取鍵盤按鍵信息Object:返回潛在的MapControl控件WhenqueryinginterfacetoIMapControl2inVisualBasic.NETorVisualC#.NETtheObjectpropertyorcontainerspecificcodemustbeused.Thisisbecause.NETcontainstherealcontrolinsideawrapperobjectknownasanhost.ShowMapTips:確定是否顯示地圖的MapTipsTipDelay:設(shè)置MapTips的延遲時間TipStyle:設(shè)置MapTips的顯示樣式SuppressResizeDrawing():在控件尺寸發(fā)生變化過程中,阻止數(shù)據(jù)實時重繪與IMapControl3相比,IMapControl4多了以下兩個可讀寫屬性:publicboolAutoKeyboardScrolling{get;set;}:Indicateswhetherkeyboardscrollingisenabled.publicboolAutoMouseWheel{get;set;}:Indicateswhetherthemousewheelisenabled.引用控件本身當(dāng)使用IMapControl2接口時:IMapControl2pMapControl=axMapControl1.ObjectasIMapControl2;或IMapControl2pMapControl=axMapControl1.GetOcx()asIMapControl2;當(dāng)使用IMapControl3接口時:IMapControl3pMapControl=axMapControl1.ObjectasIMapControl3;或IMapControl3pMapControl=axMapControl1.GetOcx()asIMapControl3;當(dāng)使用IMapControl4接口時:IMapControl4pMapControl;pMapControl=axMapControl1.ObjectasIMapControl4;或pMapControl=axMapControl1.GetOcx()asIMapControl4;使用箭頭鍵、鼠標(biāo)滾輪實現(xiàn)地圖導(dǎo)航使用箭頭鍵平移(Panning)地圖axMapControl1.KeyIntercept=(int)esriKeyIntercept.esriKeyInterceptArrowKeys;axMapControl1.AutoKeyboardScrolling=true;使用鼠標(biāo)滾輪縮放地圖axMapControl1.AutoMouseWheel=true;axMapControl1.KeyIntercept=(int)esriKeyIntercept.esriKeyInterceptArrowKeys;AutoKeyboardScrolling=true;ConstantValueDescriptionesriKeyInterceptNone0Nokeysareintercepted.esriKeyInterceptArrowKeys1Interceptsthearrowkeys,normallyhandledbythecontainertochangecontrolfocus.esriKeyInterceptAlt2InterceptstheAltkey,normallyhandledbyacontainertochangefocus.esriKeyInterceptTab4InterceptstheTabkey,normallyhandledbythecontainertochangecontrolfocus.esriKeyInterceptEnter8InterceptstheEnterkey,normallyhandledbythecontainertoclickthedefaultbutton.IMapControlEvents2接口IMapControlEvents2是一個事件接口,它定義了MapControl能夠處理的全部事件,如OnExtentUpdated是地圖的Extent屬性發(fā)生變化時觸發(fā)的事件,OnAfterscreenDraw是繪屏結(jié)束后觸發(fā)的事件等。

當(dāng)?shù)貓D的覆蓋范圍變化時,觸發(fā)OnFullExtentUpdated事件,例如,往地圖中新增加一個圖層,其覆蓋范圍大于原圖的范圍。ITOCBuddyGetActiveViewReturnstheunderlyingobjectimplementingbasicmapandActiveView.ThisisusedtopopulatetheTOC.GetScaleReturnsthescaleofthesuppliedBasicMap,thisallowsforobjectsthatdonotsupportIMap.IToolbarBuddyObjectsthatimplementtheIToolbarBuddyinterfacecanbepassedtotheToolbarControl::SetBuddyControlmethod.TheCurrentToolisthetoolusedtointeractwiththedisplayareaofthe

IToolbarControl::Buddy.Guid.ToString(stringformat)format一個單格式說明符,它指示如何格式化此Guid的值。format參數(shù)可以是“N”、“D”、“B”或“P”。如果format為空引用(在VisualBasic中為Nothing)或空字符串(""),則使用“D”。說明符返回值的格式N32位:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxD由連字符分隔的

32位數(shù)字:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxB括在大括號中、由連字符分隔的

32位數(shù)字:

{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}P括在圓括號中、由連字符分隔的

32位數(shù)字:

(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)MapControl控件與MXD文件MapControl控件可以“鏈接”或“包含”地圖文檔。對于文檔文件,MapControl控件可以直接使用LoadMxFile方法來載入,這是最簡單的方法。除此之外,可以使用IMapDocument接口定義的屬性和方法來加載一個MXD文件。下面是一個載入文檔的例子--axMapControl1.LoadMxFileprivatevoidLoadMapDocument(){ System.Windows.Forms.OpenFileDialogopenFileDialog2; openFileDialog2=newOpenFileDialog(); openFileDialog2.Title="OpenMapDocument";

openFileDialog2.Filter="MapDocuments(*.mxd)|*.mxd"; openFileDialog2.ShowDialog(); stringsFilePath=openFileDialog2.FileName;

if(axMapControl1.CheckMxFile(sFilePath)) { axMapControl1.LoadMxFile(sFilePath,0,Type.Missing); }

else { MessageBox.Show(sFilePath+"isnotavalidArcMapdocument"); return; } }

在ArcMap中使用的地圖文檔對象為MxDocument,其主要接口是IMxDocument;在使用控件開發(fā)的獨立應(yīng)用程序中,使用的地圖文檔對象為MapDocument

,其主要接口是IMapDocument。IMapDocument接口定義了操作和管理文檔對象的方法和屬性。MapDocument類能夠封裝地圖文檔文件,如mxd、mxt和pmf等,它也可以封裝一個圖層文件(*.lyr)。使用這個對象可以獲取和更新一個文檔的內(nèi)容,設(shè)置文檔文件的讀、寫屬性,保存一個文檔文件(*.mxd)。IMxDocument與IMapDocumentIApplication的屬性、方法IMapDocumentm_MapDocument=newMapDocumentClass();privatevoidLoadMapDoc(){OpenFileDialogopenFileDialog2=newOpenFileDialog();openFileDialog2.Title="OpenMapDocument";openFileDialog2.Filter="MapDocuments(*.mxd)|*.mxd";openFileDialog2.ShowDialog(); stringsFilePath=openFileDialog2.FileName; If(m_MapDocument.IsMapDocument(sFilePath)){m_MapDocument.Open(sFilePath,"");axMapControl1.Map=m_MapDocument.get_Map(0);axMapControl1.Refresh(); }}privatevoidSaveDocument(){

if(m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename)==true){MessageBox.Show("Thismapdocumentisreadonly!"); return; }m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true);MessageBox.Show("Changessavedsuccessfully!");}鼠標(biāo)與控件的交互用鼠標(biāo)和地圖控件進行交互是最常用的操作,例如改變地圖顯示范圍、移動地圖,在控件上繪制幾何圖形等。使用鼠標(biāo)拖曳確定地圖顯示的范圍(拉框放大)privatevoidaxMapControl1_OnMouseDown

(objectsender,ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvente) { //改變地圖控件顯示范圍為當(dāng)前拖曳的區(qū)域

axMapControl1.Extent=axMapControl1.TrackRectangle(); //刷新地圖 axMapControl1.Refresh(esriViewDrawPhase.esriViewGeography,null,null); }publicvoidRefresh(esriViewDrawPhasephase,objectlayerOrElement,

objectenvelope);移動、旋轉(zhuǎn)地圖在MapControl中有一種更簡單便利的方法pan()來移動其中的地圖,還可使用Rotation屬性設(shè)置地圖的旋轉(zhuǎn)角度。下面的方法使用在MapControl控件的OnMoseDown事件中:

axMapControl1.pan();

axMapControl1.Rotation=45;在MapControl控件中繪制圖形MapControl控件提供了直接在控件上繪制圖形和文字的方法DrawShape與DrawText,這兩種繪制方法繪制的圖形都是緩存(cache),而不能真正保存,一旦窗口重繪,這些圖形就將消失。privatevoidaxMapControl1_OnMouseDown

(objectsender,ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvente) { //產(chǎn)生拖曳多邊形,并繪制

IGeometrypGeom=axMapControl1.TrackPolygon();

DrawMapShape(pGeom); //刷新地圖 //axMapControl1.Refresh(esriViewDrawPhase.esriViewGeography,null,null); }privatevoidDrawMapShape

(IGeometrypGeom){ IRgbColorpColor; pColor=newRgbColorClass(); pColor.Red=220; pColor.Green=112; pColor.Blue=60; //新建一個繪制圖形的填充符號

ISimpleFillSymbolpFillsyl; pFillsyl=newSimpleFillSymbolClass(); pFillsyl.Color=pColor; objectoFillsyl=pFillsyl;

axMapControl1.DrawShape(pGeom,refoFillsyl);}publicvoidDrawShape(IGeometryShape,

refobjectsymbol

);數(shù)據(jù)選擇在MapControl控件中用戶可以很方便地使用SelectByShape方法來構(gòu)造一個基于Map的選擇集。SelectByShape()方法選擇控件中所有圖層上處于選擇范圍內(nèi)的要素,并將其設(shè)置為一個選擇集。privatevoidaxMapControl1_OnMouseDown(objectsender,IMapControlEvents2_OnMouseDownEvente){ //產(chǎn)生拖曳多邊形

IGeometrypGeom=axMapControl1.TrackPolygon();axMapControl1.Map.SelectByShape(pGeom,null,false);

axMapControl1.

Refresh(esriViewDrawPhase.esriViewGeoSelection,null,null); } 清除選擇集的方法

axMapControl1.Map.Clearselection();axMapControl1.ActiveView.Refresh();實現(xiàn)鷹眼功能這個例子中有兩個AxMapControl控件:主控件axMapControl1和鷹眼控件axMapControl2.要實現(xiàn)鷹眼功能,關(guān)鍵有兩點:一是如何讓兩個控件顯示的數(shù)據(jù)保持一致,另一點是如何繪制鷹眼控件中的顯示方框。

兩個控件的數(shù)據(jù)共享privatevoidaxMapControl1_OnMapReplaced

(objectsender,IMapControlEvents2_OnMapReplacedEvente){ IMappMap; pMap=axMapControl1.Map; inti; for(i=0;i<=pMap.LayerCount-1;i++) {

axMapControl2.Map.AddLayer(pMap.get_Layer(i)); }}繪制鷹眼控件中的顯示方框主窗體視圖的范圍發(fā)生變化后,會觸發(fā)控件的OnExtentUndated事件,繪制方框的方法就在這個事件中進行:privatevoidaxMapControl1_OnExtentUpdated(objectsender,IMapControlEvents2_OnExtentUpdatedEvente) {

…….}PageLayoutControl控件PageLayoutControl對應(yīng)于ArcMap中的布局視圖(LayoutView),它封裝了PageLayout對象,提供了在布局視圖中控制地圖元素的屬性和方法。PageLayoutControl控件用于制圖,它可以方便地操作各種元素對象,以產(chǎn)生一幅制作精美的地圖對象。與MapControl控件相似,PageLayoutControl控件并不僅僅只是包含了一個PageLayout對象,它也擁有許多附加的事件、屬性和方法。頁面布局(通常簡稱為布局)是在頁面上編排和組織的地圖元素的集合,旨在用于地圖打印。布局中排布的常見地圖元素包括一個或多個數(shù)據(jù)框(每個數(shù)據(jù)框都含有一組有序的地圖圖層)、比例尺、指北針、地圖標(biāo)題、描述性文本和符號圖例。打印機、頁面布局、大小、單位數(shù)據(jù)驅(qū)動頁面根據(jù)地圖文檔的某個要素圖層或規(guī)則格網(wǎng)將地圖分割為多個部分,然后為每個部分生成一個相應(yīng)的頁面。創(chuàng)建數(shù)據(jù)驅(qū)動頁面PageLayoutControl控件同樣實現(xiàn)了多個接口IPageLayoutControlDefaultIPageLayoutControlIPageLayoutControl2IPageLayoutControl3IPageLayoutControlEventsIPageLayoutControl3Samples:PageLayoutControlLoadMapDocumentPageLayoutControlCopyFocusMapPageLayoutControlOverviewPageLayoutControlPageAppearancePageLayoutControlPrintPreviewPageLayoutControlPrintingPageLayoutControl操作MXD文件在AxPageLayoutControl控件對MXD文件的操作,與MapControl類似。publicvoidLoadMxFile(string

fileName,

object

password);

下面是在PageLayout控件中打開一個MXD文件的方法:privatevoidOpenMapDocument(){

IMapDocumentm_MapDocument=newMapDocumentClass();

OpenFileDialog

openFileDialog1=newOpenFileDialog(); openFileDialog1.Title="OpenMapDocument"; openFileDialog1.Filter="MapDocuments(*.mxd)|*.mxd";

openFileDialog1.ShowDialog(); stringsFilePath=openFileDialog1.FileName;

m_MapDocument.Open(sFilePath,"");

axPageLayoutControl1.PageLayout=m_MapDocument.PageLayout; axPageLayoutControl1.Refresh();}privatevoidSaveDocument()

{

//Checkthatthedocumentisnotreadonly.if(m_MapDocument.get_IsReadOnly(m_MapDocument.DocumentFilename)==true)

{MessageBox.Show("Thismapdocumentisreadonly!");return;

}

//Savewiththecurrentrelativepathsetting.m_MapDocument.Save(m_MapDocument.UsesRelativePaths,true);MessageBox.Show("Changessavedsuccessfully!");

}privatevoidcmdSaveAs_Click(objectsender,System.EventArgse){

//Openafiledialogforsavingmapdocuments.saveFileDialog1.Title="SaveMapDocumentAs";

saveFileDialog1.Filter="MapDocuments(*.mxd)|*.mxd";saveFileDialog1.ShowDialog();

stringsFilePath=saveFileDialog1.FileName;

if(sFilePath==“”)

return;

if(sFilePath==m_MapDocument.DocumentFilename)

{//Savechangestothecurrentdocument.SaveDocument();

}else

{//SaveAsanewdocumentwithrelativepaths.m_MapDocument.SaveAs(sFilePath,true,true);//Openthedocument.OpenDocument((sFilePath));MessageBox.Show("Documentsavedsuccessfully!");

}

}PageLayout與MapControl聯(lián)動在ArcMap程序中,數(shù)據(jù)視圖和布局視圖中的數(shù)據(jù)改變是實時互動的。在使用ArcObjects控件開發(fā)的獨立程序中實現(xiàn)這個功能,由于MapControl、PageLayoutControl兩個控件本身并不能自動實時互動,需要開發(fā)人員自己編程來實現(xiàn)兩個控件的實時聯(lián)動。Sample:

MapAndPageLayoutSynchAppTOCControl控件TOCControl控件概述ITOCControl與ITOCControl2ITOCControlEvents應(yīng)用開發(fā)實例TOCControl控件概述

TOCControl要與一個“伙伴控件”協(xié)同工作?!盎锇榭丶笨梢允荕apControl、PageLayoutControl、SceneControl或GlobeControl?!盎锇榭丶笨梢栽谠O(shè)計時通過TOCControl屬性頁設(shè)置或用SetBuddyControl方法通過編程設(shè)置。TOCControl用“伙伴控件”來顯示其地圖、圖層和符號體系內(nèi)容的一個交互樹視圖,并保持其內(nèi)容與“伙伴控件”同步。例如,如果

“伙伴控件”是一個MapControl,而且從該MapControl中刪除了一個圖層,則該圖層也會從TOCControl中刪除。同樣地,如果終端用戶與TOCControl交互并取消了某個圖層的Visibility復(fù)選框,則該圖層在MapControl中不再可見。TOCControl的主要接口有:ITOCControl、ITOCControl2、ITOCControlEvents;ITOCControl接口已被ITOCControl2所取代。ITOCControl與ITOCControl2ITOCControl接口是任何與TOCControl有關(guān)的任務(wù)的出發(fā)點,如設(shè)置控件的外觀,設(shè)置伙伴控件,管理圖層的可見性和標(biāo)簽的編輯。ITOCControlWhenqueryinginterfacetoITOCControlinVisualBasic.NETorVisualC#.NETtheObjectpropertyorcontainerspecificcodemustbeused.Thisisbecause.NETcontainstherealcontrolinsideawrapperobjectknownasanhost.

ITOCControl2

pTOCControl; pTOCControl=axTOCControl1.ObjectasITOCControl2; 或pTOCControl=axTOCControl1.

GetOcx()asITOCControl2;publicvoidHitTest

(int

X,

int

Y,

ref

esriTOCControlItem

ItemType,

ref

IBasicMap

BasicMap,

ref

ILayer

Layer,

ref

object

Unk,

ref

object

Data);

ItemTypespecifiesanenumerationindicatingthetypeof

item(none,map,layer,headingorlegendclass).BasicMapspecifiesanIMapobject.LayerspecifiesanILayerobject.UnkspecifiesanILegendGroupobject.Dataspecifiesalongindicatingtheindexofthelegendclasswithinthelegendgroup.Usethisindexinconjunctionwiththelegendgrouptoobtainaparticularlegendclass.Anindexof-1referstotheheadingifitispresent.IBasicMapmap=newMapClass();ILayerlayer=newFeatureLayerClass();objectother=newobject();objectindex=newobject();esriTOCControlItemitem=newesriTOCControlItem();//DeterminewhatkindofitemhasbeenclickedonaxTOCControl1.HitTest(e.x,e.y,refitem,refmap,reflayer,refother,refindex);if(e.button==1){

if(layer==null)return;

IFeatureLayerfeatureLayer=layerasIFeatureLayer;if(featureLayer==null)return;

IGeoFeatureLayergeoFeatureLayer=(IGeoFeatureLayer)

featureLayer;ILegendClasslegendClass=newLegendClassClass();ISymbolsymbol=null;if(otherisILegendGroup&&(int)index!=-1){

legendClass=((ILegendGroup)other).get_Class((int)index);symbol=legendClass.Symbol;}if(symbol==null)return;symbol=GetSymbolBySymbolSelector(symbol);if(symbol==null)return;legendClass.Symbol=symbol;this.Activate();axMapControl1.ActiveView.ContentsChanged();axMapControl1.Refresh(esriViewDrawPhase.esriViewGeography,null,null);axTOCControl1.Update();}ITOCControl2與ITOCControl相比,ITOCControl2多了以下1個屬性和2個方法:publicboolEnableLayerDragDrop{get;set;}:Indicatesiflayerscanbedraggedanddroppedinthecontrol.publicvoidGetSelectedItem(refesriTOCControlItemItemType,

refIBasicMapBasicMap,refILayerLayer,refobjectUnk,refobjectData):ReturnstheselecteditemintheTOCControl.publicvoidSelectItem(objectUnk,

objectData):用于設(shè)置TOCControl中的選擇項//SelectingthefocusmapaxTOCControl1.SelectItem(

axTOCControl1.ActiveView.FocusMap);//SelectingalayerinthefocusmapaxTOCControl1.SelectItem

(axTOCControl1.ActiveView.FocusMap.get_Layer(0));//ToselectanitemwithinaLegendGroupILayerlayer=axTOCControl1.ActiveView.FocusMap.get_Layer(0);ILegendInfolegendInfo=(ILegendInfo)layer;ILegendGrouplegendGroup=legendInfo.get_LegendGroup(0);//SelectaheadingaxTOCControl1.SelectItem

(legendGroup);//SelectalegendclassaxTOCControl1.SelectItem

(legendGroup,2);ITOCControlEventsITOCControlEvents是一個事件接口,它定義了TOCControl能夠處理的全部事件,如OnMouseDown、OnMouseMove、OnMouseUp、OnDoubleClick、OnBeginLabelEdit、OnEndLabelEdit、OnKeyDown、OnKeyUp。

privatevoidaxTOCControl1_OnBeginLabelEdit

(objectsender,ITOCControlEvents_OnBeginLabelEditEvente){

…………m_TOCControl.HitTest(e.x,e.y,refitem,refmap,reflayer,refother,refindex);//已在其它地方(如窗體的Load事件中)聲明為ITOCControl類型的變量,并已實例化if(item!=esriTOCControlItem.esriTOCControlItemLayer){e.canEdit=false;}}

privatevoidaxTOCControl1_OnEndLabelEdit(objectsender,ITOCControlEvents_OnEndLabelEditEvente){//Preventemptylabelsif(e.newLabel.Trim()==""){e.canEdit=false;}}SamplesTOCControlContextMenuTOCControlLayerDragDropTOCControlMetadataViewer應(yīng)用開發(fā)實例TOC的右鍵菜單在ArcMap中,“TableofContents”中右鍵菜單功能非常豐厚,而TOCControl控件并沒有提供右鍵菜單功能。要實現(xiàn)右鍵菜單功能,需要開發(fā)人員自己編程實現(xiàn)。當(dāng)右鍵點擊地圖時,彈出當(dāng)右鍵點擊圖層時,彈出

privatevoidMainForm_Load(objectsender,EventArgse)定義地圖右鍵菜單、圖層右鍵菜單privatevoidaxTOCControl1_OnMouseDown(objectsender,ITOCControlEvents_OnMouseDownEvente)根據(jù)右鍵點擊的對象,彈出相應(yīng)的右鍵菜單

IToolbarMenu

m_TocLayerMenu=newToolbarMenuClass();

IToolbarMenu

m_TocMapMenu=newToolbarMenuClass();privatevoidMainForm_Load(objectsender,EventArgse){……

//TOCControl圖層右鍵菜單

m_TocLayerMenu.AddItem(newOpenAttributeTableCmd(),0,0,false,esriCommandStyles.esriCommandStyleIconAndText);m_TocLayerMenu.AddItem(newRemoveLayerCmd(),0,1,false,esriCommandStyles.esriCommandStyleIconAndText);

m_TocLayerMenu.AddItem(newScaleThresholds(),1,2,true,esriCommandStyles.esriCommandStyleTextOnly);m_TocLayerMenu.AddItem(newScaleThresholds(),2,3,false,esriCommandStyles.esriCommandStyleTextOnly);m_TocLayerMenu.AddItem(newScaleThresholds(),3,4,false,esriCommandStyles.esriCommandStyleTextOnly);

m_TocLayerMenu.AddItem(newLayerSelectable(),1,5,true,esriCommandStyles.esriCommandStyleTextOnly);m_TocLayerMenu.AddItem(newLayerSelectable(),2,6,false,esriCommandStyles.esriCommandStyleTextOnly);m_TocLayerMenu.AddItem(newZoomToLayerCmd(),0,7,true,esriCommandStyles.esriCommandStyleIconAndText);m_TocLayerMenu.AddItem(newLayerPropertiesCmd(),0,8,false,esriCommandStyles.esriCommandStyleIconAndText);m_TocLayerMenu.SetHook(axMapControl1);……}privatevoidaxTOCControl1_OnMouseDown

(objectsender,ITOCControlEvents_OnMouseDownEvente){……axTOCControl1.HitTest(e.x,e.y,refitem,refmap,reflayer,refother,refindex);if(e.button==2){if(item==esriTOCControlItem.esriTOCControlItemMap){m_mapControl.CustomProperty=map;

m_TocMapMenu.PopupMenu

(e.x,e.y,axTOCControl1.hWnd);}elseif(layerisIFeatureLayer){m_mapControl.CustomProperty=layer;

m_TocLayerMenu.PopupMenu

(e.x,e.y,axTOCControl1.hWnd);}}publicsealedclassOpenAttributeTableCmd:BaseCommand{……publicoverridevoidOnClick(){IMapmap=null;if(m_hookHelper.HookisIMapControl3){m_mapcontrol=m_hookHelper.HookasIMapControl3;

currentLayer=m_mapcontrol.CustomPropertyasIFeatureLayer;if(currentLayer==null)return;map=m_mapcontrol.Map;}if(map==null)return;

LayerAttributeslayerAttributeTable=newLayerAttributes

(map,currentLayer);layerAttributeTable.Show(m_hookHelperasSystem.Windows.Forms.IWin32Window);}}publicpartialclassLayerAttributes:Form{……

DataSetm_layerDataSet=newDataSet

(m_dataSetName);privateSystem.Windows.Forms.DataGridViewdataGridView1;publicLayerAttributes

(IMapmap,IFeatureLayerfeatureLayer){InitializeComponent();m_map=map;m_activeView=mapasIActiveView;currentLayer=featureLayer;}privatevoidAttributesForm_Load(objectsender,EventArgse){this.Text=currentLayer.Name+"屬性表";

introwCount=ConstructDataSet

(currentLayer);

dataGridView1.DataSource=m_layerDataSet;dataGridView1.DataMember=currentLayer.Name;toolStripStatusLabel1.Text="選擇記錄數(shù):";toolStripStatusLabel2.Text="總記錄數(shù):"+currentLayer.FeatureClass.FeatureCount(null).ToString();}privateintConstructDataSet(IFeatureLayerpFeatLyr){ILayerFieldspFeatlyrFields=pFeatLyrasILayerFields;IFeatureClasspFeatCls=pFeatLyr.FeatureClass;introws=0;if(m_layerDataSet.Tables[pFeatLyr.Name]==null){DataTablepTable=newDataTable(pFeatLyr.Name);DataColumnpTableCol;for(inti=0;i<=pFeatlyrFields.FieldCount-1;i++){pTableCol=newDataColumn(pFeatlyrFields.get_Field(i).AliasName);pTable.Columns.Add(pTableCol);pTableCol=null;}

IFeatureCursorfeatures=pFeatLyr.Search(null,false);IFeaturefeature=features.NextFeature();while(feature!=null){DataRowpTableRow=pTable.NewRow();for(inti=0;i<=pFeatlyrFields.FieldCount-1;i++){

if(pFeatlyrFields.FindField(pFeatCls.ShapeFieldName)==i){pTableRow[i]=pFeatCls.ShapeType;}else{pTableRow[i]=feature.get_Value(i);}}pTable.Rows.Add(pTableRow);feature=features.NextFeature();}rows=pTable.Rows.Count;m_layerDataSet.Tables.Add(pTable);}returnrows;}ToolbarControl及相關(guān)對象ToolbarControl控件ToolbarControl上的命令ToolbarItem更新命令ToolbarMenu組件類ToolbarPaletteCommandPool操作棧(OperationStack)定制ToolbarControl控件ToolbarControl使用鉤子(hook)來聯(lián)系命令對象與MapControl

或PageLayoutControl

等控件,并提供屬性、方法、事件用于:管理控件的外觀;設(shè)置伙伴控件;添加、刪除命令項;設(shè)置當(dāng)前工具;定制工具。ToolbarControl要與一個“伙伴控件”協(xié)同工作?!盎锇榭丶笨梢允荕apControl、PageLayoutControl、SceneControl或GlobeControl?!盎锇榭丶笨梢栽谠O(shè)計時通過ToolbarControl屬性對話框設(shè)置或用SetBuddyControl方法通過編程設(shè)置。ToolbarControl主要接口有:IToolbarControlIToolbarControl2IToolbarControlDefaultIToolbarControlEvents(default)

IToolbarControl與IToolbarControl2IToolbarControl接口是任何與ToolbarControl有關(guān)的任務(wù)的出發(fā)點,如設(shè)置控件的外觀,設(shè)置伙伴控件,添加或去除命令、工具、菜單,定制ToolbarControl的內(nèi)容。IToolbarControl接口提供的主要屬性Buddy、CommandPool、CurrentTool、Customize、CustomProperty、Enabled、Object、OperationStack

、ToolTips、TextAlignment、UpdateInterval等。IToolbarControl接口提供的主要方法

AddItem、AddMenuItem、AddToolbarDef、Find、GetItem、GetItemRect、HitTest、MoveItem、Remove、RemoveAll、SetBuddyControl、Update等。IToolbarControl2(Newin9.2)與IToolbarControl相比,IToolbarControl2增加了以下:publicboolAlignLeft{get;set;}publicuintBackColor{get;set;}publicuintFadeColor{get;set;}publicesriToolbarFillDirectionFillDirection{get;set;}publicintIconSize{get;set;}publicvoidLoadItems(IStreampStream);publicesriToolbarOrientationOrientation{get;set;}publicvoidSaveItems(IStreampStream);publicboolShowHiddenItems{get;set;}publicboolThemedDrawing{get;set;}publicboolTransparent{get;set;}IToolbarControlDefault在絕大多數(shù)開發(fā)環(huán)境中,在容器(窗體)中放置ToolbarControl控件,將產(chǎn)生一個名叫axToolbarControl1的對象,這個對象上直接可用的屬性和方法對應(yīng)于IToolbarControlDefault接口上的屬性和方法,加上容器特有的屬性和方法。IToolbarControlDefault接口的屬性和方法,與ToolbarControl的最高編號主接口的屬性、方法相同。例如,目前版本中,IToolbarControlDefault等同于IToolbarControl2,但在未來的版本中,將變?yōu)镮ToolbarControl3。在軟件開發(fā)中,使用IToolbarControlDefault接口,能夠保證總是訪問最新版本的ToolbarControl。IToolbarControlEventsIToolbarControlEvents是一個事件接口,它定義了ToolbarControl能夠處理的全部事件,如OnDoubleClick、OnItemClick、OnKeyDown、OnKeyUp、OnMouseDown、OnMouseMove、OnMouseUp。ToolbarControl上的命令當(dāng)一個命令對象駐留在ToolbarControl后,將調(diào)用

ICommand.OnCreate(),該方法傳遞一個hook參數(shù)給應(yīng)用程序。

測試是否支持hook對象,如果不支持該hook對象,那么該命令無效;如果支持該hook對象,命令將存儲該該hook對象,以便以后使用。

例如,打開地圖文檔命令設(shè)計用于MapControl的,該MapControl作為hook參數(shù)被傳遞給OnCreate(),該命令保存該hook參數(shù)以便后面使用。如果ToolbarControl作為hook參數(shù)被傳遞給OnCreate事件,該命令將檢測該ToolbarControl的伙伴控件的類型,如果該伙伴控件為一GlobeControl,而該命令是為MapControl設(shè)計的,那么這時該命令無效。駐留在ToolbarControl上的命令項,當(dāng)用戶點擊時,將調(diào)用該命令的ICommand.OnClick()方法。依據(jù)命令項的類型,命令將使用hook參數(shù)訪問來自于伙伴控件的對象,執(zhí)行相應(yīng)的功能。有以下3種類型的命令項:簡單命令(Command):當(dāng)用戶點擊時,將調(diào)用該命令的ICommand.OnClick()方法,執(zhí)行相應(yīng)的功能。工具(Tool):需要用戶與伙伴控件交互,才能完成需要的功能。

當(dāng)用戶點擊ToolbarControl上的某一工具時,該工具就成為該ToolbarControl的CurrentTool,ToolbarControl并將其設(shè)置為伙伴控件CurrentTool,并將接收來自于伙伴控件的鼠標(biāo)、鍵盤事件。工具控件(ToolControl):類似于下拉列表框,駐留在ToolbarControl上的一個小窗口,由

IToolControl.hWnd提供窗口句柄.OnlyasingleinstanceofaparticulartoolcontrolcanbeaddedtotheToolbarControl.設(shè)計時,可以通過ToolbarControl的屬性對話框?qū)⒚铐椞砑拥絋oolbarControl上??梢杂萌N方法將命令添加到ToolbarControl中使用UID對象(使用GUID)使用progID給AddItem方法提供某個現(xiàn)有命令的一個實例。publicintAddItem(

objectitem,

intSubType,

intindex,

boolbeginGroup,

intGroupSpacing,

esriCommandStylesStyle);ConstantValueDescriptionesriCommandStyleTextOnly0Displaytextonly.esriCommandStyleIconOnly1Displayicononly.esriCommandStyleIconAndText2Displayiconandtext.esriCommandStyleMenuBar4Displaybarasmainmenu.//AddingacommandbyUIDUIDuID=newUIDClass();uID.Value="esriControls.ControlsMapFullExtentCommand";axToolbarControl1.AddItem(uID,-1,-1,false,0,

esriCommandStyles.esriCommandStyleIconOnly);//AddingacommandbyProgID

stringprogID="esriControls.ControlsMapFullExtentCommand";axToolbarControl1.AddItem(progID,-1,-1,false,0,esriCommandStyles.esriCommandStyleIconOnly);//AddingacommandbyICommand

ICommandcommand=newControlsMapFullExtentCommandClass();axToolbarControl1.AddItem(command,-1,-1,false,0,esriCommandStyles.esriCommandStyleIconOnly);ToolbarItemToolbarItem就是駐留在ToolbarControl或菜單上的單個Command,Tool,ToolControl或menuitem菜單。ToolbarItem的主要接口有:IToolbarItem。IToolbarltem接口的屬性決定工具條命令項的外觀。例如,工具條命令項是否在其左側(cè)有一條垂直線表示是否開始一個命令組(Group),及命令項的樣式是否有一個位圖、標(biāo)題或兩者都有。Command和Menu屬性返回工具條命令項代表的實際命令或菜單。ToolbarItem是一個不可創(chuàng)建的對象。引用不可創(chuàng)建的對象必須通過其它對象獲得,可以使用IToolbarControl:GetItem

方法獲得ToolbarItem。更新命令默認情況下,ToolbarControl每半秒鐘自動更新其自身一次,以確保駐留在ToolbarControl上的每個工具條命令項的外觀與其底層命令的Enabled、Bitmap和Caption等屬性同步。改變UpdateInterval屬性可以更改更新的頻率。UpdateInterval為0會停止任何自動發(fā)生的更新,開發(fā)人員必須編程調(diào)用Update方法以刷新每個工具條命令項的狀態(tài)。在應(yīng)用程序中首次調(diào)用Update方法時,ToolbarControl會檢測每個工具條命令項的ICommand::OnCreate方法是否已經(jīng)被調(diào)用過。如果還沒有調(diào)用過該方法,則該ToolbarControl作為“鉤子(hook)”被自動傳遞給ICommand::OnCreate方法。ToolbarMenu組件類ToolbarControl可以駐留下拉菜單項。工具條菜單(ToolbarMenu)只能駐留簡單命令(不允許駐留工具或工具控件)工具條菜單可以駐留在ToolbarControl上或作為“子菜單”駐留在另一個工具條菜單上或作為右鍵菜單出現(xiàn)。ToolbarMenu的主要接口有:IToolbarMenu、IToolbarMenu2。IToolbarMenu2ToolbarPaletteAToolbarPalettesuppliestheimplementationofapaletteitemthatcanhostcommandsandtools.AToolbarPalettecannothostToolControl,ToolbarMenuorMultiItemobjects.AToolbarPalettecaneitherbehosted

溫馨提示

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

評論

0/150

提交評論