捕获.NET TextBox的MouseDown事件

时间:2020-03-05 18:38:57  来源:igfitidea点击:

是否有任何方法可以从.NET 2.0 TextBox控件捕获MouseDown?
我知道继承的Control类具有该事件,但未在TextBox中公开。
有没有方法可以覆盖事件处理程序?

我还尝试了OpenNETCF TextBox2控件,该控件的确公开了MouseDown事件,但是无论我做什么,它都不会触发处理程序。

有什么建议?

What kind of crazy mobile device do
  you have that has a mouse? :)

是的,Windows Mobile没有实际的鼠标,但是我们误认为Windows Mobile .NET不支持Mouse事件。在屏幕上单击或者移动仍然被视为"鼠标"事件。这样做是为了使代码可以轻松地从完整的Windows移植过来。这不是Windows Mobile特定的问题。 Windows上的TextBox控件也没有本地鼠标事件。我只是碰巧在这种情况下使用Windows Mobile。

编辑:另外,由于Windows Mobile是基于WindowsCE内核构建的,因此经常用于嵌入式桌面系统和Slim Terminal Services客户端或者" WinTerms",它具有对硬件鼠标的支持,并且使用时间长。大多数设备都没有端口可以插入。

According to the .Net Framework, the
  MouseDown Event Handler on a TextBox
  is supported. What happens when you
  try to run the code?

实际上,那只是因为它像其他所有Form控件一样从" Control"继承了它。但是,它在TextBox类中被重写(并更改为私有)。因此它不会显示在Visual Studio的IntelliSense中。

但是,我们实际上可以编写以下代码:

textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);

并且它将编译并运行良好,唯一的问题是,当我们点击TextBox控件时,不会触发textBox1_MouseDown()。我认为这是因为该事件在内部被覆盖。我什至不想更改内部事件的发生,我只想向该事件添加自己的事件处理程序,以便可以像其他事件一样触发一些自定义代码。

解决方案

回答

根据.Net Framework,支持TextBox上的MouseDown事件处理程序。当我们尝试运行代码时会发生什么?

回答

很公平。我们可能比我对Windows Mobile了解的更多。 :)我刚刚开始为此编程。但是在常规WinForms中,我们可以覆盖所有所需的OnXxx事件处理程序方法。快速查看带有CF的Reflector,可以发现Control,TextBoxBase和TextBox不会阻止我们覆盖OnMouseDown事件处理程序。

我们尝试过吗?:

public class MyTextBox : TextBox
{
    public MyTextBox()
    {
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        //do something specific here
        base.OnMouseDown(e);
    }
}

回答

看起来你是对的。笨蛋没有MouseOver事件。

但是,始终可与.NET一起使用的后备功能之一是P / Invoke。有人已经花时间对.NET CF TextBox进行了此操作。我在CodeProject上找到了这个:

http://www.codeproject.com/KB/cs/TextBox_subclassing.aspx

希望这可以帮助

回答

是否有一个我们可以捕获的" OnEnter"事件?

它可能还会在我们单击文本框以及通过点击/单击文本框进入文本框时捕获,但是如果这不是问题,那么这可能是更直接的解决方法

回答

我知道这个答案已经很晚了,但是希望它最终对发现这个问题的人有用。另外,我本人并没有完全提出这个建议。我相信我最初在OpenNETCF板上找到了大多数信息,但是下面键入的内容是从我的一个应用程序中提取的。

我们可以通过实现OpenNETCF.Windows.Forms.IMessageFilter接口并将其添加到应用程序的消息过滤器来获取mousedown事件。

static class Program {
    public static MouseUpDownFilter mudFilter = new MouseUpDownfilter();
    public static void Main() {
        Application2.AddMessageFilter(mudFilter);
        Application2.Run(new MainForm());
    }
}

这是实现MouseUpDownFilter的方法:

public class MouseUpDownFilter : IMessageFilter {
    List ControlList = new List();

    public void WatchControl(Control buttonToWatch) {
        ControlList.Add(buttonToWatch);
    }

    public event MouseEventHandler MouseUp;
    public event MouseEventHandler MouseDown;

    public bool PreFilterMessage(ref Microsoft.WindowsCE.Forms.Message m) {
        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;

        // If the message code isn't one of the ones we're interested in
        // then we can stop here
        if (m.Msg != WM_LBUTTONDOWN && m.Msg != WM_LBUTTONDOWN) {
            return false;
        }

        //  see if the control is a watched button
        foreach (Control c in ControlList) {
            if (m.HWnd == c.Handle) {
                int i = (int)m.LParam;
                int x = i & 0xFFFF;
                int y = (i >> 16) & 0xFFFF;
                MouseEventArgs args = new MouseEventArgs(MouseButtons.Left, 1, x, y, 0);

                if (m.Msg == WM_LBUTTONDOWN)
                    MouseDown(c, args);
                else
                    MouseUp(c, args);

                // returning true means we've processed this message
                return true;
            }
        }
        return false;
    }
}

现在,当此MouseUpDownFilter出现在受监视的控件(例如文本框)上时,将触发MouseUp / MouseDown事件。要使用此过滤器,我们需要添加一些监视的控件并将其分配给表单加载事件中可能触发的事件:

private void MainForm_Load(object sender, EventArgs e) {
    Program.mudFilter.WatchControl(this.textBox1);
    Program.mudFilter.MouseDown += new MouseEventHandler(mudFilter_MouseDown);
    Program.mudFilter.MouseUp += new MouseEventHandler(mudFilter_MouseUp);
}

void mudFilter_MouseDown(object sender, MouseEventArgs e) {
    if (sender == textBox1) {
        // do what you want to do in the textBox1 mouse down event :)
    }

}