{"id":3413,"date":"2013-08-26T07:00:00","date_gmt":"2013-08-26T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/08\/26\/why-doesnt-the-automatically-move-pointer-to-the-default-button-in-a-dialog-box-work-for-nonstandard-dialog-boxes-and-how-do-i-add-it-to-my-own-nonstandard-dialog-boxes\/"},"modified":"2013-08-26T07:00:00","modified_gmt":"2013-08-26T07:00:00","slug":"why-doesnt-the-automatically-move-pointer-to-the-default-button-in-a-dialog-box-work-for-nonstandard-dialog-boxes-and-how-do-i-add-it-to-my-own-nonstandard-dialog-boxes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130826-00\/?p=3413\/","title":{"rendered":"Why doesn&#039;t the &#034;Automatically move pointer to the default button in a dialog box&#034; work for nonstandard dialog boxes, and how do I add it to my own nonstandard dialog boxes?"},"content":{"rendered":"<p>\nThe Mouse control panel has a setting called\n<i>Automatically move pointer to the default button in a dialog box<\/i>,\nknown informally as <i>Snap to default button<\/i>\nor simply <i>Snap To<\/i>.\nYou may have discovered that it doesn&#8217;t work for all dialog boxes.\nWhy not?\n<\/p>\n<p>\nThe <i>Snap To<\/i> feature\nis implemented by the dialog manager.\nWhen the window is shown and the setting is enabled,\nit will center the pointer on the default button.\nIf your application does not use the dialog manager\nbut instead creates its own custom dialog-like windows,\nthen naturally the code in the standard dialog manager will not run.\n<\/p>\n<p>\nIf you want your nonstandard dialog box to support the\n<i>Snap To<\/i> feature,\nyou get to implement it yourself.\n<\/p>\n<p>\nHere&#8217;s a Little Program that creates a window with a default pushbutton\ninside it,\nand which centers the mouse on the button when the window is shown.\nStart with our\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a>\nand make these changes:\n<\/p>\n<pre>\nPOINT GetRectCenter(LPCRECT prc)\n{\n  POINT pt = {\n    prc-&gt;left + (prc-&gt;right - prc-&gt;left) \/ 2,\n    prc-&gt;top + (prc-&gt;bottom - prc-&gt;top) \/ 2\n  };\n  return pt;\n}\n<\/pre>\n<p>\nThe\n<code>Get&shy;Rect&shy;Center<\/code> helper function\ncalculates center of a rectangle.\n<\/p>\n<pre>\nBOOL OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n  CreateWindow(TEXT(\"button\"),\n               TEXT(\"Button 1\"),\n               WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,\n               0, 0, 200, 50,\n               hwnd,\n               (HMENU)1,\n               g_hinst,\n               0);\n  return TRUE;\n}\n<\/pre>\n<p>\nWhen our main window is created,\nwe put a default button inside it.\n<\/p>\n<pre>\n#define WM_CHECKSNAPDEFBUTTON WM_APP\nvoid OnShowWindow(HWND hwnd, BOOL fShow, UINT status)\n{\n  if (fShow &amp;&amp; status == 0)\n  {\n    PostMessage(hwnd, WM_CHECKSNAPDEFBUTTON, 0, 0);\n  }\n}\nvoid OnCheckSnapDefButton(HWND hwnd)\n{\n  BOOL fSnapToDefButton;\n  if (SystemParametersInfo(SPI_GETSNAPTODEFBUTTON, 0,\n                           &amp;fSnapToDefButton, 0) &amp;&amp;\n                           fSnapToDefButton &amp;&amp;\n                           hwnd == GetForegroundWindow())\n  {\n    RECT rcButton;\n    GetWindowRect(GetDlgItem(hwnd, 1), &amp;rcButton);\n    POINT ptCenter = GetRectCenter(&amp;rcButton);\n    SetCursorPos(ptCenter.x, ptCenter.y);\n  }\n}\n<\/pre>\n<p>\nWhen the window is shown, we post a message to check the\n<i>Snap To<\/i> setting after things have settled down.\nOnce things settle down,\nwe check the <i>Snap To<\/i> setting,\nand if it&#8217;s enabled,\nand if our window is still the foreground window,\nthen we center the cursor on our button.\n<\/p>\n<p>\nIt&#8217;s important to check that our window is still the foreground\nwindow,\nbecause it would be rude to move the cursor to our button\neven if we opened in the background.\n<\/p>\n<p>\nThat&#8217;s why we need to post a message to perform the check later.\nThe\n<code>WM_SHOW&shy;WINDOW<\/code> message is sent\nearly in the <code>Show&shy;Window<\/code> calculations,\nbefore the activation actually changes.\nIf we performed the check then, the answer would always be,\n&#8220;No, you&#8217;re not the foreground window,&#8221;\nand we would always back off.\n<\/p>\n<p>\nNaturally, we need to hook up our new messages.\n<\/p>\n<pre>\n  HANDLE_MSG(hwnd, WM_SHOWWINDOW, OnShowWindow);\n  case WM_CHECKSNAPDEFBUTTON: OnCheckSnapDefButton(hwnd); break;\n<\/pre>\n<p>\nAnd there you have it,\na program that honors the\n<i>Automatically move pointer to the default button in a dialog box<\/i>\nsetting in its custom nonstandard dialog.\n<\/p>\n<p>\n<b>Exercise<\/b>:\nWhat assumptions are made about the rectangle\nby the <code>Get&shy;Rect&shy;Center<\/code> function?\nHow do they differ from the assumptions made by this\nalternate version:\n<\/p>\n<pre>\nPOINT GetRectCenter(LPCRECT prc)\n{\n  POINT pt = {\n    (prc-&gt;left + prc-&gt;right) \/ 2,\n    (prc-&gt;top + prc-&gt;bottom) \/ 2\n  };\n  return pt;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Mouse control panel has a setting called Automatically move pointer to the default button in a dialog box, known informally as Snap to default button or simply Snap To. You may have discovered that it doesn&#8217;t work for all dialog boxes. Why not? The Snap To feature is implemented by the dialog manager. When [&hellip;]<\/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,104],"class_list":["post-3413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code","tag-tipssupport"],"acf":[],"blog_post_summary":"<p>The Mouse control panel has a setting called Automatically move pointer to the default button in a dialog box, known informally as Snap to default button or simply Snap To. You may have discovered that it doesn&#8217;t work for all dialog boxes. Why not? The Snap To feature is implemented by the dialog manager. When [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3413","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=3413"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3413\/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=3413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}