[Guid("D096C0A0-9B80-4F4C-9FAF-F8095C10785C")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CalculateContourTask.ComOpenShapeFile")]
public sealed class ComOpenShapeFile : BaseCommand
[ComVisible(false)]
static void RegisterFunction(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Register(regKey);
[ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Unregister(regKey);
}
IMapControl2 m_MapControl;
public ComOpenShapeFile()
{
string str = "打开地图文档";
base.m_category = str;
base.m_caption = str;
base.m_message = str;
base.m_toolTip = str;
base.m_name = str;
try
{
string strBmp = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), strBmp);
}
catch (System.Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
{
if (hook == null)
{
return;
}
if (hook is IToolbarControl)
{
IToolbarControl pToolBar = hook as IToolbarControl;
m_MapControl = pToolBar.Buddy as IMapControl2;
}else if (hook is IMapControl2)
{
m_MapControl = hook as IMapControl2;
}
{
string[] strShapeFile = AEOpenFile.OpenShapeFile(); //自定义打开文件函数
if (strShapeFile[0] == "")
{
return;
}
IMapDocument pMapDoc = new MapDocumentClass();
if (pMapDoc.get_IsPresent(strShapeFile[2]) && !pMapDoc.get_IsPasswordProtected(strShapeFile[2]))
{
m_MapControl.AddShapeFile(strShapeFile[0], strShapeFile[1]);
m_MapControl.ActiveView.Refresh();
pMapDoc.Close();
}
return;
}
[ClassInterface(ClassInterfaceType.None)]
[ProgId("CalculateContourTask.ComOpenShapeFile")]
public sealed class ComOpenShapeFile : BaseCommand
{
//注册
[ComRegisterFunction()][ComVisible(false)]
static void RegisterFunction(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Register(regKey);
}
//注销
[ComUnregisterFunction()][ComVisible(false)]
static void UnregisterFunction(Type registerType)
{
string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
ControlsCommands.Unregister(regKey);
}
IMapControl2 m_MapControl;
public ComOpenShapeFile()
{
string str = "打开地图文档";
base.m_category = str;
base.m_caption = str;
base.m_message = str;
base.m_toolTip = str;
base.m_name = str;
try
{
string strBmp = GetType().Name + ".bmp";
base.m_bitmap = new Bitmap(GetType(), strBmp);
}
catch (System.Exception ex)
{
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
}
}
//重载创建命令
public override void OnCreate(object hook){
if (hook == null)
{
return;
}
if (hook is IToolbarControl)
{
IToolbarControl pToolBar = hook as IToolbarControl;
m_MapControl = pToolBar.Buddy as IMapControl2;
}else if (hook is IMapControl2)
{
m_MapControl = hook as IMapControl2;
}
}
//重载点击
public override void OnClick(){
string[] strShapeFile = AEOpenFile.OpenShapeFile(); //自定义打开文件函数
if (strShapeFile[0] == "")
{
return;
}
IMapDocument pMapDoc = new MapDocumentClass();
if (pMapDoc.get_IsPresent(strShapeFile[2]) && !pMapDoc.get_IsPasswordProtected(strShapeFile[2]))
{
m_MapControl.AddShapeFile(strShapeFile[0], strShapeFile[1]);
m_MapControl.ActiveView.Refresh();
pMapDoc.Close();
}
return;
}
}
结果如下: