📄 form1.cs
字号:
this.ClientSize = new System.Drawing.Size(520, 390);
this.Controls.Add(this.axLicenseControl1);
this.Controls.Add(this.axPageLayoutControl1);
this.Controls.Add(this.axMapControl1);
this.Controls.Add(this.cmdZoomPage);
this.Controls.Add(this.cmdFullExtent);
this.Controls.Add(this.Label2);
this.Controls.Add(this.Label1);
this.Controls.Add(this.cboMaps);
this.Controls.Add(this.txbPath);
this.Controls.Add(this.cmdLoad);
this.Controls.Add(this.Label4);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axMapControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
m_bUpdateFocusMap = false;
m_bReplacedPageLayout = false;
axMapControl1.ShowScrollbars = false;
}
private void cmdFullExtent_Click(object sender, System.EventArgs e)
{
m_bUpdateFocusMap = true;
//Zoom to the full extent of the data in the map
axMapControl1.Extent = axMapControl1.FullExtent;
}
private void cmdZoomPage_Click(object sender, System.EventArgs e)
{
//Zoom to the whole page
axPageLayoutControl1.ZoomToWholePage();
}
private void cmdLoad_Click(object sender, System.EventArgs e)
{
//Open a file dialog for selecting map documents
openFileDialog1.Title = "Browse Map Document";
openFileDialog1.Filter = "Map Documents (*.mxd)|*.mxd";
openFileDialog1.ShowDialog();
//Exit if no map document is selected
string filePath = openFileDialog1.FileName;
if (filePath == "") return;
//If valid map document
if (axPageLayoutControl1.CheckMxFile(filePath))
{
//Set mouse pointers
axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
//Reset controls
axMapControl1.ActiveView.Clear();
axMapControl1.ActiveView.GraphicsContainer.DeleteAllElements();
axMapControl1.ActiveView.Refresh();
cboMaps.Items.Clear();
txbPath.Text = filePath;
//Load map document
axPageLayoutControl1.LoadMxFile(filePath,Type.Missing);
//Set mouse pointers
axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filePath + " is not a valid ArcMap document");
return;
}
}
private void ListMaps()
{
//Get IGraphicsContainer interface
IGraphicsContainer graphicsContainer = axPageLayoutControl1.GraphicsContainer;
graphicsContainer.Reset();
//Query Interface for IElement interface
IElement element = graphicsContainer.Next();
//Loop through the elements
int index = 0;
while (element != null)
{
if (element is IMapFrame)
{
IMapFrame mapFrame = (IMapFrame) element;
//Query interface for IElementProperties interface
IElementProperties elementProperties = (IElementProperties) element;
//Get the name of the Map in the MapFrame
string sMapName = mapFrame.Map.Name;
//Set the name of the MapFrame to the Map's name
elementProperties.Name = sMapName;
//Add the map name to the ComboBox
cboMaps.Items.Insert(index, mapFrame.Map.Name);
//If the Map is the FocusMap select the MapName in the ComboBox
if (sMapName == axPageLayoutControl1.ActiveView.FocusMap.Name)
{
cboMaps.SelectedIndex = index;
}
index = index + 1;
}
element = graphicsContainer.Next();
}
}
private void CopyAndOverwriteMap()
{
//Get IObjectCopy interface
IObjectCopy objectCopy = new ObjectCopyClass();
//Get IUnknown interface (map to copy)
object toCopyMap = axPageLayoutControl1.ActiveView.FocusMap;
//Get IUnknown interface (copied map)
object copiedMap = objectCopy.Copy(toCopyMap);
//Get IUnknown interface (map to overwrite)
object toOverwriteMap = axMapControl1.Map;
//Overwrite the MapControl's map
objectCopy.Overwrite(copiedMap, ref toOverwriteMap);
SetMapExtent();
}
private void SetMapExtent()
{
//Get IActiveView interface
IActiveView activeView = (IActiveView) axPageLayoutControl1.ActiveView.FocusMap;
//Set the control's extent
axMapControl1.Extent = activeView.Extent;
//Refresh the display
axMapControl1.ActiveView.Refresh();
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//Release COM objects
ESRI.ArcGIS.Utility.COMSupport.AOUninitialize.Shutdown();
}
private void axMapControl1_OnAfterScreenDraw(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnAfterScreenDrawEvent e)
{
//Set mouse pointers
axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
if (m_bUpdateFocusMap == false) return;
//Get IActiveView interface
IActiveView activeView = (IActiveView) axPageLayoutControl1.ActiveView.FocusMap;
//Get IDisplayTransformation interface
IDisplayTransformation displayTransformation = activeView.ScreenDisplay.DisplayTransformation;
//Set the visible extent of the focus map
displayTransformation.VisibleBounds = axMapControl1.Extent;
//Refresh the focus map
activeView.Refresh();
m_bUpdateFocusMap = false;
}
private void axMapControl1_OnBeforeScreenDraw(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnBeforeScreenDrawEvent e)
{
//Set mouse pointers
axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.MapControl.IMapControlEvents2_OnMouseDownEvent e)
{
m_bUpdateFocusMap = true;
if (e.button == 1)
{
axMapControl1.Extent = axMapControl1.TrackRectangle();
}
else if (e.button == 2)
{
axMapControl1.Pan();
}
}
private void axPageLayoutControl1_OnBeforeScreenDraw(object sender, ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnBeforeScreenDrawEvent e)
{
//Set mouse pointers
axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
}
private void axPageLayoutControl1_OnAfterScreenDraw(object sender, ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnAfterScreenDrawEvent e)
{
//Set mouse pointers
axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
if (m_bReplacedPageLayout == false) return;
CopyAndOverwriteMap();
m_bReplacedPageLayout = false;
}
private void axPageLayoutControl1_OnMouseDown(object sender, ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnMouseDownEvent e)
{
if (e.button == 1)
{
axPageLayoutControl1.Extent = axPageLayoutControl1.TrackRectangle();
}
else if (e.button == 2)
{
axPageLayoutControl1.Pan();
}
}
private void axPageLayoutControl1_OnPageLayoutReplaced(object sender, ESRI.ArcGIS.PageLayoutControl.IPageLayoutControlEvents_OnPageLayoutReplacedEvent e)
{
m_bReplacedPageLayout = true;
ListMaps();
}
private void axPageLayoutControl1_OnFocusMapChanged(object sender, System.EventArgs e)
{
CopyAndOverwriteMap();
}
private void cboMaps_SelectedIndexChanged(object sender, System.EventArgs e)
{
IPageLayoutControl pageLayoutControl = (IPageLayoutControl) axPageLayoutControl1.GetOcx();
//Get IMapFrame interface
IMapFrame element = (IMapFrame) pageLayoutControl.FindElementByName(cboMaps.Text, 1);
//Set the FocusMap
axPageLayoutControl1.ActiveView.FocusMap = element.Map;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -