{"id":100065,"date":"2018-10-26T07:00:00","date_gmt":"2018-10-26T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=100065"},"modified":"2019-03-13T00:22:45","modified_gmt":"2019-03-13T07:22:45","slug":"20181026-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20181026-00\/?p=100065\/","title":{"rendered":"How do I prevent users from using the mouse to drag the trackbar thumb to positions that aren&#8217;t multiples of five? Part 2: Nudging the thumb position"},"content":{"rendered":"<p>Last time, we investigated one approach to the problem of keeping a trackbar position on multiples of five: Just divide everything by five! (And then multiply by five when you read the value.) This works great, except that accessibility tools report the range as 0&ndash;20 in one-unit increments, even though you are presenting it to the user as a range of 0&ndash;100 in five-unit increments. <\/p>\n<p>So it&#8217;s back to the drawing board. This time, we&#8217;ll leave the range at 0&ndash;100 and manipulate the trackbar position in the <code>TRBN_<\/code><code>THUMB&shy;POS&shy;CHANGING<\/code> notification. <\/p>\n<p>Take our program from last time and make the following changes: <\/p>\n<pre>\n<font COLOR=\"blue\">#pragma comment(linker, \\\n    \"\\\"\/manifestdependency:type='win32' \\\n    name='Microsoft.Windows.Common-Controls' \\\n    version='6.0.0.0' \\\n    processorArchitecture='*' \\\n    publicKeyToken='6595b64144ccf1df' \\\n    language='*'\\\"\")<\/font>\n<\/pre>\n<p>This <code>#pragma<\/code> is <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">a quick way to enable version 6 of the common controls<\/a>. <\/p>\n<pre>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n g_hwndChild = CreateWindow(TRACKBAR_CLASS, TEXT(\"\"),\n    WS_CHILD | WS_VISIBLE <font COLOR=\"blue\">| TBS_NOTIFYBEFOREMOVE<\/font>,\n   0, 0, 100, 100,\n    hwnd, (HMENU)100, g_hinst, 0);\n\n  <font COLOR=\"blue\">SendMessage(g_hwndChild, TBM_SETLINESIZE, 0, 5);\n  SendMessage(g_hwndChild, TBM_SETPAGESIZE, 0, 20);<\/font>\n\n  return TRUE;\n}\n<\/pre>\n<p>The <code>TBS_<\/code><code>NOTIFY&shy;BEFORE&shy;MOVE<\/code> style enables the <code>TRBN_<\/code><code>THUMB&shy;POS&shy;CHANGING<\/code> notification, which we will take advantage of below. We leave the range at its default of 0&ndash;100 and set the line size and page size to the desired multiples of five. <\/p>\n<pre>\n<font COLOR=\"blue\">LRESULT OnNotify(HWND hwnd, int idCtl, NMHDR* pnm)\n{\n if (pnm-&gt;hwndFrom == g_hwndChild &amp;&amp;\n     pnm-&gt;code == TRBN_THUMBPOSCHANGING) {\n  auto ptpc = (NMTRBTHUMBPOSCHANGING*)pnm;\n  auto pos = ptpc-&gt;dwPos;\n  auto newpos = (pos + 2) \/ 5 * 5;\n  if (pos != newpos) {\n    SendMessage(pnm-&gt;hwndFrom, TBM_SETPOS, TRUE, newpos);\n    return TRUE; \/\/ we moved the thumb, so the control doesn't have to\n  }\n }\n return 0;\n}\n    HANDLE_MSG(hwnd, WM_NOTIFY, OnNotify);<\/font>\n<\/pre>\n<p>When we are being notified that the thumb is about to move, we take the proposed new position and round it to the nearest multiple of five. If that produces a value different from what the control would have done, then we manually set the thumb position to the desired multiple of five and return <code>TRUE<\/code> to tell the trackbar that it shouldn&#8217;t move the trackbar thumb (because we moved it). <\/p>\n<p>(And again, don&#8217;t forget that if this is happening in a dialog box, you need to use <code>DWLP_<\/code><code>MSG&shy;RESULT<\/code> to make the dialog box return a nonzero value from its window procedure.) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little nudging here, a little nudging there.<\/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-100065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A little nudging here, a little nudging there.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100065","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=100065"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/100065\/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=100065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=100065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=100065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}