You are on page 1of 28

Nguyn Hong Minh

----------------------------------------------------------------------------------------------------------------------

Xy dng ng dng ArcGIS Engine


vi cc iu khin Windows
-------------------------------------------------------------------------------------------------

Chng ny dnh cho nhng nh lp trnh mun xy dng v trin khai mt


ng dng s dng .NET. N miu t qu trnh xy dng v trin khai mt ng dng
se dng Cc iu khin ArcGIS.
Miu t d n.
Mc ch ca vic xy dng cc ng dng vi Window Control l gip bn
quen thuc vi cc bc cn thit pht trin v trin khai mt ng dng Web s
dng cc iu khin ArcGIS tiu chun bn trong VS .NET API. v d ny s dng
cc iu khin MapControl, PageLayoutControl, T - 1 -Chu :OC Control v
ToolbarControl nh l nhng iu khin ca Windows bn trong mi trng .NET.
cac nh lp trnh COM, Java, v C++ nn tp trung vo cc v d sau: Xy dng cc
ng dng vi ActiveX, xy dng cc ng dng vi visual JavaBeans,...
V d ny cung cp cc bc cn thit to ra mt ng dng GIS cho vic xem cc
ti liu bn c to ra t trc s dng ArcMap - mt ng dng ArcGIS
Desktop:
+ Np v nhng cc iu khin ArcGIS Control trong .NET.
+ Np ti liu c to ra t trc trong MapControl v
PageLayoutControl.
+ Thit t cc iu khin ToolbarControl v TOCControl.
+ iu khin vic thay i kch c form.
+ Thm cc lnh v cc cng c ArcGIS Engine cho ToolbarControl.
+ To ra cc thanh cng c x xung.
+ Qun l vic chnh sa nhn trong TOCControl.
+ V cc hnh khi trong MapControl.
+ To ra mt cng c ngi dng lm vic vi MapControl,
PageLayoutControl v ToolbarControl.
+ Trin khai ng dng trong h iu hnh Win.
Ni dung.
V d ny c thc thi trong mi trng .NET v s dng cc thnh phn
ESRI cha ArcGIS bn trong cc iu khin Windows. Cc thnh phn ESRI ny
hot ng nh mt cu ni gia m khng c qun l (unmanaged code) ca
COM v cc cc m .NET c qun l.Bt c tham chiu no ti cc thnh vin ca
cc iu khin ArcGIS COM u c hng toi i tng COM thc s. Tng
t nh vy, nhng phn hi t i tng COM cng c a qua cc thnh phn c
th tch hp ESRI ri a ti ng dng .NET. Mi iu khin ArcGIS Engine u c
cc s kin, thuc tnh v phng thc c th c truy nhp khi c nhng bn
trong .NET form. Cc i tong v chc nng bn trong mi iu khin c th c

----------------------------------------------------------------------------------------------------------------- 1
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

kt hp vi cc iu khin ESRI v iu khin t to khc to ra ng dng cui


cng pha ngi dng.
V d ny c vit bi c CSharp v VisualBasic.NET, mc d qu trnh thc
thi sau tp trung vo cc v d CSharp. Nhiu nh lp trnh s cm thy thoi mi
hn vi VB.NET, trong khi cu trc ca ngn ng lp trnh CSharp s quen thuc
hn vi nhng nh lp trnh Java v C++. D mi trng lp trnh th no i na,
bn s thnh cng vi cc iu khin ArcGIS ph thuc vo k nng trong trong mi
trng lp trnh v ArcObjects.
MapControl, PageLayoutControl,TOCControl v ToolbarControl c s
dng trong v d ny cung cp giao din lp trnh ng dng cho ngi s dng.C
iu khin ArcGIS ny c s dng lm cu ni trung gian gia cc ArcObjects
khc v c cu lnh ArcGIS Engine to ra mt ng dng hin th thng tin a l.
Thit k.
ng dng ny c thit k trc ht lm ni bt vn cc iu khin
ArcGIS s tng tc vi nhau nh th no, v th hai biu din mt vi kiu i
tng iu khin ArcGIS cho ngi lp trnh.
Mi iu khin ArcGIS .NET c mt b cc thuc tnh c th truy nhp khi
iu khin c nhng trong mi trng .NET. Nhng trang thuc tnh ny cung cp
nhng con ng ngn nht cho vic thit t cc thuc tnh v phng thc ca
iu khin, v cho php nh lp trnh xy dng ng dng m khng phi vit code.
ng dng ny khng s dng cc trang thuc tnh, m xy dng ng dng thun nht
l lp trnh. Mun tm hiu them nhng thng tin v cc trang thuc tnh, hy c
ArcGIS Developer Help.

Nhng thnh phn cn thit.

Ci t ArcGIS Engine Developer Kit v cc file bn quyn.


Ci t .NET 2003, .NET Framework 1.1 v bn quyn thch hp.
Ngi lp trnh phi quen thuc vi h iu hnh Windows v kin thc lm
vic vi .NET, hoc vi CSharp hoc vi VB.NET. V d ny cung cp mt s
thng tin v cch s dng cc iu khin ArcGIS trong .NET,ch n khng
phi l s thay th cho vic thc hnh trong mi trng lp trnh.
Kin thc c bn v ArcObjects v ng dng ArcGIS, chng hn ArcMap v
ArcCatalog,trong khi khng cn phi nm su lm v cc phn mm ESRI
khc.
V d ny c trong ng dn:
<install_location>\DeveloperKit\Samples\Developer_Guide_Scenarios\ArcGI
S_Engine\Building_an_ArcGIS_Control_Application\Map_Viewer.
Cc iu khin v th vin s dng trong v d ny bao gm:
AxMapControl AxTOCControl
AxPageLayoutControl AxToolbarControl
ESRI.ArcGIS.Carto ESRI.ArcGIS.System

----------------------------------------------------------------------------------------------------------------- 2
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

ESRI.ArcGIS.Display ESRI.ArcGIS.SystemUI
ESRI.ArcGIS.Geometry ESRI.ArcGIS.Utility
esriMapControl esriTOCControl
esriPageLayoutControl esriToolbarControl
Thc thi ng dng:
Np cc iu khin ArcGIS:
Trc khi bt u lp trnh ng dng, cc iu khin ArcGIS v cc tham
chiu th vin ArcGIS Engine khc m ng dng s dng phai c np trong
mi trng ng dng.
1. Khi ng VS.NET v to mt ng dng Windows Application ca C# t
hp thoi New project.
2. t tn ng dng l Controls, v duyt v tr save ng dng.
3. Bm chut phi vo tab Windows Form ca thanh cng c v chn Add/
Remove Items t menu ng cnh.
4. Trong hp thoi Customize Toolbox, chn .NET Framework v check vo
AxMapControl, AxPageLayoutControl,v AxTOCControl. Chn OK.
Cc iu khin ny gi y xut hin trong tab Windows Forms ca thanh
cng c.

5.Chn Project trn thanh menu ri chn Add Reference


6.Trong hp thoi Add Reference, nhy p vo ESRI.ArcGIS.Carto,
ESRI.ArcGIS.Display, ESRI.ArcGIS.Geometry, ESRI.ArcGIS.System,
ESRI.ArcGIS.SystemUI, ESRI.ArcGIS.Utility ri chn OK.

----------------------------------------------------------------------------------------------------------------- 3
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

Nhng cc iu khin ArcGIS trong mt container.

Trc khi bn c th truy cp cc thuc tnh, phng thc cng nh cc s


kin ca cc iu khin, mi iu khin cn c nhng bn trong mt ni
cha trong .NET. Khi cc iu khin c nhng bn trong form, chng s
xut hin trong giao din lp trnh ngi s dng ca ng dng.

1. M .NET Form
2. Kch p vo AxMapControl trong Windows Forms tab ca thanh cng c
them mt MapControl trn form.
3. Lp li thao tc ny vi AxPageLayoutControl, AxTOCControl v
AxToolbarControl.
4. Thay i kch thc v thay i v tr ca cc iu khin trn form nh sau:

----------------------------------------------------------------------------------------------------------------- 4
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

5.Kch p vo form hin th code ca form. S dng cc directives


sau ti v tr trn cng ca on m:

using System;
using System.Windows.Forms;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using esriToolbarControl;
using esriTOCControl;

Np cc ti liu Map trong PageLayoutControl v MapControl

Cc lp d liu v cc ti liu ring bit c to t trc m s dng


ArcMap- mt ng dng ArcGIS Desktop, c th c np trong iu khin
MapControl v PageLayoutControl.Bn c th np bn theo v d hoc np
mt bn bt k theo s thch c nhn.
1.Chn s kin Form_Load v np dng code sau (nu bn s dng bn
ring th phi thay th tn file bn chnh xc)

private void Form1_Load(object sender, System.EventArgs e)


{
//Np
//Np bn c to t trc trong PageLayoutControl s dng cc ng dn quan
h.
string fileName =

----------------------------------------------------------------------------------------------------------------- 5
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
@"..\..\..\..\..\..\..\..\Data\\ArcGIS_Engine_Developer_Guide\Gulf of St.
Lawrence.mxd";
if (axPageLayoutControl1.CheckMxFile(fileName))
{
axPageLayoutControl1.LoadMxFile(fileName,"");
}
}

2.Hin th form v chn axPageLayoutControl1 t ca s thuc tnh v


hin th cc s kin ca axLayoutControl. Kch p vo s kin
OnPageLayoutReplace thm phng thc x l s kin.

3. Trong s kin axPageLayoutControl_ OnPageLayoutReplace thm vo


dng code sau np bn tng t vo MapControl.

