WinForms 禁用双击并接受所有鼠标单击?

发布于 2024-12-22 10:06:41 字数 74 浏览 1 评论 0原文

如何获得所有点击来完成该活动?我注意到,如果您单击太快,它会认为您正在双击,并且不会将单击发送到事件处理程序。有没有办法获得所有点击?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人疚 2024-12-29 10:06:41

不确定为什么这个问题得到了赏金,接受的答案应该已经非常接近解决方案了。除了您应该使用 MouseUp 而不是 MouseDown 之外,您的用户通常希望在释放按钮时单击操作生效。它提供了退出“哎呀,不是要单击它,移动鼠标以便它被忽略”选项。

不过,对于内置 Winforms 控件(例如 PictureBox),可以使用 Control.SetStyle() 方法进行配置。将新类添加到您的项目中并粘贴下面所示的代码。编译。从工具箱顶部删除新控件:

using System;
using System.Windows.Forms;

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

但是请注意,这对于包装现有本机 Windows 控件的 .NET 类不起作用。像 TextBox、ListBox、TreeView 等。其底层配置是 WNDCLASSEX.style 成员,CS_DBLCLKS 样式标志。设置该样式标志的代码已内置到 Windows 中并且无法更改。您需要进行不同类型的手术才能将双击恢复为单击。您可以通过重写 WndProc() 方法来实现此目的,我将给出 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 让它按照你想要的方式工作从来不需要太多代码,只需要 Petzold :)

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.

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

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

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 :)

轻拂→两袖风尘 2024-12-29 10:06:41

您想要使用控件的 MouseDown 事件而不是 Click 事件。每次在该控件上“按下”鼠标时,MouseDown 都会被调用。如果系统认为这是双击,则可能不会调用 Click。相反,将引发 DoubleClick 事件。

You want to use the controls MouseDown event instead of the Click event. MouseDown will be called every single time the mouse is "pressed" on that control. Click may not get called if the system thinks it was a double click. A DoubleClick event would be raised instead.

[浮城] 2024-12-29 10:06:41

我想你想计算所有点击。
如果您想对点击进行计数,请设置一个计数器变量并在点击事件中增加它。
也许这对你有帮助......

I think you want to count all click.
If you want to count click then set one counter variable and increase it in click event.
May be this help to you....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文