在 Windows 窗体应用程序中捕获 MonthCalendar 控件的双击

发布于 2024-12-21 08:05:01 字数 117 浏览 4 评论 0原文

如何捕获 System.Windows.Forms.MonthCalendar 控件的双击事件?我尝试过使用 MouseDown 的 MouseEventArgs.Clicks 属性,但它始终为 1,即使我双击也是如此。

How do I capture a doubleclick event of the System.Windows.Forms.MonthCalendar control? I've tried using MouseDown's MouseEventArgs.Clicks property, but it is always 1, even if I doubleclick.

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

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

发布评论

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

评论(3

离笑几人歌 2024-12-28 08:05:01

请注意,MonthCalendar 在“属性”窗口中既不显示 DoubleClick 也不显示 MouseDoubleClick 事件。麻烦的明显迹象是,本机 Windows 控件会阻止生成这些事件。您可以通过观察 MouseDown 事件并测量单击之间的时间来合成您自己的。

将新类添加到您的项目中并粘贴下面所示的代码。编译。从工具箱顶部放下新控件。为 DoubleClickEx 事件编写一个事件处理程序。

using System;
using System.Windows.Forms;

class MyCalendar : MonthCalendar {
    public event EventHandler DoubleClickEx;

    public MyCalender() {
        lastClickTick = Environment.TickCount - SystemInformation.DoubleClickTime;
    }

    protected override void OnMouseDown(MouseEventArgs e) {
        int tick = Environment.TickCount;
        if (tick - lastClickTick <= SystemInformation.DoubleClickTime) {
            EventHandler handler = DoubleClickEx;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        else {
            base.OnMouseDown(e);
            lastClickTick = tick;
        }
    }

    private int lastClickTick;
}

Do note that MonthCalendar neither shows the DoubleClick nor the MouseDoubleClick event in the Property window. Sure sign of trouble, the native Windows control prevents those events from getting generated. You can synthesize your own by watching the MouseDown events and measuring the time between clicks.

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox. Write an event handler for the DoubleClickEx event.

using System;
using System.Windows.Forms;

class MyCalendar : MonthCalendar {
    public event EventHandler DoubleClickEx;

    public MyCalender() {
        lastClickTick = Environment.TickCount - SystemInformation.DoubleClickTime;
    }

    protected override void OnMouseDown(MouseEventArgs e) {
        int tick = Environment.TickCount;
        if (tick - lastClickTick <= SystemInformation.DoubleClickTime) {
            EventHandler handler = DoubleClickEx;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        else {
            base.OnMouseDown(e);
            lastClickTick = tick;
        }
    }

    private int lastClickTick;
}
囚你心 2024-12-28 08:05:01

您需要自己跟踪点击事件。您需要使用 DateSelected 事件来标记单击日期的时间,并使用 DateChanged 事件来“重置”时间跨度,这样您就不会将单击不同的日期算作双击。

注意:如果您使用鼠标按下事件,则会出现错误行为

无论单击什么,鼠标按下事件都会发生,例如单击日历的月/年等标题将仅注册与点击真实日期相同。因此使用 DateSelected 而不是鼠标按下事件。

private DateTime last_mouse_down = DateTime.Now;

private void monthCalendar_main_DateSelected(object sender, DateRangeEventArgs e)
{
    if ((DateTime.Now - last_mouse_down).TotalMilliseconds <= SystemInformation.DoubleClickTime)
    {
        // respond to double click
    }
    last_mouse_down = DateTime.Now;
}

private void monthCalendar_main_DateChanged(object sender, DateRangeEventArgs e)
{
    last_mouse_down = DateTime.Now.Subtract(new TimeSpan(1, 0, 0));
}

You'll need to keep track of the click events yourself. You need to use the DateSelected event to mark when a date is clicked, and the DateChanged event to 'reset' the time span so you don't count clicking different dates as a double click.

Note: if you use a mouse down event you will get buggy behavior

The mouse down event occurs no matter what is clicked, for example clicking on the month / year etc header of the Calendar will be registered just the same as clicking a real date. Hence the use of DateSelected instead of the mouse down event.

private DateTime last_mouse_down = DateTime.Now;

private void monthCalendar_main_DateSelected(object sender, DateRangeEventArgs e)
{
    if ((DateTime.Now - last_mouse_down).TotalMilliseconds <= SystemInformation.DoubleClickTime)
    {
        // respond to double click
    }
    last_mouse_down = DateTime.Now;
}

private void monthCalendar_main_DateChanged(object sender, DateRangeEventArgs e)
{
    last_mouse_down = DateTime.Now.Subtract(new TimeSpan(1, 0, 0));
}
萌梦深 2024-12-28 08:05:01

最好添加以下代码,否则如果您快速单击两个日期,您将获得该事件。

    protected override void OnDateChanged(DateRangeEventArgs drevent) {
        lastClickTick = Environment.TickCount - 2 * SystemInformation.DoubleClickTime;
        base.OnDateChanged(drevent);
    }

Best to add following code, otherwise if you click quickly on two dates you will have the event.

    protected override void OnDateChanged(DateRangeEventArgs drevent) {
        lastClickTick = Environment.TickCount - 2 * SystemInformation.DoubleClickTime;
        base.OnDateChanged(drevent);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文