设置按钮的图标,需要动态加载一张图片,程序如下:base.m_bitmap = new System.Drawing.Bitmap (GetType().Assembly.GetManifestResourceStream(GetType(), "zoomout.bmp"));
// 这行语句的意思是让程序从程序集清单中加载zoomout.bmp图片,其中Assembly可以理解为当前的工程所在的命名空间,GetType()我还不是很清楚,反正用toString()输出后就是你自定义按钮的类所在的路径
但 是调试时总会抛出异常“未处理的“System.ArgumentException”类型的异常出现在 system.drawing.dll 中。其他信息: “null”不是“stream”的有效值。”,显然GetTyp().Assembly.GetManifestResourceStream()方法 并没有找到需要加载的图片。查 了一下资料,发现有人提到Build Action" property should be set to " Embedded Resourse",并且Assembly.GetManifestResourceStream 是从当前程序集加载指定的清单资源,直接将图片加载到当前工程中是不能被加载的。应设置图片的属性为:生成操作-->嵌入的资源即可,还有一点就是,图片必须和你新建的按钮类在同一个命名空间中,否则这种方法加载不了,但可以用
Assembly asm = Assembly.GetExecutingAssembly();
Image image = new Bitmap(asm.GetManifestResourceStream(asm.GetName().Name + "." +mapname));类似的方法加载
剩下就没有别的了
以下是自定义放大工具
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geometry;
using System.Drawing;
namespace 自定义工具程序
{
public sealed class ZoomOut : BaseTool
{
IMapControl3 mapControl;
private IHookHelper m_HookHelper = new HookHelperClass();
public ZoomOut()
{
//Set command properties
base.m_caption = "Zoom Out";
base.m_toolTip = "Zoom Out";
base.m_message = "Zoom Out on the Display";
base.m_name = "Generic_ZoomOut";
base.m_bitmap = new System.Drawing.Bitmap
(GetType().Assembly.GetManifestResourceStream(GetType(), "zoomout.bmp"));
}
public override void OnCreate(object hook)
{
m_HookHelper.Hook = hook;
if (hook is IToolbarControl)
{
IToolbarControl toolbarControl = (IToolbarControl)hook;
mapControl = (IMapControl3)toolbarControl.Buddy;
}
}
{
get
{
if (m_HookHelper.FocusMap == null) return false;
return true;
}
}
public override void OnMouseDown(int Button, int Shift, int X, int Y)
{
//Track a rectangle on the active view
IEnvelope trackExtent = mapControl.TrackRectangle();
if (trackExtent == null) return;
//Get the current extent of the active view
IEnvelope currentExtent = mapControl.Extent;
IEnvelope newExtent = null;
//If the tracked envelope is empty
if (trackExtent.IsEmpty)
{
//Expand the current extent
newExtent = currentExtent;
newExtent.Expand(2.0, 2.0, true);
newExtent.CenterAt(mapControl.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y));
}
else
{
//Create coordinates for the new extent
double dWidth = currentExtent.Width * (currentExtent.Width /
trackExtent.Width);
double dHeight = currentExtent.Height * (currentExtent.Height /
trackExtent.Height);
double dXmin = currentExtent.XMin - ((trackExtent.XMin -
currentExtent.XMin) * (currentExtent.Width / trackExtent.Width));
double dYmin = currentExtent.YMin - ((trackExtent.YMin -
currentExtent.YMin) * (currentExtent.Height / trackExtent.Height));
double dXmax = (currentExtent.XMin - ((trackExtent.XMin -
currentExtent.XMin) * (currentExtent.Width / trackExtent.Width))) + dWidth;
double dYmax = (currentExtent.YMin - ((trackExtent.YMin -
currentExtent.YMin) * (currentExtent.Height / trackExtent.Height))) + dHeight;
//Set the extent coordinates
newExtent = new EnvelopeClass();
newExtent.PutCoords(dXmin, dYmin, dXmax, dYmax);
}
//Set the new extent
mapControl.Extent = newExtent;
}
}
}