昨天写了一半突然断电,没有存盘,只有重写了,郁闷啊!
和zoomout、zoomin等工具不同,全图工具不用和地图交互,属于命令类工具,只要实现ICommand接口即可。参考zoomout的实现,首先新建一个基类CommandBase,实现Icommand接口,然后新建FullExtent类,继承这个基类,重写一下OnClick方法,即可。
1: public class CommandBase:BaseCommand,IDisposable
2: {
3: protected IHookHelper m_pHookHelper;
4:
5: protected object m_pControl;
6: protected IActiveView m_pActiveView;
7: protected IMap m_pMap;
8:
9: /// <summary>
10: ///
11: /// </summary>
12: /// <param name="str">工具的名称</param>
13: public CommandBase(string str):base()
14: {
15: m_pHookHelper=new HookHelperClass();
16: InitialToolRes(str);
17: }
18: /// <summary>
19: ///
20: /// </summary>
21: /// <param name="mapCtl">工具关联的地图控件</param>
22: /// <param name="str"></param>
23: public CommandBase(AxMapControl mapCtl, string str)
24: :this(str)
25: {
26: OnCreate(mapCtl.Object);
27: }
28:
29: /// <summary>
30: ///
31: /// </summary>
32: /// <param name="plCtl">工具关联的地图控件</param>
33: /// <param name="str"></param>
34: public CommandBase(AxPageLayoutControl plCtl, string str)
35: : this(str)
36: {
37: OnCreate(plCtl.Object);
38: }
39:
40:
41: public override void OnCreate(object hook)
42: {
43: m_pHookHelper.Hook=hook;
44:
45: if (hook is Control)
46: {
47: m_pControl = hook;
48: if (hook is AxMapControl)
49: {
50: AxMapControl mapcontrl = hook as AxMapControl;
51: m_pHookHelper.Hook = mapcontrl.Object;
52: }
53: else if (hook is AxPageLayoutControl)
54: {
55: AxPageLayoutControl pagepcontrl = hook as AxPageLayoutControl;
56: m_pHookHelper.Hook = pagepcontrl.Object;
57: }
58: }
59: else
60: {
61: if (hook is IMapControlDefault)
62: {
63: IMapControlDefault pMapDefault = hook as IMapControlDefault;
64: m_pControl = Control.FromChildHandle(new IntPtr(pMapDefault.hWnd));
65: m_pHookHelper.Hook = pMapDefault;
66: }
67: else if (hook is IPageLayoutControlDefault)
68: {
69: IPageLayoutControlDefault pPageDefault = hook as IPageLayoutControlDefault;
70: m_pControl = Control.FromChildHandle(new IntPtr(pPageDefault.hWnd));
71: m_pHookHelper.Hook = pPageDefault;
72: }
73: }
74:
75: m_pActiveView = m_pHookHelper.ActiveView;
76: m_pMap = m_pHookHelper.FocusMap;
77: }
78:
79:
80: //初始化该功能的GUI资源
81: private void InitialToolRes(string str)
82: {
83: ResourceManager pResourceManager = Resources.ResourceManager;
84: pResourceManager.IgnoreCase=true;
85: base.m_bitmap=(Bitmap)pResourceManager.GetObject("bmp"+str);
86: base.m_name = (string)pResourceManager.GetObject("na" + str);
87: base.m_caption = (string)pResourceManager.GetObject("cpt" + str);
88: if (m_caption == string.Empty) m_caption = m_name;
89:
90: base.m_category=(string)pResourceManager.GetObject("ctg"+str);
91:
92: base.m_message=(string)pResourceManager.GetObject("msg"+str);
93: if (m_message == string.Empty) m_message = m_name;
94:
95: base.m_toolTip=(string)pResourceManager.GetObject("tlt"+str);
96: if (m_toolTip == string.Empty) m_toolTip = m_name;
97:
98: pResourceManager.ReleaseAllResources();
99: }
100:
101:
102:
103: #region IDisposable 成员
104:
105: public void Dispose()
106: {
107: m_pHookHelper = null;
108: m_bitmap = null;
109: Marshal.ReleaseComObject(m_pHookHelper);
110: }
111:
112: #endregion
113: }
FullExtent类的代码如下,
1: public class FullExtent : CommandBase
2: {
3: public FullExtent()
4: : base("FullExtent")
5: { }
6: public FullExtent(AxMapControl mapCtl)
7: : base(mapCtl, "FullExtent")
8: { }
9: public FullExtent(AxPageLayoutControl plCtl)
10: : base(plCtl, "FullExtent")
11: { }
12:
13: public override void OnClick()
14: {
15: m_pActiveView.Extent = m_pActiveView.FullExtent;
16: m_pActiveView.Refresh();
17: }
18: }