双击PageLayout界面触发事件:
IMapSurroundFrame psurround = null;
IPageLayoutControl pPageLayout = aecPageLayout.Object as IPageLayoutControl;
IElement pelement, selectElement = null;
IGraphicsContainerSelect pGraphicsContainerSelect = pPageLayout.PageLayout as IGraphicsContainerSelect;
IGraphicsContainer pGraphicsContainer = pPageLayout.PageLayout as IGraphicsContainer;
pGraphicsContainer.Reset();
pelement = pGraphicsContainer.Next();
while (pelement != null)
{
if (pelement.HitTest(e.pageX, e.pageY, 0.1))
{
if (pelement is ITextElement)
{
selectElement = pelement;
}
if (pelement is IMapSurroundFrame)
{
selectElement = pelement;
}
if (pelement is IMarkerElement)
{
selectElement = pelement;
}
if (pelement is IPictureElement)
{
selectElement = pelement;
}
}
pelement = pGraphicsContainer.Next();
}
if (selectElement is IMapSurroundFrame)
{
psurround = selectElement as IMapSurroundFrame;
if (psurround.MapSurround is ILegend)
{
LegendProperty pLegendProperty = new LegendProperty(MapControl, pPageLayout, selectElement);
pLegendProperty.ShowDialog();
}
}
private AxMapControl MapControl;
private IPageLayoutControl PageLayoutControl;
private IMapSurroundFrame pMapSurroundFrame;
private IMapSurround pMapSurround;
private ILegend3 pLegend;
private IFrameProperties m_FrameProperties;
private stdole.IFontDisp pFont_title;
private ILegendItem mLegendItem;
public LegendProperty(AxMapControl mapControl, IPageLayoutControl pageLayoutControl, IElement Element)
{
InitializeComponent();
MapControl = mapControl;
PageLayoutControl = pageLayoutControl;
pMapSurroundFrame = Element as IMapSurroundFrame;
pMapSurround = pMapSurroundFrame.MapSurround;
pLegend = pMapSurround as ILegend3;
IFrameElement pIFrameElement = Element as IFrameElement;
m_FrameProperties = pIFrameElement as IFrameProperties;
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LegendProperty_Load(object sender, EventArgs e)
{
//更新图层
GetLayer(MapControl, listMapLayer);
listMapLayer.SetSelected(0, true);
for (int i = 0; i < pLegend.ItemCount; i++)
{
listLegend.Items.Add(pLegend.get_Item(i).Layer.Name);
}
if (pLegend.ItemCount != 0)
{
listLegend.SetSelected(0, true);
}
listLegend_ValueChanged();
//更新复选框
checkHeading.Checked = pLegend.get_Item(0).ShowHeading;
checkLayerName.Checked = pLegend.get_Item(0).ShowLayerName;
checkLabel.Checked = pLegend.get_Item(0).ShowLabels;
checkDescriptions.Checked = pLegend.get_Item(0).ShowDescriptions;
//更新图例标题
textTitle.Text = pLegend.Title;
checkTitleVisible.Checked = pLegend.Format.ShowTitle;
//更新对齐方式
if (pLegend.Format.TitleSymbol.HorizontalAlignment == esriTextHorizontalAlignment.esriTHALeft)
{
checkLeft.Checked = true;
}
if (pLegend.Format.TitleSymbol.HorizontalAlignment == esriTextHorizontalAlignment.esriTHACenter)
{
checkCenter.Checked = true;
}
if (pLegend.Format.TitleSymbol.HorizontalAlignment == esriTextHorizontalAlignment.esriTHARight)
{
checkRight.Checked = true;
}
if (pLegend.Format.TitleSymbol.HorizontalAlignment == esriTextHorizontalAlignment.esriTHAFull)
{
checkNothing.Checked = true;
}
//更新图例标题颜色和尺寸
cboTextColor.BackColor = Toolkit.toColor(pLegend.Format.TitleSymbol.Color);
textBox2.Text = pLegend.Format.TitleSymbol.Font.Name + " " + pLegend.Format.TitleSymbol.Font.Size.ToString();
//更新边框间距
if (m_FrameProperties.Border == null)
{
numGap.Value = 0;
}
else
{
numGap.Value = (decimal)m_FrameProperties.Border.Gap;
}
//更新布局尺寸
ILegendFormat pLegendFormat = pLegend.Format;
textHeadingGap.Text = pLegendFormat.HeadingGap.ToString(".0");
textGroupGap.Text = pLegendFormat.GroupGap.ToString(".0");
textHorizontalItemGap.Text = pLegendFormat.HorizontalItemGap.ToString(".0");
textLayerNameGap.Text = pLegendFormat.LayerNameGap.ToString(".0");
textTextGap.Text = pLegendFormat.TextGap.ToString(".0");
textVerticalItemGap.Text = pLegendFormat.VerticalItemGap.ToString(".0");
textHorizontalPatchGap.Text = pLegendFormat.HorizontalPatchGap.ToString(".0");
//更新自适应策略
FixedBox.Checked = pLegend.FixedFrame;
if (FixedBox.Checked == true)
{
fixeBoxSelect();
AutoColumn.Checked = pLegend.AutoColumns;
Shrink.Checked = pLegend.AutoFit;
TextSize.Value = (decimal)pLegend.MinFontSize;
}
}
/// <summary>
/// /创建图例
/// </summary>
/// <param name="MapSurroundFrame"></param>
/// <param name="List_Layer"></param>
/// <param name="Columns"></param>
/// <param name="Title"></param>
/// <param name="pLegendFormat"></param>
private void CreateLegend(IMapSurroundFrame MapSurroundFrame, List<string> List_Layer, int Columns, string Title, ILegendFormat pLegendFormat)
{
IGraphicsContainer pGraphicsContainer = PageLayoutControl.GraphicsContainer;
if (pGraphicsContainer != null)
{
IMapSurroundFrame pMapSurroundFrame = MapSurroundFrame;
if (pMapSurroundFrame != null)
{
IMapSurround pMapSurround = pMapSurroundFrame.MapSurround;
ILegend2 Legend = pMapSurround as ILegend2;
Legend.ClearItems();
//设置标题
Legend.Title = Title;
//设置样式
Legend.Format = pLegendFormat;
//添加指定图层
for (int i = 0; i < List_Layer.Count; i++)
{
ILayer layer = GetLayerByName(List_Layer[i]);
ILegendItem pItem = SetItemStyle(layer);
Legend.AddItem(pItem);
}
//设置图例列数
Legend.AdjustColumns(Columns);
//自适应策略
Legend.FixedFrame = FixedBox.Checked;
Legend.AutoColumns = AutoColumn.Checked;
Legend.AutoFit = Shrink.Checked;
Legend.MinFontSize = (double)TextSize.Value;
//设置边框样式
IFrameProperties pFrameProperties = pMapSurroundFrame as IFrameProperties;
pFrameProperties.Border = m_FrameProperties.Border;
pFrameProperties.Background = m_FrameProperties.Background;
pFrameProperties.Shadow = m_FrameProperties.Shadow;
pGraphicsContainer.UpdateElement(pMapSurroundFrame as IElement);
PageLayoutControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
else
{
MessageBox.Show("添加图例失败!", "提示", MessageBoxButtons.OK);
}
}
else
{
MessageBox.Show("添加图例失败!", "提示", MessageBoxButtons.OK);
}
}
/// <summary>
/// 设置图例
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
private ILegendItem SetItemStyle(ILayer layer)
{
ILegendItem pLegendItem = new HorizontalLegendItemClass();
pLegendItem.Layer = layer;
pLegendItem.ShowHeading = checkHeading.Checked ;
pLegendItem.ShowLayerName = checkLayerName.Checked;
pLegendItem.ShowLabels = checkLabel.Checked;
pLegendItem.ShowDescriptions = checkDescriptions.Checked;
TextSymbol(pLegendItem);
return pLegendItem;
}
/// <summary>
/// 遍历所有图层
/// </summary>
/// <param name="MapControl"></param>
/// <param name="listMapLayer"></param>
private void GetLayer(AxMapControl MapControl, ListBox listMapLayer)
{
for (int i = 0; i < MapControl.LayerCount; i++)
{
if (MapControl.get_Layer(i) is ICompositeLayer)
{
ICompositeLayer pCompositeLayer = MapControl.get_Layer(i) as ICompositeLayer;
Togetlayer(pCompositeLayer, listMapLayer);
}
else
{
listMapLayer.Items.Add(MapControl.get_Layer(i).Name);
}
}
}
/// <summary>
/// 遍历图层组下的所有图层
/// </summary>
/// <param name="pCompositeLayer"></param>
/// <param name="listMapLayer"></param>
private ILayer getlayer(ICompositeLayer pCompositeLayer, ILayer pLayer, string IN_LayerName)
{
for (int i = 0; i < pCompositeLayer.Count; i++)
{
if (pCompositeLayer.get_Layer(i) is ICompositeLayer)
{
ICompositeLayer CompositeLayer = pCompositeLayer.get_Layer(i) as ICompositeLayer;
pLayer = getlayer(CompositeLayer, pLayer, IN_LayerName);
if (pLayer != null)
{
break;
}
}
else if (pCompositeLayer.get_Layer(i).Name == IN_LayerName)
{
pLayer = pCompositeLayer.get_Layer(i);
break;
}
}
return pLayer;
}
/// <summary>
/// 根据图层名获取图层
/// </summary>
/// <param name="IN_LayerName"></param>
/// <returns></returns>
private ILayer GetLayerByName(string IN_LayerName)
{
ILayer pLayer = null;
for (int i = 0; i < MapControl.LayerCount; i++)
{
if (MapControl.get_Layer(i) is ICompositeLayer)
{
ICompositeLayer pCompositeLayer = MapControl.get_Layer(i) as ICompositeLayer;
pLayer = getlayer(pCompositeLayer, pLayer, IN_LayerName);
if(pLayer != null) break;
}
else if (MapControl.get_Layer(i).Name == IN_LayerName)
{
pLayer = MapControl.get_Layer(i);
break;
}
}
return pLayer;
}
/// <summary>
/// 遍历图层组下的所有图层
/// </summary>
/// <param name="pCompositeLayer"></param>
/// <param name="listMapLayer"></param>
private ILayer getlayer(ICompositeLayer pCompositeLayer, ILayer pLayer, string IN_LayerName)
{
for (int i = 0; i < pCompositeLayer.Count; i++)
{
if (pCompositeLayer.get_Layer(i) is ICompositeLayer)
{
ICompositeLayer CompositeLayer = pCompositeLayer.get_Layer(i) as ICompositeLayer;
getlayer(CompositeLayer, pLayer, IN_LayerName);
}
else if (pCompositeLayer.get_Layer(i).Name == IN_LayerName)
{
pLayer = pCompositeLayer.get_Layer(i);
break;
}
}
return pLayer;
}
/// <summary>
/// 更新图例
/// </summary>
private void UpdateLegend()
{
ProcessBar processBar = new ProcessBar();
processBar.Show();
processBar.Update();
//获取图层列表
List<string> List_Layer = new List<string>();
for (int i = 0; i < listLegend.Items.Count; i++)
{
string[] a = new string[listLegend.Items.Count];
a[i] = Convert.ToString(listLegend.Items[i]);
List_Layer.Add(a[i]);
}
//获取列数
int Columns = (int)nColumns.Value;
//获取图例标题
string Title = textTitle.Text;
//获取图例标题样式
ITextSymbol symbolTitle = pLegend.Format.TitleSymbol;
if (comboTextLable.Text == "应用至图例标题" || comboTextLable.Text == "应用至全部标注")
{
if (pFont_title != null)
{
symbolTitle.Font = pFont_title;
}
symbolTitle.Color = Toolkit.toColor(cboTextColor.BackColor);
}
//图例对齐方式
if (checkLeft.Checked)
{
symbolTitle.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;
}
if (checkCenter.Checked)
{
symbolTitle.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;
}
if (checkRight.Checked)
{
symbolTitle.HorizontalAlignment = esriTextHorizontalAlignment.esriTHARight;
}
if (checkNothing.Checked)
{
symbolTitle.HorizontalAlignment = esriTextHorizontalAlignment.esriTHAFull;
}
//获取边框间距
if (m_FrameProperties.Border != null)
{
IBorder pBorder = m_FrameProperties.Border;
pBorder.Gap = (double)numGap.Value;
m_FrameProperties.Border = pBorder;
}
//同步字体格式
mLegendItem = pLegend.get_Item(0);
ILegendFormat pLegendFormat = pLegend.Format;
//设置标题样式
pLegendFormat.ShowTitle = checkTitleVisible.Checked;
pLegendFormat.TitleSymbol = symbolTitle;
if (IsNumbericA(textHeadingGap.Text) && IsNumbericA(textGroupGap.Text) && IsNumbericA(textHorizontalItemGap.Text) && IsNumbericA(textTextGap.Text) &&
IsNumbericA(textLayerNameGap.Text) && IsNumbericA(textVerticalItemGap.Text) && IsNumbericA(textHorizontalPatchGap.Text))
{
//标题和图例项之间的垂直距离
pLegendFormat.HeadingGap = Convert.ToDouble(textHeadingGap.Text);
//图例项之间的垂直距离
pLegendFormat.GroupGap = Convert.ToDouble(textGroupGap.Text);
//图例项列之间的水平距离
pLegendFormat.HorizontalItemGap = Convert.ToDouble(textHorizontalItemGap.Text);
//图层名称和图形之间的垂直距离
pLegendFormat.LayerNameGap = Convert.ToDouble(textLayerNameGap.Text);
//标签和描述之间的水平距离
pLegendFormat.TextGap = Convert.ToDouble(textTextGap.Text);
//图例项目之间的垂直距离
pLegendFormat.VerticalItemGap = Convert.ToDouble(textVerticalItemGap.Text);
//图面和标注之间的水平距离
pLegendFormat.HorizontalPatchGap = Convert.ToDouble(textHorizontalPatchGap.Text);
CreateLegend(pMapSurroundFrame, List_Layer, Columns, Title, pLegendFormat);
processBar.Close();
}
else
{
MessageBox.Show("请输入正确的数值!", "提示");
}
}
/// <summary>
/// 应用文本样式
/// </summary>
/// <param name="pLegendItem"></param>
/// <returns></returns>
private ILegendItem TextSymbol(ILegendItem pLegendItem)
{
string TextLabel = comboTextLable.Text;
ITextSymbol textSymbol = new TextSymbolClass();
textSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;
if (pFont_title != null)
{
textSymbol.Font = pFont_title;
}
textSymbol.Color = Toolkit.toColor(cboTextColor.BackColor);
pLegendItem.LayerNameSymbol = mLegendItem.LayerNameSymbol;
pLegendItem.HeadingSymbol = mLegendItem.HeadingSymbol;
pLegendItem.LegendClassFormat.LabelSymbol = mLegendItem.LegendClassFormat.LabelSymbol;
pLegendItem.LegendClassFormat.DescriptionSymbol = mLegendItem.LegendClassFormat.DescriptionSymbol;
if (TextLabel == "应用至图层名称" || TextLabel == "应用至全部标注")
{
pLegendItem.LayerNameSymbol = textSymbol;
}
if (TextLabel == "应用至类标题" || TextLabel == "应用至全部标注")
{
pLegendItem.HeadingSymbol = textSymbol;
}
if (TextLabel == "应用至类标注" || TextLabel == "应用至全部标注")
{
pLegendItem.LegendClassFormat.LabelSymbol = textSymbol;
}
if (TextLabel == "应用至类描述" || TextLabel == "应用至全部标注")
{
pLegendItem.LegendClassFormat.DescriptionSymbol = textSymbol;
}
return pLegendItem;
}
private void simpleOK_Click(object sender, EventArgs e)
{
UpdateLegend();
this.Close();
}
private void simpleCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void simpleApply_Click(object sender, EventArgs e)
{
UpdateLegend();
}
private void simpleAdd_Click(object sender, EventArgs e)
{
object Item = listMapLayer.SelectedItem;
listLegend.Items.Add(Item);
listLegend.SetSelected(0, true);
listLegend_ValueChanged();
}
private void simpleAddAll_Click(object sender, EventArgs e)
{
listLegend.Items.AddRange(listMapLayer.Items);
listLegend.SetSelected(0, true);
listLegend_ValueChanged();
}
private void simpleRemove_Click(object sender, EventArgs e)
{
if (listLegend.Items.Count != 0)
{
object Item = listLegend.SelectedItem;
listLegend.Items.Remove(Item);
if (listLegend.Items.Count != 0)
{
listLegend.SetSelected(0, true);
}
listLegend_ValueChanged();
}
}
private void simpleRemoveAll_Click(object sender, EventArgs e)
{
if (listLegend.Items.Count != 0)
{
listLegend.Items.Clear();
listLegend_ValueChanged();
}
}
private void simpleUp_Click(object sender, EventArgs e)
{
object Item = listLegend.SelectedItem;
int i = listLegend.SelectedIndex;
if (i != 0)
{
listLegend.Items.Insert(i - 1, Item);
listLegend.Items.RemoveAt(i + 1);
listLegend.SetSelected(i - 1, true);
}
}
private void simpleDown_Click(object sender, EventArgs e)
{
object Item = listLegend.SelectedItem;
int i = listLegend.SelectedIndex;
if (i != listLegend.Items.Count - 1)
{
listLegend.Items.Insert(i + 2, Item);
listLegend.Items.RemoveAt(i);
listLegend.SetSelected(i + 1, true);
}
}
private void listLegend_ValueChanged()
{
if (listLegend.Items.Count != 0)
{
simpleRemove.Enabled = true;
simpleRemoveAll.Enabled = true;
simpleUp.Enabled = true;
simpleDown.Enabled = true;
}
else
{
simpleRemove.Enabled = false;
simpleRemoveAll.Enabled = false;
simpleUp.Enabled = false;
simpleDown.Enabled = false;
}
}
private void cboTextColor_Click(object sender, EventArgs e)
{
cboTextColor.BackColor = chooseColor2(cboTextColor.BackColor.ToArgb());
}
private System.Drawing.Color chooseColor2(int color)
{
ColorDialog colorDialog = new ColorDialog();
colorDialog.AllowFullOpen = true;
colorDialog.CustomColors = new int[] { color };
colorDialog.ShowHelp = true;
DialogResult result = colorDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
return colorDialog.Color;
}
return System.Drawing.Color.FromArgb(color);
}
private void simpleTextStyle_Click(object sender, EventArgs e)
{
FontDialog fontDialog = new FontDialog();
if (fontDialog.ShowDialog() == DialogResult.OK)
{
System.Drawing.Font selectFont = fontDialog.Font;
pFont_title = ESRI.ArcGIS.ADF.COMSupport.OLE.GetIFontDispFromFont(selectFont) as stdole.IFontDisp;
textBox2.Text = selectFont.Name + " " + selectFont.Size.ToString();
}
}
private void simpleBorder_Click(object sender, EventArgs e)
{
BorderForm pBorderForm = new BorderForm("Border");
pBorderForm.ShowDialog();
IBorder pBorder = pBorderForm.getFrameProperties().Border;
if (pBorder != null)
{
m_FrameProperties.Border = pBorder;
}
}
private void simpleBackground_Click(object sender, EventArgs e)
{
BorderForm pBorderForm = new BorderForm("Background");
pBorderForm.ShowDialog();
m_FrameProperties.Background = pBorderForm.getFrameProperties().Background;
}
private void simpleShadow_Click(object sender, EventArgs e)
{
BorderForm pBorderForm = new BorderForm("Shadow");
pBorderForm.ShowDialog();
m_FrameProperties.Shadow = pBorderForm.getFrameProperties().Shadow;
}
private void checkLeft_Click(object sender, EventArgs e)
{
checkCenter.Checked = false;
checkRight.Checked = false;
checkNothing.Checked = false;
}
private void checkCenter_Click(object sender, EventArgs e)
{
checkLeft.Checked = false;
checkRight.Checked = false;
checkNothing.Checked = false;
}
private void checkRight_Click(object sender, EventArgs e)
{
checkLeft.Checked = false;
checkCenter.Checked = false;
checkNothing.Checked = false;
}
private void checkNothing_Click(object sender, EventArgs e)
{
checkLeft.Checked = false;
checkCenter.Checked = false;
checkRight.Checked = false;
}
private void FixedBox_Click(object sender, EventArgs e)
{
fixeBoxSelect();
}
private void fixeBoxSelect()
{
if (FixedBox.Checked == true)
{
AutoColumn.Enabled = true;
Shrink.Enabled = true;
TextSizeName.Enabled = true;
TextSize.Enabled = true;
}
else
{
AutoColumn.Enabled = false;
Shrink.Enabled = false;
TextSizeName.Enabled = false;
TextSize.Enabled = false;
}
}
#endregion
#region 通用函数
/// <summary>
/// 是否大于0的数字
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
private bool IsNumbericA(string v)
{
return (this.IsFloatA(v));
}
/// <summary>
/// 是否正浮点数
/// </summary>
/// <param name="v"></param>
/// <returns></returns>
private bool IsFloatA(string v)
{
string pattern = @"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$";
Regex reg = new Regex(pattern);
return reg.IsMatch(v);
}
#endregion