JYelton
JYelton

Reputation: 36512

How do I implement a double-right-click for winforms?

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

Upvotes: 8

Views: 2988

Answers (4)

IamMan
IamMan

Reputation: 442

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

Upvotes: 2

JYelton
JYelton

Reputation: 36512

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.

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564413

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

Upvotes: 3

Hans Olsson
Hans Olsson

Reputation: 55009

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.

Upvotes: 5

Related Questions