[ArcEngine地图制图系列]—添加地图标题
添加地图标题的核心代码如下
private IHookHelper m_hookHelper = null;
private IPageLayoutControl2 axPageControl;
private TitleForm frmTitle; //设置标题样式的窗体
private string title = “”;
private System.Drawing.Font font ;
private System.Drawing.Color color ;
/// <summary>
/// Occurs when this command is clicked
/// </summary>
public override void OnClick()
{
// TODO: Add AddTitleCommand.OnClick implementation
IEnvelope pEnvelope = PageManager.GetEnvelopeInPage2(axPageControl, 0.5, 0.9, 2, 1);
if (pEnvelope.IsEmpty || pEnvelope == null || pEnvelope.Width == 0 || pEnvelope.Height == 0)
{
return;
}
else
{
if (frmTitle == null || frmTitle.IsDisposed == true)
{
frmTitle = new TitleForm();
frmTitle.GetTitleText += new TitleForm.GetTitleTextEventHandler(frmTitle_GetTitleText);
}
frmTitle.ShowDialog();
AddTitle(axPageControl.ActiveView, pEnvelope, this.title,this.font,this.color);
pEnvelope = null;
}
}
#endregion
//设置标题样式的委托函数
private void frmTitle_GetTitleText(ref string newName, ref System.Drawing.Font newFont, ref System.Drawing.Color newColor)
{
this.title = newName;
this.font = newFont;
this.color = newColor;
}
private void AddTitle(IActiveView pActiveView, IEnvelope pEnv,string myTitle, System.Drawing.Font myFont,System.Drawing.Color myColor)
{
try
{
IGraphicsContainer pGraphicsContainer = (IGraphicsContainer)axPageControl.PageLayout;
//设置文本格式
ITextSymbol pTextSymbol = new TextSymbolClass();
//StdFont myFont = new stdole.StdFontClass();
//myFont.Name = “宋体”;
pTextSymbol.Font = ESRI.ArcGIS.ADF.COMSupport.OLE.GetIFontDispFromFont(myFont) as IFontDisp; ;
//pTextSymbol.Size = pSize;
pTextSymbol.Color = ColorUtilty.GetRgbColor(myColor.R, myColor.G, myColor.B); ;
pTextSymbol.Angle = 0;
pTextSymbol.RightToLeft = false;//文本由左向右排列
pTextSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVACenter;//垂直方向基线对齐
pTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft;//文本两端对齐
pTextSymbol.Text = myTitle;
ITextElement pTextElement = new TextElementClass();
pTextElement.Symbol = pTextSymbol;
pTextElement.Text = pTextSymbol.Text;
IElement pElement = axPageControl.FindElementByName(“Title”);//获取PageLayout中的元素
if (pElement != null)
{
pGraphicsContainer.DeleteElement(pElement); //删除已经存在标题
}
IElementProperties pElePro = null;
pElement = (IElement)pTextElement;
pElement.Geometry = (IGeometry)pEnv.UpperLeft;
pElePro = pElement as IElementProperties;
pElePro.Name = “Title”;
pGraphicsContainer.AddElement(pElement, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
转载自:https://blog.csdn.net/zhangcjing/article/details/45641571