How can I prevent the mouse from moving in response to touch input?

Raymond Chen

A customer had a program that responded to touch input, but they found that when the user touched the screen, the mouse jumped to the touch point. How can they prevent that?

What you can do is make your program WM_POINTER-aware: Process the various WM_POINTER messages directly, and don’t let them go to Def­Window­Proc. It is the Def­Window­Proc function that takes unprocessed pointer messages and turns them into equivalent mouse activity.

You can take our scratch program and make these changes:

    case WM_POINTERUP:
        auto pointerId = GET_POINTERID_WPARAM(wParam);
        POINTER_INPUT_TYPE type;
        if (GetPointerType(pointerId, &type) && type == PT_MOUSE) {
            return DefWindowProc(hwnd, uiMsg, wParam, lParam);
        /* here is where you process the pointer message directly */
        return 0;

This program checks whether the pointer message came from a mouse. If so, then it lets the message go through and be processed normally.¹ Otherwise, it handles the message. Or at least, it would handle the message once you replace that comment with code that processes the message.

The mapping between pointer messages and mouse messages is

Pointer Mouse

There are also corresponding nonclient pointer and mouse messages, but I’m going to let those be processed normally so you can use touch to drag the window by its title bar.

¹ Mouse messages by default don’t even come in as WM_POINTER messages, but you can change that with Enable­Mouse­In­Pointer.


Discussion is closed. Login to edit/delete existing comments.

  • aybe 0

    Hey Raymond, can you shed some light on these two quirks when using Office?

    First, open Excel, save a document in that Microsoft Excel location; you get “You can’t save here. Please choose another location.”. Why does that even exist?

    Second, if you click “Hide Folders” in that save dialog, you suddenly see more tags to assign to the document. Why is that not an obvious thing?


    • Mystery Man 0

      First, you’d better post this in an Excel forum, where you’re more likely to get a good answer. (Preferably, avoid a Microsoft-operated forum.)

      Second, no, you can’t save in the “Microsoft Excel” location. You have to browse into its nodes. If you don’t see any nodes, be sure to have signed in with your Microsoft 365 account. Then, verify if the locations are set up.

      Third, “tags” as you call them, appear both when folders are shown and when they are hidden. You just have to resize the Save As… window to see them. However, when the folders are shown, the focus must be the folders. After all, what’s the use of a button that says “Show folders” but doesn’t do such a thing? You can change tags elsewhere in Excel, i.e., in the Info tab of the backstage screen.

  • Jan Ringoš 0

    How do WM_GESTURE and WM_TOUCH come into the picture?
    Which is translated into which or does third component generate them?

Feedback usabilla icon