{"id":13963,"date":"2010-05-21T07:00:00","date_gmt":"2010-05-21T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/05\/21\/shautocomplete-giveth-and-shautocomplete-taketh-away\/"},"modified":"2010-05-21T07:00:00","modified_gmt":"2010-05-21T07:00:00","slug":"shautocomplete-giveth-and-shautocomplete-taketh-away","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100521-00\/?p=13963","title":{"rendered":"SHAutoComplete giveth, and SHAutoComplete taketh away"},"content":{"rendered":"<p>\nThe <code>SH&shy;Auto&shy;Complete<\/code> function lets you attach\nautocomplete functionality to an edit control,\nand there are flags that describe what sources you want\nthe autocomplete to draw from.\nIf you call <code>SH&shy;Auto&shy;Complete<\/code> a second time,\nthe second set of flags replace the original flags.\nThe flags do not accumulate.\nFor example, if you first call\n<code>SH&shy;Auto&shy;Complete(SHACF_FILESYS_ONLY)<\/code>, and then\nyou later call\n<code>SH&shy;Auto&shy;Complete(SHACF_URLHISTORY)<\/code>,\nthe result is that the autocompletion uses only the URL history.\n<\/p>\n<p>\nThis replacement behavior (as opposed to accumulation behavior)\nis handy if you want to <i>remove<\/i> an autocompletion that you\npreviously added.\nYou just call <code>SH&shy;Auto&shy;Complete<\/code> a second time\nand leave off the flags for autocomplete sources you don&#8217;t want.\nThere&#8217;s a catch, though:\nIf you want to turn off everything, then you cannot pass zero,\nbecause that gets interpreted as <code>SHACF_DEFAULT<\/code>.\nYou have to pass a nonzero value,\nand fortunately there&#8217;s a handy nonzero value which means\n<i>Turn off everything<\/i>:\n<code>SHACF_AUTOSUGGEST_FORCE_OFF<\/code>.\n<\/p>\n<p>\nLet&#8217;s illustrate this technique by disabling autocomplete\nin the common dialog, a problem which\ncommenter Ian mistakenly\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/12\/11\/9193695.aspx#9201524\">\nsolved by modifying a global setting<\/a>.\n<\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;shlwapi.h&gt;\n#include &lt;commctrl.h&gt;\n#include &lt;commdlg.h&gt;\n#include &lt;dlgs.h&gt;\nUINT_PTR CALLBACK HookProc(HWND hdlg, UINT uMsg,\n                           WPARAM wParam, LPARAM lParam)\n{\n    switch (uMsg) {\n    case WM_INITDIALOG:\n        PostMessage(hdlg, WM_APP, 0, 0);\n        break;\n    case WM_APP:\n        SHAutoComplete(\n          (HWND)SendDlgItemMessage(GetParent(hdlg), cmb13,\n                                   CBEM_GETEDITCONTROL, 0, 0),\n          SHACF_AUTOSUGGEST_FORCE_OFF);\n        break;\n    }\n    return 0;\n}\nint WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,\n                   LPSTR lpCmdLine, int nShowCmd)\n{\n    TCHAR szFile[MAX_PATH];\n    szFile[0] = TEXT('\\0');\n    OPENFILENAME ofn = { sizeof(ofn) };\n    ofn.hInstance = hinst;\n    ofn.lpstrFilter = TEXT(\"All files\\0*.*\\0\");\n    ofn.lpstrFile = szFile;\n    ofn.nMaxFile = MAX_PATH;\n    ofn.Flags = OFN_ENABLEHOOK | OFN_EXPLORER;\n    ofn.lpfnHook = HookProc;\n    GetOpenFileName(&amp;ofn);\n    return 0;\n}\n<\/pre>\n<p>\nThe hook procedure uses the <code>SH&shy;Auto&shy;Complete<\/code>\nfunction to turn off autocompletion on the file name\nedit control in the common dialog.\nThere are a few annoying bits that I have to get through\nbefore I finally make that <code>SH&shy;Auto&shy;Complete<\/code>\ncall:\nFirst I have to find the edit control,\nwhich means finding the combo box and then asking the combo box\nfor the interior edit control.\n(Fortunately, this is already called out in the documentation\nfor <code>SH&shy;Auto&shy;Complete<\/code>, so I didn&#8217;t have to puzzle over\nit for long.)\nAnd second,\nI couldn&#8217;t disable autocomplete directly in <code>WM_INITDIALOG<\/code>\nbecause that happens too early in the common file dialog\ninitialization process.\nInstead, I post myself a message and do the &#8220;final initialization&#8221;\nlater.\n(This I discovered by trial and error.)\n<\/p>\n<p>\nAnd there you have it, a common dialog box with no autocomplete.\n<\/p>\n<p>\n<b>Update<\/b>:\n<a HREF=\"http:\/\/www.deltics.co.nz\/blog\/\">\nJoylon Smith<\/a>\npoints out that\n<a HREF=\"http:\/\/www.deltics.co.nz\/blog\/?p=630\">\nthe documentation for <code>SHAutoComplete<\/code> explicitly\ncautions against calling it more than once on the same window<\/a>\nbecause it results in a memory leak.\n<\/p>\n<p>\nThat caution was written based on information I provided\nback in Windows&nbsp;XP.\nThe memory leak was fixed in Windows Vista, but the documentation\nwas not updated to match.\nSo please mentally insert\n&#8220;On versions of Windows prior to Windows Vista (and versions\nof Windows Server prior to Windows Server 2008)&#8221; at the start of\nthat paragraph.\nA doc change request has also been submitted, so hopefully the\nrevised documentation will appear soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SH&shy;Auto&shy;Complete function lets you attach autocomplete functionality to an edit control, and there are flags that describe what sources you want the autocomplete to draw from. If you call SH&shy;Auto&shy;Complete a second time, the second set of flags replace the original flags. The flags do not accumulate. For example, if you first call SH&shy;Auto&shy;Complete(SHACF_FILESYS_ONLY), [&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],"class_list":["post-13963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The SH&shy;Auto&shy;Complete function lets you attach autocomplete functionality to an edit control, and there are flags that describe what sources you want the autocomplete to draw from. If you call SH&shy;Auto&shy;Complete a second time, the second set of flags replace the original flags. The flags do not accumulate. For example, if you first call SH&shy;Auto&shy;Complete(SHACF_FILESYS_ONLY), [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13963","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=13963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13963\/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=13963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=13963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=13963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}