{"id":34613,"date":"2005-08-11T10:00:08","date_gmt":"2005-08-11T10:00:08","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/08\/11\/adding-a-lookup-control-to-the-dictionary-just-getting-it-on-the-screen\/"},"modified":"2005-08-11T10:00:08","modified_gmt":"2005-08-11T10:00:08","slug":"adding-a-lookup-control-to-the-dictionary-just-getting-it-on-the-screen","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050811-08\/?p=34613","title":{"rendered":"Adding a lookup control to the dictionary: Just getting it on the screen"},"content":{"rendered":"<p><P>\nWhen we last left the dictionary project,\nwe were able to display the dictionary entries\nbut hadn&#8217;t yet gotten around to searching it.\nToday, we&#8217;ll place the lookup control, though we won&#8217;t\nhook it up until next time.\n<\/P>\n<P>\nFirst, we give the edit control an ID and create some member variables\nto keep track of it.\n<\/P>\n<PRE>\nclass RootWindow : public Window\n{\n &#8230;\n enum {\n  IDC_LIST = 1,\n  <FONT COLOR=\"blue\">IDC_EDIT = 2,<\/FONT>\n };\n &#8230;\nprivate:\n HWND m_hwndLV;\n <FONT COLOR=\"blue\">HWND m_hwndEdit;\n int  m_cyEdit;<\/FONT>\n COLORREF m_clrTextNormal;\n Dictionary m_dict;\n};\n<\/PRE>\n<P>\nOf course, we need to create the edit control, too.\n<\/P>\n<PRE>\nLRESULT RootWindow::OnCreate()\n{\n &#8230;\n ListView_SetItemCount(m_hwndLV, m_dict.Length());<\/p>\n<p> <FONT COLOR=\"blue\">m_hwndEdit = CreateWindow(TEXT(&#8220;edit&#8221;), NULL,\n                  WS_VISIBLE | WS_CHILD | WS_TABSTOP |\n                  ES_LEFT | ES_AUTOHSCROLL,\n                  0, 0, 0, 0,\n                  m_hwnd,\n                  (HMENU)IDC_EDIT,\n                  g_hinst,\n                  NULL);\n if (!m_hwndEdit) return -1;<\/p>\n<p> HFONT hfLV = GetWindowFont(m_hwndLV);\n SetWindowFont(m_hwndEdit, hfLV, FALSE);<\/p>\n<p> m_cyEdit = 0;\n HDC hdc = GetDC(m_hwndEdit);\n if (hdc) {\n  HFONT hfPrev = SelectFont(hdc, hfLV);\n  if (hfPrev) {\n   SIZE siz = { 0, 0 };\n   if (GetTextExtentPoint32(hdc, TEXT(&#8220;0&#8221;), 1, &amp;siz)) {\n     RECT rc = { 0, 0, siz.cx, siz.cy };\n     AdjustWindowRectEx(&amp;rc, GetWindowStyle(m_hwndEdit), FALSE,\n                             GetWindowExStyle(m_hwndEdit));\n     m_cyEdit = rc.bottom &#8211; rc.top;\n   }\n   SelectFont(hdc, hfPrev);\n  }\n  ReleaseDC( m_hwndEdit, hdc);\n }\n if (!m_cyEdit) return -1;<\/FONT><\/p>\n<p> return 0;\n}\n<\/PRE>\n<P>\nAfter creating it, we give it the same font that the listview\nis using, so that they match.\nWe then measure that font to figure out how big the edit control needs\nto be in order to accomodate the text.\n<\/P>\n<P>\nWe use this size information to guide how we lay out our window.\n<\/P>\n<PRE>\nLRESULT RootWindow::HandleMessage(\n                          UINT uMsg, WPARAM wParam, LPARAM lParam)\n{\n &#8230;\n  case WM_SIZE:\n   <FONT COLOR=\"blue\">if (m_hwndEdit) {\n    SetWindowPos(m_hwndEdit, NULL, 0, 0,\n                 GET_X_LPARAM(lParam), m_cyEdit,\n                 SWP_NOZORDER | SWP_NOACTIVATE);\n   }<\/FONT>\n   if (m_hwndLV) {\n    SetWindowPos(m_hwndLV, NULL, 0, <FONT COLOR=\"blue\">m_cyEdit,\n                 GET_X_LPARAM(lParam),\n                 GET_Y_LPARAM(lParam) &#8211; m_cyEdit,<\/FONT>\n                 SWP_NOZORDER | SWP_NOACTIVATE);\n   }\n   return 0;\n &#8230;\n}\n<\/PRE>\n<P>\nThe edit control goes at the top of our client area, and the listview\ngoes directly below it.\n<\/P>\n<P>\nFinally,\nwe add a call to\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/dialogboxes\/dialogboxreference\/dialogboxfunctions\/isdialogmessage.asp\">\nthe <CODE>IsDialogMessage<\/CODE> function<\/A>\nto our message loop,\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/10\/21\/55384.aspx\">\nmaking the dialog manager do the heavy lifting of\nnavigating around our window<\/A> via the Tab and Shift+Tab keys.\n<PRE>\n  RootWindow *prw = RootWindow::Create();\n  if (prw) {\n   ShowWindow(prw-&gt;GetHWND(), nShowCmd);\n   MSG msg;\n   while (GetMessage(&amp;msg, NULL, 0, 0)) {\n    <FONT COLOR=\"blue\">if (IsDialogMessage(prw-&gt;GetHWND(), &amp;msg)) {\n     \/* processed *\/\n    } else {<\/FONT>\n     TranslateMessage(&amp;msg);\n     DispatchMessage(&amp;msg);\n    <FONT COLOR=\"blue\">}<\/FONT>\n   }\n  }\n<\/PRE>\n<P>\nWhen you run this program, observe that the edit control\nand listview position themselves correctly as you resize the\nwindow, and that you can Tab between them.\nBut there&#8217;s still something wrong:\nFocus always returns to the listview when you\nswitch away and back.\nThat&#8217;s because I missed a spot.\n<\/P>\n<PRE>\nclass RootWindow : public Window\n{\nprivate:\n HWND m_hwndLV;\n HWND m_hwndEdit;\n <FONT COLOR=\"blue\">HWND m_hwndLastFocus;<\/FONT>\n int  m_cyEdit;\n &#8230;\n};<\/p>\n<p>LRESULT RootWindow::OnCreate()\n{\n &#8230;\n if (!m_cyEdit) return -1;<\/p>\n<p> <FONT COLOR=\"blue\">m_hwndLastFocus = m_hwndEdit;<\/FONT><\/p>\n<p> return 0;\n}<\/p>\n<p>LRESULT RootWindow::HandleMessage(\n                          UINT uMsg, WPARAM wParam, LPARAM lParam)\n{\n &#8230;\n  case WM_SETFOCUS:\n   if (<FONT COLOR=\"blue\">m_hwndLastFocus<\/FONT>) {\n    SetFocus(<FONT COLOR=\"blue\">m_hwndLastFocus<\/FONT>);\n   }\n   return 0;<\/p>\n<p>  <FONT COLOR=\"blue\">case WM_ACTIVATE:\n   if (wParam == WA_INACTIVE) {\n    m_hwndLastFocus = GetFocus();\n   }\n   break;<\/FONT>\n &#8230;\n}\n<\/PRE>\n<P>\nThe new member variable keeps track of which control had focus\nlast.  We update it when we lose activation and restore it\nwhen we regain focus.\n(Its initial value is set at creation so we know whom to give focus\nto when the window is shown for the first time.)\n<\/P>\n<P>\nOkay, that was an awful lot of typing without very much payoff.\nNext time, we&#8217;ll start searching the dictionary.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we last left the dictionary project, we were able to display the dictionary entries but hadn&#8217;t yet gotten around to searching it. Today, we&#8217;ll place the lookup control, though we won&#8217;t hook it up until next time. First, we give the edit control an ID and create some member variables to keep track of [&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-34613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>When we last left the dictionary project, we were able to display the dictionary entries but hadn&#8217;t yet gotten around to searching it. Today, we&#8217;ll place the lookup control, though we won&#8217;t hook it up until next time. First, we give the edit control an ID and create some member variables to keep track of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34613","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=34613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34613\/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=34613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=34613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=34613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}