【发布时间】:2010-10-07 12:36:25
【问题描述】:
环境:.NET Framework 2.0,VS 2008。
我正在尝试创建某些 .NET 控件(标签、面板)的子类,它将通过某些鼠标事件(MouseDown、MouseMove、MouseUp)传递到其父控件(或者传递到顶部)级形式)。我可以通过在标准控件的实例中为这些事件创建处理程序来做到这一点,例如:
public class TheForm : Form
{
private Label theLabel;
private void InitializeComponent()
{
theLabel = new Label();
theLabel.MouseDown += new MouseEventHandler(theLabel_MouseDown);
}
private void theLabel_MouseDown(object sender, MouseEventArgs e)
{
int xTrans = e.X + this.Location.X;
int yTrans = e.Y + this.Location.Y;
MouseEventArgs eTrans = new MouseEventArgs(e.Button, e.Clicks, xTrans, yTrans, e.Delta);
this.OnMouseDown(eTrans);
}
}
我无法将事件处理程序移动到控件的子类中,因为在父控件中引发事件的方法受到保护,并且我没有父控件的限定符:
无法通过
System.Windows.Forms.Control类型的限定符访问受保护成员System.Windows.Forms.Control.OnMouseDown(System.Windows.Forms.MouseEventArgs);限定符必须是TheProject.NoCaptureLabel类型(或派生自它)。
我正在考虑在我的子类中覆盖控件的WndProc 方法,但希望有人能给我一个更简洁的解决方案。
【问题讨论】:
-
这是此代码的正确错误消息吗?一个是 MouseUp,另一个是 MouseDown。
-
我也有点不清楚您所说的“通过”是什么意思。
-
报错信息不正确,应该是MouseDown。我所说的“通过”是指,当在控件上引发某些事件时,我会以编程方式在控件的父级上引发它们。
标签: c# winforms mouseevent