的WinForms禁用双击并接受所有的鼠标点击? [英] WinForms disable double clicks and accept all mouse clicks?

查看:116
本文介绍了的WinForms禁用双击并接受所有的鼠标点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何获得的所有点击要经过的事件吗?如果你点击太快它认为你是双击并不会发送点击到事件处理程序,我注意到。有没有办法让所有的点击?

How do you get all clicks to go through the event? I noticed if you click too fast it thinks you are double clicking and doesn't send the clicks to the event handler. Is there a way to get all the clicks?

推荐答案

这并不是说不清楚为什么这个问题得到了赏金,接受的答案应该是已经相当接近的解决方案。除了你应该使用的MouseUp,而不是的MouseDown,用户通常希望当他释放按钮点击动作即可生效。它提供了背出来哎呀,并不意味着点击它,所以它被忽略了移动鼠标选项。

Not that sure why this question got a bounty, the accepted answer ought to be already pretty close to a solution. Except that you ought to use MouseUp instead of MouseDown, your user typically expects a click action to take effect when he releases the button. Which provides the back-out "oops, didn't mean to click it, move the mouse so it gets ignored" option.

然而,对于内置的WinForms控制,像图片框,这是与Control.SetStyle()方法可配置的。添加一个新类到您的项目并粘贴如下所示的代码。编译。从工具箱的顶部掉落新的控制:

Nevertheless, for the built-in Winforms controls, like PictureBox, this is configurable with the Control.SetStyle() method. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox:

using System;
using System.Windows.Forms;

class MyPictureBox : PictureBox {
    public MyPictureBox() {
        this.SetStyle(ControlStyles.StandardDoubleClick, false);
    }
}



不过,一定要提防这不会对工作.NET类来封装现有的原生Windows的控制。像文本框,列表框,树视图等,为底层的配置是的 WNDCLASSEX.style成员,CS_DBLCLKS风格的标志。它设置了风格标志的代码被烤到Windows,不能更改。你需要不同的手术转向双击返回到一个单一的点击。您可以通过重写的WndProc()方法,这样做,我就给了文本框的例子:

Do beware however that this won't work for the .NET classes that wrap an existing native Windows control. Like TextBox, ListBox, TreeView, etc. The underlying configuration for that is the WNDCLASSEX.style member, CS_DBLCLKS style flag. The code that sets that style flag is baked into Windows and cannot be changed. You'd need different kind of surgery to turn a double-click back into a single click. You can do so by overriding the WndProc() method, I'll give an example for TextBox:

using System;
using System.Windows.Forms;

class MyTextBox : TextBox {
    protected override void WndProc(ref Message m) {
        // Change WM_LBUTTONDBLCLK to WM_LBUTTONCLICK
        if (m.Msg == 0x203) m.Msg = 0x201;
        base.WndProc(ref m);
    }
}



如果你想这样做

只需更改类名其他控件。征用的WinForms,使其工作你想永远的方式花费的代码,只要彼佐尔德:)

Just change the class name if you want to do it for other controls. Commandeering Winforms to make it work the way you want never takes much code, just Petzold :)

这篇关于的WinForms禁用双击并接受所有的鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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