krish bala
krish bala

Reputation: 49

How to differentiate between touch and mouse in WndProc in C# WinForms?

We are working on providing touch resizing support for our control in WinForms and our requirement was to show the touch resizing PopUp when touching the app and closing the PopUp when an MouseUp or MouseDown event occurs.

We can differ the touch by using the constant value WM_GESTURE = 0x0119, but PointerUp and PointerDown are also becoming TRUE when touched and we're not able to differ the mouse touch from the other events.

Is there any way to specifically identify the mouse?

switch (m.Msg)
{                
    case WM_TOUCH:
        IsTouchEnabled = true;
        break;
    case WM_POINTERUP:
        IsTouchEnabled = false;
        break;
    case WM_POINTERDOWN:
        IsTouchEnabled = false;
        break;
 }

Thanks

Upvotes: 1

Views: 1011

Answers (1)

Selvamz
Selvamz

Reputation: 382

You can differentiate the touch down and mouse down by using GetMessageExtraInfo method like below.

    protected override void OnMouseDown(MouseEventArgs e)
    {
        Console.WriteLine("IsTouch: " + IsTouch());
        base.OnMouseDown(e);
    }

    public bool IsTouch()
    {
        uint extra = GetMessageExtraInfo();
        bool isTouchOrPen = ((extra & 0xFFFFFF00) == 0xFF515700);

        if (!isTouchOrPen)
            return false;

        bool isTouch = ((extra & 0x00000080) == 0x00000080);

        return isTouch;
    }

    [DllImport("user32.dll")]
    private static extern uint GetMessageExtraInfo();

Upvotes: 2

Related Questions