直通鼠标事件于母公司控制 [英] Pass-through mouse events to parent control

查看:119
本文介绍了直通鼠标事件于母公司控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:.NET框架2.0,VS 2008

Environment: .NET Framework 2.0, VS 2008.

我试图创造条件,通过一定的鼠标事件(特定.NET控件(标签,面板)的一个子类的MouseDown 的MouseMove 的MouseUp )到其父控件(或者到顶层的形式)。我可以通过在标准控件的实例,例如制造这些事件的处理程序做到这一点:

I am trying to create a subclass of certain .NET controls (label, panel) that will pass through certain mouse events (MouseDown, MouseMove, MouseUp) to its parent control (or alternatively to the top-level form). I can do this by creating handlers for these events in instances of the standard controls, e.g.:

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);
    }
}

我不能移动的事件处理程序到控制的一个子类,因为这提高父控件事件的方法受到保护,我没有父控件限定符:

I cannot move the event handler into a subclass of the control, because the methods that raise the events in the parent control are protected and I don't have a qualifier for the parent control:

通过类型的预选赛无法访问受保护的成员 System.Windows.Forms.Control.OnMouseDown(System.Windows.Forms.MouseEventArgs) System.Windows.Forms.Control的;预选赛的类型必须是 TheProject.NoCaptureLabel (或其衍生)。

Cannot access protected member System.Windows.Forms.Control.OnMouseDown(System.Windows.Forms.MouseEventArgs) via a qualifier of type System.Windows.Forms.Control; the qualifier must be of type TheProject.NoCaptureLabel (or derived from it).

我期待到覆盖在我的子类的控件的WndProc 方法,但希望有人可以给我一个清晰的解决方案。

I am looking into overriding the WndProc method of the control in my sub-class, but hopefully someone can give me a cleaner solution.

推荐答案

是的。很多搜索之后,我发现文章<一个href=\"http://www.vbaccelerator.com/home/NET/$c$c/Controls/Popup_Windows/Floating_Controls/article.asp\">\"Floating控制,工具提示式,它使用的WndProc WM_NCHITTEST 更改消息 HTTRANSPARENT ,使控制透明的鼠标事件。

Yes. After a lot of searching, I found the article "Floating Controls, tooltip-style", which uses WndProc to change the message from WM_NCHITTEST to HTTRANSPARENT, making the Control transparent to mouse events.

要实现这一目标,创建标签继承了控制和简单的添加下列code。

To achieve that, create a control inherited from Label and simply add the following code.

protected override void WndProc(ref Message m)
{
    const int WM_NCHITTEST = 0x0084;
    const int HTTRANSPARENT = (-1);

    if (m.Msg == WM_NCHITTEST)
    {
        m.Result = (IntPtr)HTTRANSPARENT;
    }
    else
    {
        base.WndProc(ref m);
    }
}

我已经在Visual Studio 2010与.NET Framework 4的客户端配置文件测试这一点。

I have tested this in Visual Studio 2010 with .NET Framework 4 Client Profile.

这篇关于直通鼠标事件于母公司控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