Search code examples
c#winformsinteropwndprocwm-touch

c# WM_TOUCH Messages in WndProc and PreFilterMessage


I'm experimenting with WM_TOUCH messages to capture touch events in my application. I want to register for example a "3 Finger Swipe" gesture and because that was not given in the WM_GESTURE I started experimenting with WM_TOUCH. I found this example http://msdn.microsoft.com/en-us/library/windows/desktop/dd940546%28v=vs.85%29.aspx The problem with the example is, that they use WndProc which only works for individual forms. I want to catch the touches in my whole application, so I tried to use PreFilterMessage instead of WndProc.

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public bool PreFilterMessage(ref Message m)
    {
        // Decode and handle WM_TOUCH message.
        bool handled;
        switch (m.Msg)
        {
            case WM_TOUCH:
                Debug.WriteLine("PreFilter TOUCH: " + m.ToString());
                handled = DecodeTouch(ref m);
                break;
            case 0x201:
                Debug.WriteLine("PreFilter LEFTMOUSEDOWN: " + m.ToString());
                handled = false;
                break;
            default:
                handled = false;
                break;
        }
...

The Problem now is, that the LParam in PreFilterMessage is different from the LParam in WndProc which leads to problems when i try to call GetTouchInputInfo. Here is my Debug log:

PreFilter TOUCH: msg=0x240 hwnd=0x530598 wparam=0x1 lparam=0x3ff0573 result=0x0
WndProc TOUCH: msg=0x240 hwnd=0x530598 wparam=0x1 lparam=0xf170000 result=0x0

PreFilter LEFTMOUSEDOWN: msg=0x201 (WM_LBUTTONDOWN) hwnd=0x530598 wparam=0x1 lparam=0x14100c1 result=0x0
WndProc LEFTMOUSEDOWN: msg=0x201 (WM_LBUTTONDOWN) hwnd=0x530598 wparam=0x1 lparam=0x14100c1 result=0x0

Why are the LParam for the mouseinput the same, and for the touchinput different? How can I convert the LParam in PreFilterMessage so that I can call GetTouchInputInfo?


Solution

  • I ended up using a different approach since i could not get this to work. I used a transparent form as an overlay (like this) to get the correct Message. This way I got the right LParam. Also I could forward the Message to the window below the overlay by using CallWindowProc with a modified hWnd, if I need to.