{"id":35343,"date":"2005-06-13T09:02:16","date_gmt":"2005-06-13T09:02:16","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/06\/13\/displaying-the-dictionary-part-1-naive-version\/"},"modified":"2005-06-13T09:02:16","modified_gmt":"2005-06-13T09:02:16","slug":"displaying-the-dictionary-part-1-naive-version","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050613-16\/?p=35343","title":{"rendered":"Displaying the dictionary, part 1:  Naive version"},"content":{"rendered":"<p>\nWe return briefly to the\nongoing Chinese\/English dictionary series<\/a>\nand write some code to display all the definitions we had worked so\nhard to collect.\n(I figure you&#8217;re anxious to see something on the screen, so I\nam going to handle the Traditional Chinese\/Simplified Chinese\nissue later.  For now, the &#8220;Simplified&#8221; column will be blank.)\n<\/p>\n<p>\nTake\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/05\/19\/420038.aspx\">\nthe dictionary program we&#8217;ve been developing so far<\/a> and\npaste it into our\n<a>\nnew scratch program<\/a>.\n(Delete the <code>main<\/code> function, of course.)\nFirst, search\/replace and change <code>m_hwndChild<\/code>\nto <code>m_hwndLV<\/code> since our child window is a listview,\nand it&#8217;s just nicer to say what it is up front since we&#8217;re\ngoing to be talking about it a lot.\nNext, make the following additional changes:\n<\/p>\n<pre>\nclass RootWindow : public Window\n{\npublic:\n virtual LPCTSTR ClassName() { return TEXT(\"Scratch\"); }\n static RootWindow *Create();\nprotected:\n LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);\n LRESULT OnCreate();\n <font COLOR=\"blue\">const DictionaryEntry&amp; Item(int i) { return m_dict.Item(i); }\n int Length() { return m_dict.Length(); }<\/font>\nprivate:\n enum {\n  IDC_LIST = 1,\n };\n enum {\n  COL_TRAD,\n  COL_SIMP,\n  COL_PINYIN,\n  COL_ENGLISH,\n };\nprivate:\n HWND m_hwndLV;\n Dictionary m_dict;<\/font>\n};\n<font COLOR=\"blue\">LRESULT RootWindow::OnCreate()\n{\n  m_hwndLV = CreateWindow(WC_LISTVIEW, NULL,\n                  WS_VISIBLE | WS_CHILD | WS_TABSTOP |\n                  LVS_NOSORTHEADER |\n                  LVS_SINGLESEL | LVS_REPORT,\n                  0, 0, 0, 0,\n                  m_hwnd,\n                  (HMENU)IDC_LIST,\n                  g_hinst,\n                  NULL);\n if (!m_hwndLV) return -1;\n ListView_SetExtendedListViewStyleEx(m_hwndLV,\n                                     LVS_EX_FULLROWSELECT,\n                                     LVS_EX_FULLROWSELECT);\n LVCOLUMN lvc;\n lvc.mask = LVCF_TEXT | LVCF_WIDTH;\n lvc.cx = 200;\n lvc.pszText = TEXT(\"Traditional\");\n ListView_InsertColumn(m_hwndLV, COL_TRAD, &amp;lvc);\n lvc.mask = LVCF_TEXT | LVCF_WIDTH;\n lvc.cx = 200;\n lvc.pszText = TEXT(\"Simplified\");\n ListView_InsertColumn(m_hwndLV, COL_SIMP, &amp;lvc);\n lvc.mask = LVCF_TEXT | LVCF_WIDTH;\n lvc.cx = 200;\n lvc.pszText = TEXT(\"PinYin\");\n ListView_InsertColumn(m_hwndLV, COL_PINYIN, &amp;lvc);\n lvc.mask = LVCF_TEXT | LVCF_WIDTH;\n lvc.cx = 800;\n lvc.pszText = TEXT(\"English\");\n ListView_InsertColumn(m_hwndLV, COL_ENGLISH, &amp;lvc);\n ListView_SetItemCount(m_hwndLV, Length());\n for (int i = 0; i &lt; Length(); i++) {\n  const DictionaryEntry&amp; de = Item(i);\n  LVITEM item;\n  item.mask = LVIF_TEXT;\n  item.iItem = i;\n  item.iSubItem = COL_TRAD;\n  item.pszText = const_cast&lt;LPWSTR&gt;(de.m_pszTrad);\n  item.iItem = ListView_InsertItem(m_hwndLV, &amp;item);\n  if (item.iItem &gt;= 0) {\n   item.iSubItem = COL_PINYIN;\n   item.pszText = const_cast&lt;LPWSTR&gt;(de.m_pszPinyin);\n   ListView_SetItem(m_hwndLV, &amp;item);\n   item.iSubItem = COL_ENGLISH;\n   item.pszText = const_cast&lt;LPWSTR&gt;(de.m_pszEnglish);\n   ListView_SetItem(m_hwndLV, &amp;item);\n  }\n }\n return 0;\n}<\/font>\n<\/pre>\n<p>\nAfter creating the listview control, we set it into full row\nselect mode and create our columns.\nBefore inserting the words into the dictionary, we use\n<code>ListView_SetItemCount<\/code> to tell the listview\nthe number of items we&#8217;re about to put into the listview.\n(This is optional; it allows the listview to pre-allocate some structures.)\nI&#8217;m not using an STL iterator because this code is going to be\ndeleted soon.  You&#8217;ll find out why if you can&#8217;t figured it out already.\n<\/p>\n<p>\nCompile and run this program.  Notice that it takes a ridiculously\nlong time to start up.  That&#8217;s because our loop is inserting 20,000\ndictionary entries into the listview, and that can&#8217;t be fast.\n<\/p>\n<p>\nNext time, we&#8217;ll work on speeding that up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We return briefly to the ongoing Chinese\/English dictionary series and write some code to display all the definitions we had worked so hard to collect. (I figure you&#8217;re anxious to see something on the screen, so I am going to handle the Traditional Chinese\/Simplified Chinese issue later. For now, the &#8220;Simplified&#8221; column will be blank.) [&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-35343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>We return briefly to the ongoing Chinese\/English dictionary series and write some code to display all the definitions we had worked so hard to collect. (I figure you&#8217;re anxious to see something on the screen, so I am going to handle the Traditional Chinese\/Simplified Chinese issue later. For now, the &#8220;Simplified&#8221; column will be blank.) [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35343","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=35343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/35343\/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=35343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=35343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=35343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}