环境:.NET框架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。 – 2009-11-19 16:35:12
我也不太清楚你是什么意思的“通过”。 – 2009-11-19 16:36:35
错误消息不正确,应该是MouseDown。 我的意思是“通过”是,当控件上发生某些事件时,我通过编程方式在控件的父级上引发它们。 – GentlemanCoder 2009-11-23 14:11:47