public override void OnClick()
{
IMapDocument newDocument;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "";
openFileDialog.Filter = "Map Documents (*.mxd;*.mxt)|*.mxd;*.mxt";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
newDocument = new MapDocumentClass();
newDocument.Open(filePath, "");
IPageLayout tempPageLayout = newDocument.PageLayout;
IPage tempPage = tempPageLayout.Page;
IPage currentPage = m_HookHelper.PageLayout.Page;
//替换页面尺寸和单位
currentPage.Units = tempPage.Units;
currentPage.Orientation = tempPage.Orientation;
double width;
double height;
tempPage.QuerySize(out width, out height);
currentPage.PutCustomSize(width, height);
//删除当前Layout中的所有elements
IMapFrame pMapFrame=null;
IGraphicsContainer graphicsContainer = m_HookHelper.ActiveView as IGraphicsContainer;
graphicsContainer.Reset();
IElement element = graphicsContainer.Next();
while (element != null)
{
if (element is IMapFrame)
{
pMapFrame = element as IMapFrame;
element = graphicsContainer.Next();
}
else
{
graphicsContainer.DeleteElement(element);
graphicsContainer.Reset();
element = graphicsContainer.Next();
}
}
//添加模板中的所有元素
IGraphicsContainer tempGraphicsContainer = tempPageLayout as IGraphicsContainer;
tempGraphicsContainer.Reset();
IArray pArray = new ArrayClass();
element = tempGraphicsContainer.Next();
while (element != null)
{
if (element is IMapFrame)
{
(pMapFrame as IElement).Geometry = element.Geometry;
pMapFrame.Map = m_HookHelper.FocusMap;
}
else
{
graphicsContainer.AddElement(element, 0);
}
element = tempGraphicsContainer.Next();
}
graphicsContainer.Reset();
m_HookHelper.ActiveView.Refresh();
}
}