private void axPageLayoutControl1_OnPageLayoutReplaced(object sender,


ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnPageLayoutReplacedEvent
e)
{
//Np cng ti liu bn trong MapControl
axMapControl1.LoadMxFile(axPageLayoutControl1.DocumentFilename,null,
axMapControl1.LoadMxFile(axPageLayoutControl1.DocumentFilename,null,
null);
//t s m rng ca iu khin MapControl ty theo kch c thc ca nh

----------------------------------------------------------------------------------------------------------------- 6
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
axMapControl1.Extent = axMapControl1.FullExtent;
}

Thit t TOCControl v ToolbarControl

TOCControl v ToolbarControl lm vic nh l cu ni vi


PageLayoutControl ch khng phi l vi MapControl. lm c nh th,
PageLayoutControl phi c thit t l buddy control. TOCControl s dng
ActiveView ca buddy cha bn , cc lp v cc biu tng, trong khi bt c
cu lnh, cng c hoc cc menu items trn ToolbarControl s tng tc vi s hin
th ca buddy control.
1.Trong s kin Form_Load nhp vo nhng dng code sau :

private void Form1_Load(object sender, System.EventArgs e)


{
//Np bn c to t trc trong PageLayoutControl s dng cc ng dn quan h.
string fileName =@"..\..\..\..\..\..\..\..\Data\\ArcGIS_Engine_Developer_Guide\Gulf of
St.Lawrence.mxd";
if (axPageLayoutControl1.CheckMxFile(fileName))
{
axPageLayoutControl1.LoadMxFile(fileName,"");
}
//Thit t cc iu khin buddy.
axTOCControl1.SetBuddyControl(axPageLayoutControl1);
axToolbarControl1.SetBuddyControl(axPageLayoutControl1);
}

2. Xy dng v chy ng dng. Bn s c np trong PageLayoutControl


v TOCControl s thng k cc lp d liu trong bn . S dng TOCControl
thit t cc lp n hay hin bng cch check hay uncheck vo cc hp. Theo mc
nh, tiu im bn s c np vo MapControl. Ti thi im ny,
toolbarControl s trng bi khng c cu lnh no c add vo. Th thay i kch
c ca form, v ch rng cc iu khin khng thay i kch c.

----------------------------------------------------------------------------------------------------------------- 7
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

iu khin form thay i kch c

Khi form thay i kch c ti thi im chng trnh ang chy,


PageLayoutControl v MapControl khng t ng thay i kch c theo form.
thay i kch c cc control ny chng lun c th ko di lp y form, bn phi
c nh (anchor) cc iu khin vo form. Nu PageLayoutControl v MapControl
cha nhiu d liu th redraw (np li) d liu khi s kin Form_Resize xy ra.
tng hiu qu thc thi, bn c th ngn cn d liu redraw (np li) cho n khi
qu trnh thay i kch c kt thc. Trong qu trnh thay i kch c, ch c mt
bitmap c v li m thi.

1. Hin thj form v chon axPageLayoutControl1 t ca s thuc tnh. Chn thuc


tnh v c nh axPageLayoutControl vi l trn, di, tri,phi ca form.

----------------------------------------------------------------------------------------------------------------- 8
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

2. C nh axMapcontrol vi l trn, di v tri ca form.

3.Thm dng code sau vo u s kin Form_Load:

private void Form1_Load(object sender, System.EventArgs e)


{
//Ngn cn vic v li trong khi thay i kch c form.
this.SetStyle(ControlStyles.EnableNotifyMessage,true);
}

4.Khai bo cc hng s sau:

public class Form1 : System.Windows.Forms.Form


{

private const int WM_ENTERSIZEMOVE = 0x231;
private const int WM_EXITSIZEMOVE = 0x232;

5.Thm cc dng code sau np chng phng thc OnNotifyMessage

----------------------------------------------------------------------------------------------------------------- 9
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
protected override void OnNotifyMessage(System.Windows.Forms.Message m)
{
base.OnNotifyMessage (m);
if (m.Msg == WM_ENTERSIZEMOVE)
{
axMapControl1.SuppressResizeDrawing(true, 0);
axPageLayoutControl1.SuppressResizeDrawing(true, 0);
}
else if (m.Msg == WM_EXITSIZEMOVE)
{
axMapControl1.SuppressResizeDrawing(false, 0);
axPageLayoutControl1.SuppressResizeDrawing(false, 0);
}
}

6.Xy dng v sau chy th chng trnh. Th thay i kch c Form

Thm cc cu lnh vo ToolbarControl

ArcGIS Engine xy dng trn 120 cu lnh v cng c lm vic trc tip
vi MapControl, PageLayoutControl v ToolbarControl. Nhng cu lnh v cng
c ny cung cp cho bn rt nhiu cc chc nng GIS c s dng thng xuyn
cho vic iu chnh bn , qun l nh v la chn cc tnh nng. Bn s thm
nhng cu lnh v cng c vo ng dng ca bn.

1.Thm nhng dng m sau vo s kin Form_Load:

private void Form1_Load(object sender, System.EventArgs e)


{
string progID;
//Thm nhng cu lnh thng thng.
progID = "esriControlToolsGeneric.ControlsOpenDocCommand";
axToolbarControl1.AddItem(progID, -1 , -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
//Thm nhng cu lnh iu chnh PageLayout.
progID = "esriControlToolsPageLayout.ControlsPageZoomInTool";
axToolbarControl1.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPageZoomOutTool";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPagePanTool";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsPageLayout.ControlsPageZoomWholePageCommand";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID =

----------------------------------------------------------------------------------------------------------------- 10
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
"esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentBackCommand";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID =
"esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentForwardCommand";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
//Thm
//Thm nhng cu lnh iu chnh Map.
progID = "esriControlToolsMapNavigation.ControlsMapZoomInTool";
axToolbarControl1.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsMapNavigation.ControlsMapZoomOutTool";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsMapNavigation.ControlsMapPanTool";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
progID = "esriControlToolsMapNavigation.ControlsMapFullExtentCommand";
axToolbarControl1.AddItem(progID, -1, -1, false, 0,
esriCommandStyles.esriCommandStyleIconOnly);
}

Xy dng v chy th ng dng. ToolbarControl by gi cha cc cu lnh


ArcGIS Engine v cc cng c bn c th s dng iu chnh bn c np
trong PageLayoutControl. S dng cc cu lnh page layout iu chnh quanh
page layout thc s v cc cu lnh bn iu chnh d liu xut hin trong
khung d liu. S dng cu lnh m ti liu duyt v np cc ti liu khc.

----------------------------------------------------------------------------------------------------------------- 11
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

To Popup menu cho LayoutControl

Cng vi vic thm cc cu lnh ArcGIS Engine vo ToolbarControl lm


vic vi cc buddy control, bn cn c th to cc popup menu t cc cu lnh
ArcGIS Engine. Bn s thm mt popup menu lm vic vi PageLayoutControl.
Popup Menu ny s hin th bt c khi no bn kch chut phi trong vng hin
th ca PageLayoutControl

1. Thm nhng bin thnh vin sau :

public class Form1 : System.Windows.Forms.Form


{
private ESRI.ArcGIS.ToolbarControl.AxToolbarControl
axToolbarControl1;
private ESRI.ArcGIS.TOCControl.AxTOCControl axTOCControl1;
private ESRI.ArcGIS.MapControl.AxMapControl axMapControl1;
private ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl
axPageLayoutControl1;
private IToolbarMenu m_ToolbarMenu = new ToolbarMenuClass(); //The
popup menu

----------------------------------------------------------------------------------------------------------------- 12
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

2. Thm nhng dng code sau vo s kin Form_Load tip theo nhng dng
code thm cc lnh vo ToolbarControl, nhng trc cc dng code np (load)
bn :

private void Form1_Load(object sender, System.EventArgs e)


{
//Thm nhng cu lnh iu chnh bn
//Chia s command pool ca ToobarControl.
m_ToolbarMenu.CommandPool = axToolbarControl1.CommandPool;
axToolbarControl1.CommandPool;
//Thm nhng cu lnh vo toolbarMenu.
progID = "esriControlToolsPageLayout.ControlsPageZoomInFixedCommand";
m_ToolbarMenu.AddItem(progID, -1, -1, false,
esriCommandStyles.esriCommandStyleIconAndText);
progID = "esriControlToolsPageLayout.ControlsPageZoomOutFixedCommand";
m_ToolbarMenu.AddItem(progID, -1, -1, false,
esriCommandStyles.esriCommandStyleIconAndText);
progID = "esriControlToolsPageLayout.ControlsPageZoomWholePageCommand";
m_ToolbarMenu.AddItem(progID, -1, -1, false,
esriCommandStyles.esriCommandStyleIconAndText);
progID =
"esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentBackCommand";
m_ToolbarMenu.AddItem(progID, -1, -1, true,
esriCommandStyles.esriCommandStyleIconAndText);
progID =
"esriControlToolsPageLayout.ControlsPageZoomPageToLastExtentForwardCommand";
m_ToolbarMenu.AddItem(progID, -1, -1, false,
esriCommandStyles.esriCommandStyleIconAndText);
// Mc axPageLayoutControl vi ToolbarMenu.
m_ToolbarMenu.SetHook(axPageLayoutControl1);
//Np bn ...
}

3.Hin th Form dng ang thit k v chn axPageLayoutControl1 t ca s


thuc tnh, sau hin th cc s kin ca axPageLayoutControl. Kch p vo s
kin OnMouseDown thm phng thc x l s kin

4.Trong s kin axPageLayoutControl_OnMouseDown thm nhng dng code


sau:

private void axPageLayoutControl1_OnMouseDown(object sender,


ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnMouseDownEvent e)
{
//popup ToobarMenu.
if (e.button == 2)
{
m_ToolbarMenu.PopupMenu(e.x,e.y,axPageLayoutControl1.hWnd);
}
}

----------------------------------------------------------------------------------------------------------------- 13
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

5.Xy dng v chy th chng trnh. Kch chut phi ln vng hin th ca
PageLayoutControl hin th popup menu, v sau iu chnh quanh page
layout.

iu khin vic chnh sa nhn trong TOCControl

Theo mc nh, TOCControl cho php ngi dng thit t vic n hay hin
ca cc lp v thay i bn v tn cc lp khi chng xut hin. Bn s thm code
ngn cn user chnh sa tn v thay th chng vi mt chui trng.
1.Thm nhng dng code vo u s kin Form_Load:

private
private void Form1_Load(object sender, System.EventArgs e)
{
axTOCControl1.LabelEdit = esriTOCControlEdit.esriTOCControlManual;
//Thm nhng cu lnh chung
}

----------------------------------------------------------------------------------------------------------------- 14
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

2.Hin th Form dng thit k v chn AxTOCControl t ca s thuc tnh


v hin th cc s kin AxTOCControl. Kch dp vo OnEndLabelEdit thm
phng thc x l s kin.

3. Thm nhng dng code vo s kin axTOCControl_OnEndLabelEdit:

private void axTOCControl1_OnEndLabelEdit(object sender,


ESRI.ArcGIS.TOCControl.ITOCControlEvents_OnEndLabelEditEvent e)
{
//Nu nhn mi l mt chui trng th tm ngng vic chnh sa li.
string newLabel = e.newLabel;
if (newLabel.Trim() == "")
{
e.canEdit = false;
}
}
4.Xy dng v chy th chng trnh. iu chnh mt bn , mt lp ,tiu
hay cc nhn ch thch trong TOCControl, click vo n 1 ln v sau click vo
n ln th 2 bt u thay i nhn. Th thay th nhn vi mt chui trng. Bn c
th s dng phm ESC bt c khi no trong qu trnh chnh sa nhn tm ngng
vic thay i nhn li.

----------------------------------------------------------------------------------------------------------------- 15
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

V cc hnh khi trong MapControl

Bn s s dng MapControl nh l mt ca s khung nhn v v ln trn phm vi


hin thi ca bn bn trong PageLayoutControl. Khi bn iu chnh xung quanh
d liu bn trong khung d liu ca PageLayoutControl, bn s thy cp nht ca
khung ca s MapControl.

1. Thm nhng bin thnh vin sau:

public class Form1 : System.Windows.Forms.Form


{
private ESRI.ArcGIS.ToolbarControl.AxToolbarControl axToolbarControl1;
private ESRI.ArcGIS.TOCControl.AxTOCControl axTOCControl1;
private ESRI.ArcGIS.MapControl.AxMapControl axMapControl1;
private ESRI.ArcGIS.PageLayoutControl.AxPageLayoutControl
axPageLayoutControl1;
private IToolbarMenu m_ToolbarMenu = new ToolbarMenuClass();
private IEnvelope m_Envelope; //ng bao c v trn MapControl.
private Object m_FillSymbol; //Biu tng v ng bao trong MapControl.
private ITransformEvents_VisibleBoundsUpdatedEventHandler
visBoundsUpdatedE; //S kin tiu im bn ca PageLayoutMapControl.

2. To mt hm mi gi l CreateOverviewSymbol. y l ni bn s to ra cc
biu tng c s dng trong MapControl i din cho phm vi ca d
liu trong bn ca PageLayoutControl. Thm nhng dng code sau to
ra hm:

private void CreateOverviewSymbol()


{
//Ly giao din IRGBColor.
IRgbColor color = new RgbColor();
//t thuc tnh mu.
color.RGB = 255;
//Ly giao in biu tng ILine
ILineSymbol outline = new SimpleLineSymbol();
//t thuc tnh ca biu tng ng thng.
outline.Width = 1.5;
outline.Color = color;
//Ly giao din IFillSymbol.
ISimpleFillSymbol simpleFillSymbol = new SimpleFillSymbolClass();
//Thit t thuc tnh ca fill symbol.
simpleFillSymbol.Outline = outline;
simpleFillSymbol.Style = esriSimpleFillStyle.esriSFSHollow;
m_FillSymbol = simpleFillSymbol;
}

----------------------------------------------------------------------------------------------------------------- 16
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

3. Gi hm CreateOverviewSymbol t s kin Form_Load trc on m v


chnh sa nhn cho TOCControl.

private void Form1_Load(object sender, System.EventArgs e)


{
//To ra biu tng s dng trong MapControl.
CreateOverviewSymbol();
//Thit t chnh sa nhn
}

4. Thm hm OnVisibleBoundUpdated. Hm ny s lien kt ti mt s kin


c pht sinh bt c khi no phm vi ca bn b thay i v c s dng
thit t mt ng bao cho ranh gii nhn thy c ca bn . Lm ti
MapControl v hin th mi.

private void OnVisibleBoundsUpdated(IDisplayTransformation sender, bool


sizeChanged)
{
//Thit t ng bao mi cho phm vi nhn thy c.
m_Envelope = sender.VisibleBounds;
//Refresh MapControl.
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.
esriViewForeground, null, null);
}

5. Giao din s kin mc nh ca PageLayoutControl l


IPageLayoutControlEvents. Nhng s kin ny khng bit khi no phm vi
ca bn bn trong khung d liu thay i. n nhn ra s thay i
ny,chng ta phi dng giao din ItransformEvents ca tiu im bn bn
trong PageLayoutControl. Thm nhng dng code sau vo hm x l s kin
PageLayoutControl_OnPageLayoutReplaced trc on m load bn .

private void axPageLayoutControl1_OnPageLayoutReplaced(object sender,


ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnPageLayoutReplacedEvent
e)
{
//Ly giao din IActiveView ca bn trong PageLayoutControl.
IActiveView activeView = (IActiveView)
axPageLayoutControl1.ActiveView.FocusMap;
//By ITranformEvents ca tiu im bn bn trong PageLayoutControl
visBoundsUpdatedE = new
ITransformEvents_VisibleBoundsUpdatedEventHandler(OnVisibleBoundsUpdated);
((ITransformEvents_Event)activeView.ScreenDisplay.
DisplayTransformation).VisibleBoundsUpdated += visBoundsUpdatedE;
//Ly
//Ly phm vi ca bn .
m_Envelope = activeView.Extent;
//Np
//Np cng bn c to ra t trc trong MapControl.

----------------------------------------------------------------------------------------------------------------- 17
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
axMapControl1.LoadMxFile(axPageLayoutControl1.DocumentFilename,null,null);
//t
//t phm vi ca MapControl vi pham vi y ca d liu.
axMapControl1.Extent = axMapControl1.FullExtent;
}

6. Hin th Form ang thit kt v chn axMapControl1 t ca s thuc tnh v


hin th cc s kin ca axMapControl. Kch p trn s kin OnAfterDraw v
thm trnh x l s kin.
7. Thm nhng dng code sau vo hm x l s kin
axMapControl_OnAfterDraw v mt ng bao vi biu tng m bn to
ra t trc trn MapControl.

private void axMapControl1_OnAfterDraw(object sender,


ESRI.ArcGIS.MapControl.IMapControlEvents2_OnAfterDrawEvent e)
{
if (m_Envelope == null)
{
return;
}
//Nu nn c v li.
esriViewDrawPhase viewDrawPhase = (esriViewDrawPhase) e.viewDrawPhase;
if (viewDrawPhase == esriViewDrawPhase.esriViewForeground)
{
IGeometry geometry = m_Envelope;
axMapControl1.DrawShape(geometry, ref m_FillSymbol);
}
}

8. Xy dng chng trnh v chy th. S dng cc cng c iu hng m bn


thm vo t trc thay i phm vi bn trong PageLayoutControl.Phm
vi mi s c v ra trong MapControl.

----------------------------------------------------------------------------------------------------------------- 18
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

To cng c mi

To cc lnh v cng c lm vic vi MapControl v PageLayoutControl


rt ging vi vic to cc lnh cho ng dng ESRI ArcMap bn lm cc chng
trc. Bn s to mt cng c cho php thm ngy thng hin ti va v tr kch
chut trong PageLayoutControl. Bn cng s to cc lnh lm vic vi MapControl
v ToolbarControl.
on m cho cc cng c ngi dng t nh ngha ny l on m ngun cn li
ca v d ny. Nu bn mun s dng lnh ngi dng t nh ngha mt cch trc
tip ch khng mun t to n, bn c th i thng ti bc 24.
1. o mt n Class Library trong C# t hp thoi New.
2. t tn chng trnh l Commands, duyt v tr t file v save chng trnh.
3. Click trn menu Project v chn Add Reference
4. Trong hp thoi Add Reference, check ESRI.ArcGIS.Geometry,
ESRI.ArcGIS.System , ESRI.ArcGIS.SystemUI , ESRI.ArcGIS.Utility v
ESRI.ArcGIS.ControlCommands.
5. Them mt lp vo project, t tn l AddDateTool.
6. Click vo menu Project v chn Add Existing Item. Duyt file date.bmp t v
tr m ngun v d v thm n vo n ca bn.
7. Click vo date.bmp trong ca s Solution Explorer hin th cc thuc tnh
ca n trong ca s thuc tnh. Thay i thuc tnh Build Action thnh
----------------------------------------------------------------------------------------------------------------- 19
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

Embedded Resource. nh date.bmp c s dng lm trn b mt ca nt


lnh.
8. Thay i namespace ca AddDateTool thnh CSharpDotNETCommands.

namespace CSharpDotNETCommands
{
.

9. Thm nhng directive sau on m trn cng:

using System;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.ControlCommands;
using ESRI.ArcGIS.Utility.BaseClasses;
using System.Runtime.InteropServices;

10. Khai bo lp AddDateTool c tha k t lp o ESRI BaseTool v thm


vo modifier sealed:

public sealed class AddDateTool : BaseTool

11. Thm nhng dng code sau vo constructor ca lp AddDateTool.

public AddDateTool()
{
//Ly mng cc ti nguyn trong Assembly.
string[] res = GetType().Assembly.GetManifestResourceNames();
//Thit t thuc tnh cho cng c.
base.m_bitmap = new
System.Drawing.Bitmap(GetType().Assembly.GetManifestResourceStream(res[0]));
base.m_caption = Add Date;
base.m_category = "CustomCommands";
base.m_message = "Adds a date element to the page layout";
base.m_name = "CustomCommands_Add Date";
base.m_toolTip = "Add date";
}

12. Thm nhng bin thnh vin sau vo lp AddDateTool:

public sealed class AddDateTool : BaseTool


{
private IHookHelper m_HookHelper = new HookHelperClass();

----------------------------------------------------------------------------------------------------------------- 20
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

13. Trong ca s ClassView, iu chnh ti phng thc BaseCommand OnCreate


v chn chut phi hin th menu ng cnh.Chn Add, sau np chn
Override thm thuc tnh vo ca s vit code
14. Thm on m sau vo phng thc np chng OnCreate:

public override void OnCreate(object hook)


{
m_HookHelper.Hook = hook;
}

15. Trong ca s ClassView, tm thuc tnh BaseCommand Enable, bm chut


phi hin th menu ng cnh. Chon Add, Override thm thuc tnh ny
vo on m.
16. Thm on m sau np chng gi tr Enable mc nh khi c thc thi bi
lp BaseTool.

public override bool Enabled


{
get
{
if (m_HookHelper.ActiveView != null)
{
return true;
}
else
{
return false;
}
}
}

17. Trong ca s ClassView, chnh ti phng thc BaseToolOnMouseDown v


bm chut phi hin th menu ng cnh. Chn Add, sau Override
thm thuc tnh vo on m.
18. Thm on code sau np chng hm mc nh OnMouseDown khi c
thc thi bi BaseTool class.

public override void OnMouseDown(int Button, int Shift, int X, int Y)


{
base.OnMouseDown (Button, Shift, X, Y);
//Ly khung nhn ang hot ng.
IActiveView activeView = m_HookHelper.ActiveView;
//To mt thnh t vn bn mi.
ITextElement textElement = new TextElementClass();
//To biu tng vn bn.
ITextSymbol textSymbol = new TextSymbolClass();

----------------------------------------------------------------------------------------------------------------- 21
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
textSymbol.Size = 25;
//Thit t thuc tnh ca vn bn.
textElement.Symbol = textSymbol;
textElement.Text = DateTime.Now.ToShortDateString();
//QI cho IElement.
IElement element = (IElement) textElement;
//To mt page point.
IPoint point = new PointClass();
point = activeView.ScreenDisplay.DisplayTransformation.ToMapPoint(X,Y);
//Thit t elements geometry
element.Geometry = point;
activeView.GraphicsContainer.AddElement(element, 0);
//Lm ti graphics.
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null,
null);
}

19. ArcGIS Engine thch hp vi cc lnh ngi dng t nh ngha l mt lp


COM, bi vy, lp .NET m bn to phi c biu th nh mt lp COM
bng cch to mt gi COM c th gi c cho lp .NET . Trong ca s
Solution Explorer, chn chut phi vo project Commands v chn Properties
t menu ng cnh.
20. Trong hp thoi Property, chon Configuration Properties,v click vo Build.
Trong khung bn phi, thay i thuc tnh Register for COM Interop thnh
True. Nhn OK.
21. Trong ca s lp trnh ca lp AddDateTool, thm vo cc dng lnh khai
bo lp AddDateTool, xc nh cc thuc tnh cn thit cho COM.

[ClassInterface(ClassInterfaceType.None)]
[Guid("D880184E-AC81-47E5-B363-781F4DC4528F")]

22. Thm nhng dng m vo lop AddDateTool sau khi khai bo bin.on code
nh ngha cc hm ng k v khng ng k lp AddDateTool vi loi
thnh phn ESRI Controls Commands s dng cc loi tin ch (categories
utility.)

//ng k trong loi thnh phn 'ESRI Controls Commands'


#region Component Category Registration
[ComRegisterFunction()]
[ComVisible(false)]
static void RegisterFunction(String sKey)
{
string fullKey = sKey.Remove(0, 18) + @"\Implemented Categories";
Microsoft.Win32.RegistryKey regKey =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(fullKey, true);
if (regKey != null)
{

----------------------------------------------------------------------------------------------------------------- 22
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
regKey.CreateSubKey("{B284D891-22EE-4F12-A0A9-
B1DDED9197F4}");
}
}
[ComUnregisterFunction()]
[ComVisible(false)]
static void UnregisterFunction(String sKey)
{
string fullKey = sKey.Remove(0, 18) + @"\Implemented Categories";
Microsoft.Win32.RegistryKey regKey =
Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(fullKey, true);
if (regKey != null)
{
regKey.DeleteSubKey("{B284D891-22EE-4F12-A0A9-B1DDED9197F4}");
}
}
#endregion

23. Xy dng chng trnh.


24. Trong chng trnh VS.NET Windows Application bn to trc ,
thm on m lnh sau thm nhng lnh iu chnh bn :

private void Form1_Load(object sender, System.EventArgs e)


{
//Thm cc lnh iu khin ArcMap.
//Thm cng c biu din ngy thng ngi dng t nh ngha.
progID = "CSharpDotNETCommands.AddDateTool";
axToolbarControl1.AddItem(progID, -1, -1, true, 0,
esriCommandStyles.esriCommandStyleIconAndText);
//Thm cc cu lnh ti ToolbarMenu.
}

25. Chy chng trnh v s dng AddDateTool thm cc thnh phn vn bn


vo PageLayoutControl hin th ngy hin thi.

----------------------------------------------------------------------------------------------------------------- 23
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

C nhn ha ToolbarControl

Cng vi vic thm cc lnh v cng c ArcGIS Engine vo ToolbarControl


trong s kin Form_Load,bn cn c th thc hin iu ny thng qua vic c nhn
ha ToolbarControl v s dng hp thoi c nhn ha. lm nh th, bn s t
ToolbarControl theo kiu customize v hin th hp thoi customize.

1.Thm nhng bin thnh vin sau:

public class Form1 : System.Windows.Forms.Form


{

private ITransformEvents_VisibleBoundsUpdatedEventHandler
visBoundsUpdatedE;
private ICustomizeDialog m_CustomizeDialog = new
CustomizeDialogClass();
//CustomizeDialog c s dng bi ToolbarControl.
private ICustomizeDialogEvents_OnStartDialogEventHandler
startDialogE;
private ICustomizeDialogEvents_OnCloseDialogEventHandler
closeDialogE;

----------------------------------------------------------------------------------------------------------------- 24
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

2.To mt hm mi c tn l CreateCustomizeDialog. y l ni bn s to
mt hp thoi customize bng cch thm nhng cu lnh sau trong hm:

private void CreateCustomizeDialog()

startDialogE = new
ICustomizeDialogEvents_OnStartDialogEventHandler(OnStartDialog);
((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnStartDialog +=
startDialogE;
closeDialogE = new
ICustomizeDialogEvents_OnCloseDialogEventHandler(OnCloseDialog);
((ICustomizeDialogEvents_Event)m_CustomizeDialog).OnCloseDialog +=
closeDialogE;
//Thit t ta
m_CustomizeDialog.DialogTitle = Customize ToolbarControl Items;
//Hin th nt lnh Add from file
m_CustomizeDialog.ShowAddFromFile = true;
//Thit t ToolbarControl c th thm cc item.
m_CustomizeDialog.SetDoubleClickDestination(axToolbarControl1);
}

3.Gi hm CreateCustomizeDialog t s kin Form_Load trc cu lnh gi


hm CreateOverviewSymbol :
private void Form1_Load(object sender, System.EventArgs e)
{
//to hp thoi Customize cho ToolbarControl.
CreateCustomizeDialog();
//To biu tng s dng trong ToolbarControl.
}
4.Thm mt check box vo Form, t tn l chkCustomize v nhn l
Customize
5.Hin th Form dng thit k v chn chkCustomize t ca s thuc tnh v
hin th cc s kin ca chkCustomize. Kch p vo s kin CheckedChanged
thm mt trnh x l s kin vo ca s lp trnh.
6.Thm on code sau vo s kin chkCustomize_CheckedChanged:

private void chkCustomize_CheckedChanged(object sender, System.EventArgs


e)
{
//Hin th hay du i hp thoi customize.
if (chkCustomize.Checked == false)
{
m_CustomizeDialog.CloseDialog();
axToolbarControl1.Customize = false;
}
else
{

----------------------------------------------------------------------------------------------------------------- 25
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------
m_CustomizeDialog.StartDialog(axToolbarControl1.hWnd);
axToolbarControl1.Customize = true;
}
}

7.Thm hm x l s kin OnStartDialog v OnCloseDialog. Nhng hm ny


c lin kt vi cc s kin pht sinh bt c skhi no hp thoi customize m hoc
ng.

private void OnStartDialog()


{
axToolbarControl1.Customize = true;
}
private void OnCloseDialog()
{
axToolbarControl1.Customize = false;
chkCustomize.Checked = false;
}

8.Xy dng v chy th chng trnh v check vo check box customize


thit t ToolbarControl theo kiu c nhn ha v m hp thoi customize.
9.Trong tab Commands chn loi Graphics Element, kch p vo Select
Elements thm n vo ToolbarControl. Click vo cc biu tng trn
ToolbarControl, bn c th iu chnh giao din theo bn, hoc nhm cc i
tng.

----------------------------------------------------------------------------------------------------------------- 26
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

10.Tm ngng c nhn ha ng dng. S dng cng c select di chuyn


dng vn bn cha ngy thng hin thi.

Trin khai ng dng


trin khai thnh cng ng dng trong mt my tnh khc, ng dng phi c
c mt license. Trc ht n s kim tra xem product license c sn sng khng v
sau n khi ng license ny. Nu ng k license tht bi, ng dng khng th
hot ng.
1. Thm nhng bin thnh vin sau:
2. Thm nhng dng code sau vo u hm x l s kin Form_Load:
3. Hin th Form dng thit k v chn Form1 t ca s thuc tnh v hin th
cc s kin ca Form. Click p vo s kin Closing thm hm x l s
kin.
4. Trong s kin Form_Closing , thm nhng dng code sau:
5. Xy dng chng trnh v pht hnh ng dng.

trin khai ng dng thn cng mt my khch:

----------------------------------------------------------------------------------------------------------------- 27
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows
Nguyn Hong Minh
----------------------------------------------------------------------------------------------------------------------

+ File chy ca chng trnh v DLL cha lnh c nhn ha cn c trin khai
trn cc my khch. Cng c ng k thnh phn (RegAsm.exe) c s dng
thm thng tin v cc lp c xy dng vi registry.
+My khch cn phi ci t ArcGIS Engine Runtime v bn quyn ArcGIS
Engine tiu chun.
+My khch phi c ci t .NET Framework 1.1.

----------------------------------------------------------------------------------------------------------------- 28
Xy dng ng dng ArcGIS Engine vi cc iu khin Windows

You might also like