我有几个自定义控件的实例,它们在 MouseMove 上引发自定义事件。这是代码: EventArgs 类:
public class GroupMoveEventArgs
{
public enum Action { CalcOffset, Move };
Action action;
int mouse_x;
int mouse_y;
// setters missed here
public GroupMoveEventArgs(GroupMoveEventArgs.Action action,
int mouse_x, int mouse_y)
{
this.action = action;
this.mouse_x = mouse_x;
this.mouse_y = mouse_y;
}
}
控制类:
public delegate void GroupMoveEventHandler(object sender, GroupMoveEventArgs e);
public event GroupMoveEventHandler GroupMoveEvent;
protected virtual void figureMouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
if (inGroup)
{ // raising the event
if (this.GroupMoveEvent != null)
GroupMoveEvent(this, new GroupMoveEventArgs(
GroupMoveEventArgs.Action.Move,
Parent.PointToClient(Control.MousePosition).X,
Parent.PointToClient(Control.MousePosition).Y));
}
}
}
protected virtual void OnGroupMoveEvent(object sender, GroupMoveEventArgs e)
{
Console.WriteLine("Got mouse move event " + this.num_of_points);
if (inGroup)
{
if (e.EventAction == GroupMoveEventArgs.Action.Move)
{
Location = new Point(e.MouseX - offset.X, e.MouseY - offset.Y);
}
else
if (e.EventAction == GroupMoveEventArgs.Action.CalcOffset)
{
Control c = sender as Control;
Point pnt = Parent.PointToClient(Control.MousePosition);
Point tmp = c.Location;
offset.X = pnt.X - tmp.X;
offset.Y = pnt.Y - tmp.Y;
}
}
}
问题是当我引发一个事件时,它只由发送者处理,而我需要所有控件来处理它。我怎样才能做到这一点?
在此先感谢,伊万。