小编Gen*_*der的帖子

将鼠标事件传递给父控件

环境:.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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法将事件处理程序移动到控件的子类中,因为引发父控件中的事件的方法受到保护,并且我没有父控件的限定符:

无法System.Windows.Forms.Control.OnMouseDown(System.Windows.Forms.MouseEventArgs)通过类型的限定符访问受保护的成员System.Windows.Forms.Control; 限定符必须是类型TheProject.NoCaptureLabel(或从中派生).

我正在研究覆盖WndProc我的子类中的控制方法,但希望有人可以给我一个更清洁的解决方案.

c# mouseevent winforms

32
推荐指数
2
解决办法
6万
查看次数

标签 统计

c# ×1

mouseevent ×1

winforms ×1