{"id":95585,"date":"2017-02-27T07:00:00","date_gmt":"2017-02-27T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=95585"},"modified":"2019-03-13T01:07:08","modified_gmt":"2019-03-13T08:07:08","slug":"20170227-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170227-00\/?p=95585","title":{"rendered":"How do I disable the press-and-hold gesture for my window?"},"content":{"rendered":"<p>A customer had a program which responded to left mouse clicks, but they found that when used with a touch screen, when users touched the screen, the <code>WM_LBUTTON&shy;DOWN<\/code> message didn&#8217;t arrive until the users lifted their fingers from the screen. They wanted to know whether this was by design, and also whether there was a way to get the <code>WM_LBUTTON&shy;DOWN<\/code> message as soon as the finger touches the screen. <\/p>\n<p>Yes, this behavior is by design. The system is waiting to see whether the user is making a press-and-hold gesture. If so, then the touch events are converted to right-mouse-button messages (<code>WM_RBUTTON&shy;DOWN<\/code> and <code>WM_RBUTTON&shy;UP<\/code>). But if the finger does not remain in contact for a long time, then the touch events are converted to left-mouse-button messages (<code>WM_LBUTTON&shy;DOWN<\/code> and <code>WM_LBUTTON&shy;UP<\/code>). <\/p>\n<p>The customer&#8217;s program was targeting Windows&nbsp;7, so they were looking for solutions that would work on that platform. <\/p>\n<p>Take our <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20030723-00\/?p=43073\">scratch program<\/a> and add the following: <\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;strsafe.h&gt; \/\/ StringCchPrintf\n#include &lt;tpcshrd.h&gt; \/\/ WM_TABLET_QUERYSYSTEMGESTURESTATUS<\/font>\n\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n  <font COLOR=\"blue\">g_hwndChild = CreateWindow(TEXT(\"listbox\"), NULL,\n        LBS_HASSTRINGS | WS_CHILD | WS_VISIBLE | WS_VSCROLL,\n        0, 0, 0, 0, hwnd, NULL, g_hinst, 0);<\/font>\n  return TRUE;\n}\n\nvoid\nOnSize(HWND hwnd, UINT state, int cx, int cy)\n{\n    if (g_hwndChild) {\n        MoveWindow(g_hwndChild, 0, 0, cx, <font COLOR=\"blue\">cy\/2<\/font>, TRUE);\n    }\n}\n\n\nLRESULT CALLBACK\nWndProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)\n{\n  ...\n  <font COLOR=\"blue\">case WM_LBUTTONDOWN:\n  case WM_LBUTTONUP:\n  case WM_RBUTTONDOWN:\n  case WM_RBUTTONUP:\n  {\n    TCHAR buffer[80];\n    StringCchPrintf(buffer, 80, TEXT(\"%04x %d %d\"), uiMsg,\n                    GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));\n    ListBox_AddString(g_hwndChild, buffer);\n  }\n  break;\n\n  case WM_TABLET_QUERYSYSTEMGESTURESTATUS:\n    return TABLET_DISABLE_PRESSANDHOLD;<\/font>\n  ...\n}\n<\/pre>\n<p>Most of this code is just creating a logging window so we can see the message traffic. (Note that we divide <code>cy<\/code> by 2 in the <code>OnSize<\/code> function so that there is room at the bottom of the window for touch activity.) <\/p>\n<p>The interesting part is adding a handler for the <code>WM_TABLET_QUERY&shy;SYSTEM&shy;GESTURE&shy;STATUS<\/code> message and responding that we want to disable press-and-hold. <\/p>\n<p>This successfully disables the press-and-hold gesture on Tablet PC (remember that?), allowing the left-button messages to be generated immediately upon contact. But it doesn&#8217;t help for Windows 7 and above. For that, we need something else: <\/p>\n<pre>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n  g_hwndChild = CreateWindow(TEXT(\"listbox\"), NULL,\n        LBS_HASSTRINGS | WS_CHILD | WS_VISIBLE | WS_VSCROLL,\n        0, 0, 0, 0, hwnd, NULL, g_hinst, 0);\n\n  <font COLOR=\"blue\">GESTURECONFIG config;\n  config.dwID = 0;\n  config.dwWant = 0;\n  config.dwBlock = GC_ALLGESTURES;\n  SetGestureConfig(hwnd, 0, 1, &amp;config, sizeof(config));<\/font>\n\n  return TRUE;\n}\n<\/pre>\n<p>This time, we disable all gestures using <code>Set&shy;Gesture&shy;Config<\/code>. This takes care of Windows 7 and higher. <\/p>\n<p>So there are your options: There&#8217;s a &#8220;Windows XP and Windows Vista&#8221; solution, and there&#8217;s a &#8220;Windows 7 and higher&#8221; solution. Or you can just play it safe and use both. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Digging back into ancient history.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-95585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Digging back into ancient history.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=95585"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/95585\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=95585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=95585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=95585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}