环境:.NET Framework 2.0,VS 2008。
我正在尝试创建某些.NET控件(标签,面板)的子类,它将通过某些鼠标事件(MouseDown,
MouseMove,
MouseUp)传递到其父控件(或者传递到顶层窗体)。我可以通过在标准控件实例中创建这些事件的处理程序来实现这一点,例如:
<code><code><code>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);
}
}
</code></code></code>
因为父控件中触发事件的方法是受保护的,我无法将事件处理程序移动到控件的子类中,且没有用于父控件的限定符:
无法通过类型限定符
System.Windows.Forms.Control
访问受保护的成员System.Windows.Forms.Control.OnMouseDown(System.Windows.Forms.MouseEventArgs)
; 限定符必须是类型TheProject.NoCaptureLabel
(或派生自该类型)。
我正在研究在子类中重写控件的
WndProc
方法,但希望有人能给我提供更简洁的解决方案。