using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication1
{
public partial class FrmMain : DevComponents.DotNetBar.RibbonForm
{
private INorthArrow m_NorthArrrow;
private IElement m_Element;
private string operation;
// 构造函数
public FrmMain()
{
InitializeComponent();
}
// 地图放大
private void btnZoomIn_Click(object sender, EventArgs e)
{
operation = null;
ICommand command = new ControlsPageZoomInTool();
command.OnCreate(axPageLayoutControl1.Object);
axPageLayoutControl1.CurrentTool = command as ITool;
}
// 地图缩小
private void btnZoomOut_Click(object sender, EventArgs e)
{
operation = null;
ICommand command = new ControlsPageZoomOutTool();
command.OnCreate(axPageLayoutControl1.Object);
axPageLayoutControl1.CurrentTool = command as ITool;
}
// 地图漫游
private void btnPan_Click(object sender, EventArgs e)
{
operation = null;
ICommand command = new ControlsPagePanTool();
command.OnCreate(axPageLayoutControl1.Object);
axPageLayoutControl1.CurrentTool = command as ITool;
}
// 全图显示
private void btnFullExtent_Click(object sender, EventArgs e)
{
ICommand command = new ControlsPageZoomWholePageCommand();
command.OnCreate(axPageLayoutControl1.Object);
command.OnClick();
}
// 添加数据
private void btnLoadData_Click(object sender, EventArgs e)
{
ICommand command = new ControlsAddDataCommand();
command.OnCreate(axPageLayoutControl1.Object);
command.OnClick();
}
// 添加指北针
private void btnAddNorthArrow_Click(object sender, EventArgs e)
{
FrmNorthArrow frmNorthArrow = new FrmNorthArrow();
frmNorthArrow.OnQueryNorthArrow += pNorthArrow => m_NorthArrrow = pNorthArrow;
frmNorthArrow.ShowDialog();
axPageLayoutControl1.CurrentTool = null;
axPageLayoutControl1.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
operation = "添加指北针";
}
// OnMouseDown事件
private void axPageLayoutControl1_OnMouseDown(object sender, IPageLayoutControlEvents_OnMouseDownEvent e)
{
if (operation == "添加指北针")
{
IEnvelope pEnvelope = axPageLayoutControl1.TrackRectangle();
if (pEnvelope.IsEmpty || pEnvelope == null || pEnvelope.Width == 0 || pEnvelope.Height == 0)
{
return;
}
// 删除已有指北针
IActiveView pActiveViewv = axPageLayoutControl1.PageLayout as IActiveView;
IGraphicsContainer pGraphicsContainer = pActiveViewv.GraphicsContainer;
if (m_Element != null)
{
pGraphicsContainer.DeleteElement(m_Element);
pActiveViewv.Refresh();
}
// 获取框架元素
IMapFrame pMapFrame = pGraphicsContainer.FindFrame(pActiveViewv.FocusMap) as IMapFrame;
IMapSurroundFrame pMapSurroundFrame = new MapSurroundFrame() as IMapSurroundFrame;
pMapSurroundFrame.MapFrame = pMapFrame;
pMapSurroundFrame.MapSurround = m_NorthArrrow as IMapSurround;
// 添加指北针
m_Element = pMapSurroundFrame as IElement;
m_Element.Geometry = pEnvelope;
pGraphicsContainer.AddElement(m_Element, 0);
pActiveViewv.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using stdole;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication1
{
public partial class FrmNorthArrow : DevComponents.DotNetBar.OfficeForm
{
// 定义事件
public event Action<INorthArrow> OnQueryNorthArrow;
// 样式变量
private ISymbologyStyleClass m_SymbologyStyleClass;
private IStyleGalleryItem m_StyleGalleryItem;
private INorthArrow m_NorthArrow;
// 构造函数
public FrmNorthArrow()
{
InitializeComponent();
}
// Load事件
private void FrmNorthArrow_Load(object sender, EventArgs e)
{
axSymbologyControl1.LoadStyleFile(Application.StartupPath + "\\style\\ESRI.ServerStyle");
axSymbologyControl1.StyleClass = esriSymbologyStyleClass.esriStyleClassNorthArrows;
// 选择符号
m_SymbologyStyleClass = axSymbologyControl1.GetStyleClass(axSymbologyControl1.StyleClass);
m_SymbologyStyleClass.SelectItem(0);
// 预览符号
PriviewSymbol();
inputAngle.Value = (int)m_NorthArrow.CalibrationAngle;
inputColor.SelectedColor = Utility.ConvertToColor(m_NorthArrow.Color);
}
// 切换符号
private void axSymbologyControl1_OnItemSelected(object sender, ISymbologyControlEvents_OnItemSelectedEvent e)
{
m_StyleGalleryItem = e.styleGalleryItem as IStyleGalleryItem;
m_NorthArrow = m_StyleGalleryItem.Item as INorthArrow;
//
PriviewSymbol();
inputAngle.Value = (int)m_NorthArrow.CalibrationAngle;
inputColor.SelectedColor = Utility.ConvertToColor(m_NorthArrow.Color);
}
// 设置颜色
private void inputColor_SelectedColorChanged(object sender, EventArgs e)
{
m_NorthArrow.Color = Utility.ConvertToRgbColor(inputColor.SelectedColor);
PriviewSymbol();
}
// 设置角度
private void inputAngle_ValueChanged(object sender, EventArgs e)
{
m_NorthArrow.CalibrationAngle = inputAngle.Value;
PriviewSymbol();
}
// 符号预览
private void PriviewSymbol()
{
IPictureDisp pPictureDisp = m_SymbologyStyleClass.PreviewItem(m_StyleGalleryItem, pictureBox1.Width, pictureBox1.Height);
Image priviewImage = Image.FromHbitmap(new IntPtr(pPictureDisp.Handle));
pictureBox1.Image = priviewImage;
}
// 确定
private void btnOk_Click(object sender, EventArgs e)
{
if (OnQueryNorthArrow != null)
{
m_NorthArrow.Size *= 3;
OnQueryNorthArrow(m_NorthArrow);
this.Close();
this.Dispose();
}
}
// 取消
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
}
}
辅助类代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using stdole;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
namespace WindowsFormsApplication1
{
public class Utility
{
// Color转换为IColor
public static IColor ConvertToRgbColor(Color color)
{
IColor pColor = new RgbColor();
pColor.RGB = color.R + color.G * 256 + color.B * 65536;
return pColor;
}
// IColor转换为Color
public static Color ConvertToColor(IColor pColor)
{
return ColorTranslator.FromOle(pColor.RGB);
}
}
}