{"id":38283,"date":"2004-08-02T07:00:00","date_gmt":"2004-08-02T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/08\/02\/how-to-set-focus-in-a-dialog-box\/"},"modified":"2004-08-02T07:00:00","modified_gmt":"2004-08-02T07:00:00","slug":"how-to-set-focus-in-a-dialog-box","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040802-00\/?p=38283","title":{"rendered":"How to set focus in a dialog box"},"content":{"rendered":"<p>\nSetting focus in a dialog box is more than just calling\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/userinput\/keyboardinput\/keyboardinputreference\/keyboardinputfunctions\/setfocus.asp\">\nSetFocus<\/a>.\n<\/p>\n<p>A dialog box maintains the concept of a &#8220;default button&#8221;\n(which is always a pushbutton).\nThe default button is typically drawn with a distinctive look\n(a heavy outline or a different color) and indicates what action\nthe dialog box will take when you hit Enter.\nNote that this is <strong>not<\/strong> the same as the control\nthat has the focus.\n<\/p>\n<p>\nFor example, open the Run dialog from the Start menu.\nObserve that the OK button is the default button;\nit has a different look from the other buttons.\nBut focus is on the edit control.\nYour typing goes to the edit control, until you hit Enter; the\nEnter activates the default button, which is OK.\n<\/p>\n<p>\nAs you tab through the dialog, observe what happens to the default\nbutton.  When the dialog box moves focus\nto a pushbutton, that pushbutton becomes\nthe new default button.\nBut when the dialog box moves focus\nto something that isn&#8217;t a pushbutton at all,\nthe OK button resumes its position as the default button.\n<\/p>\n<p>\nThe dialog manager remebers which control was the default button\nwhen the dialog was initially created, and when it moves focus\nto something that isn&#8217;t a button, it restores that original button\nas the default button.\n<\/p>\n<p>\nYou can ask a dialog box what the default button is by sending\nit\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/DialogBoxes\/DialogBoxReference\/DialogBoxMessages\/DM_GETDEFID.asp\">\nthe DM_GETDEFID message<\/a>;\nsimilarly, you can change it with\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/DialogBoxes\/DialogBoxReference\/DialogBoxMessages\/DM_SETDEFID.asp\">\nthe DM_SETDEFID message<\/a>.\n<\/p>\n<p>\n(Notice that the return value of the DM_GETDEFID message packs the\ncontrol ID in the low word and flags in the high word.\nAnother place where expanding dialog control IDs to 32-bit values\ndoesn&#8217;t buy you anything.)\n<\/p>\n<p>\nAs the remarks to the DM_SETDEFID function note, messing directly\nwith the default ID carelessly can lead to odd cases like a dialog box\nwith two default buttons.\nFortunately, you rarely need to change the default ID for a dialog.\n<\/p>\n<p>\nA bigger problem is using SetFocus to shove focus around a dialog.\nIf you do this, you are going directly to the window manager,\nbypassing the dialog manager.  This means that you can create\n&#8220;impossible&#8221; situations like having focus on a pushbutton without\nthat button being the default!\n<\/p>\n<p>\nTo avoid this problem, don&#8217;t use SetFocus to change focus on a dialog.\nInstead, use\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/DialogBoxes\/DialogBoxReference\/DialogBoxMessages\/WM_NEXTDLGCTL.asp\">\nthe WM_NEXTDLGCTL message<\/a>.\n<\/p>\n<pre>\nvoid SetDialogFocus(HWND hdlg, HWND hwndControl)\n{\n SendMessage(hdlg, WM_NEXTDLGCTL, (WPARAM)hwndControl, TRUE);\n}\n<\/pre>\n<p>\nAs the remarks for the WM_NEXTDLGCTL message observe,\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/DialogBoxes\/DialogBoxReference\/DialogBoxFunctions\/DefDlgProc.asp\">\nthe DefDlgProc function<\/a> handles the WM_NEXTDLGCTL message\nby updating all the internal dialog manager bookkeeping,\ndeciding which button should be default, all that good stuff.\n<\/p>\n<p>\nNow you can update dialog boxes like the professionals,\navoiding oddities like having no default button, or worse,\nmultiple default buttons!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting focus in a dialog box is more than just calling SetFocus. A dialog box maintains the concept of a &#8220;default button&#8221; (which is always a pushbutton). The default button is typically drawn with a distinctive look (a heavy outline or a different color) and indicates what action the dialog box will take when you [&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-38283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Setting focus in a dialog box is more than just calling SetFocus. A dialog box maintains the concept of a &#8220;default button&#8221; (which is always a pushbutton). The default button is typically drawn with a distinctive look (a heavy outline or a different color) and indicates what action the dialog box will take when you [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38283","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=38283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38283\/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=38283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=38283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=38283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}