收录日期:2020/11/26 23:57:39 时间:2010-09-07 20:29:42 标签:c#,winforms,mouseevent

This question seems to point to the existence of a windows event for a double-right-click. How to implement it in a C# windows form, however, is less than clear.

What's the best way to implement double-right-click on a control such as a button?

(I'm thinking I must use MouseDown and keep track of the time between clicks. Is there a better way?)

Override the WndProc function and listen for WM_RBUTTONDBLCLK, which as can be seen on this pinvoke page is 0x0206.

That pinvoke page also has some C# sample code for how to do it.

Whenever you see something about a windows message and/or windows API and you want to use it in C#, the pinvoke site is a good place to start looking.

Override Control.WndProc, and handle the WM_RBUTTONDBLCLK message manually.

MouseEventArgs contain property 'Button' that indicate wich button was clicked. So you can simply check this:

    private void MouseDoubleClickEventHandler(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            DoSomthing();
        } else if (e.Button == MouseButtons.Right)
        {
            DoSomethingElse();
        }
    }

I was able to implement this by inheriting from a button and overriding WndProc as ho1 and Reed suggested. Here's the inherited button:

public class RButton : Button
{
    public delegate void MouseDoubleRightClick(object sender, MouseEventArgs e);
    public event MouseDoubleRightClick DoubleRightClick;
    protected override void WndProc(ref Message m)
    {
        const Int32 WM_RBUTTONDBLCLK = 0x0206;
        if (m.Msg == WM_RBUTTONDBLCLK)
            DoubleRightClick(this, null);
        base.WndProc(ref m);
    }
}

I added the button programatically to the form and subscribed to its new DoubleRightClick event. I'm not sure how exactly to generate the appropriate MouseEventArgs but since this is just a test case, it's not important.