C# 捕获 .NET TextBox 的 MouseDown 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Capture MouseDown event for .NET TextBox
提问by Adam Haile
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox control? I know the inherited Control class has the event, but it's not exposed in TextBox. Is there a way to override the event handler?
有没有办法从 .NET 2.0 TextBox 控件中捕获 MouseDown?我知道继承的 Control 类有这个事件,但它没有在 TextBox 中公开。有没有办法覆盖事件处理程序?
I also tried the OpenNETCF TextBox2 control which does have the MouseDown event exposed, but no matter what I do, it doesn't fire the handler.
我还尝试了 OpenNETCF TextBox2 控件,它确实公开了 MouseDown 事件,但无论我做什么,它都不会触发处理程序。
Any suggestions?
有什么建议?
What kind of crazy mobile device do you have that has a mouse? :)
你有什么带鼠标的疯狂移动设备?:)
Yes, windows mobile does not have an actual mouse, but you are mistaken that Windows Mobile .NET do not support the Mouse events. A click or move on the screen is still considered a "Mouse" event. It was done this way so that code could port over from full Windows easily. And this is not a Windows Mobile specific issue. The TextBox control on Windows does not have native mouse events either. I just happened to be using Windows Mobile in this case.
是的,windows mobile 没有实际的鼠标,但是您误认为Windows Mobile .NET 不支持鼠标事件。在屏幕上单击或移动仍被视为“鼠标”事件。这样做是为了让代码可以轻松地从完整的 Windows 移植过来。这不是 Windows Mobile 特定的问题。Windows 上的 TextBox 控件也没有本机鼠标事件。在这种情况下,我碰巧使用的是 Windows Mobile。
Edit: And on a side note...as Windows Mobile is built of the WindowsCE core which is often used for embedded desktop systems and Slim Terminal Services clients or "WinTerms" it has support for a hardware mouse and has for a long time. Most devices just don't have the ports to plug one in.
编辑:顺便提一下……由于 Windows Mobile 是由 WindowsCE 核心构建的,该核心通常用于嵌入式桌面系统和 Slim 终端服务客户端或“WinTerms”,它支持硬件鼠标并且已经有很长时间了。大多数设备只是没有端口可以插入。
According to the .Net Framework, the MouseDown Event Handler on a TextBox is supported. What happens when you try to run the code?
根据 .Net Framework,支持 TextBox 上的 MouseDown 事件处理程序。当您尝试运行代码时会发生什么?
Actually, that's only there because it inherits it from "Control", as does everyother Form control. It is, however, overridden (and changed to private I believe) in the TextBox class. So it will not show up in IntelliSense in Visual Studio.
实际上,这只是因为它从“Control”继承它,就像所有其他 Form 控件一样。但是,它在 TextBox 类中被覆盖(我相信已更改为私有)。因此它不会显示在 Visual Studio 的 IntelliSense 中。
However, you actually can write the code:
但是,您实际上可以编写代码:
textBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseDown);
and it will compile and run just fine, the only problem is that textBox1_MouseDown() will not be fired when you tap the TextBox control. I assume this is because of the Event being overridden internally. I don't even want to change what's happening on the event internally, I just want to add my own event handler to that event so I can fire some custom code as you could with any other event.
它会编译并运行得很好,唯一的问题是当您点击 TextBox 控件时不会触发 textBox1_MouseDown()。我认为这是因为事件在内部被覆盖。我什至不想在内部更改事件中发生的事情,我只想将我自己的事件处理程序添加到该事件,这样我就可以像处理任何其他事件一样触发一些自定义代码。
采纳答案by ageektrapped
Looks like you're right. Bummer. No MouseOver event.
看起来你是对的。无赖。没有鼠标悬停事件。
One of the fallbacks that always works with .NET, though, is P/Invoke. Someone already took the time to do this for the .NET CF TextBox. I found this on CodeProject:
但是,始终与 .NET 一起使用的后备之一是 P/Invoke。已经有人花时间为 .NET CF TextBox 做这件事了。我在 CodeProject 上找到了这个:
http://www.codeproject.com/KB/cs/TextBox_subclassing.aspx
http://www.codeproject.com/KB/cs/TextBox_subclassing.aspx
Hope this helps
希望这可以帮助
回答by GateKiller
According to the .Net Framework, the MouseDown Event Handler on a TextBoxis supported. What happens when you try to run the code?
根据 .Net Framework,支持TextBox 上的MouseDown 事件处理程序。当您尝试运行代码时会发生什么?
回答by ageektrapped
Fair enough. You probably know more than I do about Windows Mobile. :) I just started programming for it. But in regular WinForms, you can override the OnXxx event handler methods all you want. A quick look in Reflector with the CF shows that Control, TextBoxBase and TextBox don't prevent you from overriding the OnMouseDown event handler.
很公平。您可能比我更了解 Windows Mobile。:) 我刚开始为它编程。但是在常规的 WinForms 中,您可以随心所欲地覆盖 OnXxx 事件处理程序方法。使用 CF 快速查看 Reflector 会发现 Control、TextBoxBase 和 TextBox 不会阻止您覆盖 OnMouseDown 事件处理程序。
Have you tried this?:
你试过这个吗?:
public class MyTextBox : TextBox
{
public MyTextBox()
{
}
protected override void OnMouseDown(MouseEventArgs e)
{
//do something specific here
base.OnMouseDown(e);
}
}
回答by dalelane
is there an 'OnEnter' event that you could capture instead?
是否有您可以捕获的“OnEnter”事件?
it'd presumably also capture when you tab into the textbox as well as enter the text box by tapping/clicking on it, but if that isn't a problem, then this may be a more straightforward work-around
当您进入文本框以及通过点击/单击它来输入文本框时,它可能也会捕获,但如果这不是问题,那么这可能是一个更直接的解决方法
回答by Frank Razenberg
I know this answer is way late, but hopefully it ends up being useful for someone who finds this. Also, I didn't entirely come up with it myself. I believe I originally found most of the info on the OpenNETCF boards, but what is typed below is extracted from one of my applications.
我知道这个答案来得太晚了,但希望它最终对发现这个问题的人有用。此外,我自己并没有完全想出它。我相信我最初在 OpenNETCF 板上找到了大部分信息,但下面键入的内容是从我的一个应用程序中提取的。
You can get a mousedown event by implementing the OpenNETCF.Windows.Forms.IMessageFilter interface and attaching it to your application's message filter.
您可以通过实现 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()); } }
This is how you could implement the MouseUpDownFilter:
这是实现 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; } }
Now this MouseUpDownFilter will fire an MouseUp/MouseDown event when they occur on a watched control, for example your textbox. To use this filter you add some watched controls and assign to the events it might fire in your form's load event:
现在,当 MouseUp/MouseDown 事件发生在一个被监视的控件上时,这个 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 :) } }