{"id":34953,"date":"2005-07-13T10:00:12","date_gmt":"2005-07-13T10:00:12","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/07\/13\/converting-from-traditional-to-simplified-chinese-part-3-highlighting-differences\/"},"modified":"2005-07-13T10:00:12","modified_gmt":"2005-07-13T10:00:12","slug":"converting-from-traditional-to-simplified-chinese-part-3-highlighting-differences","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050713-12\/?p=34953","title":{"rendered":"Converting from traditional to simplified Chinese, part 3: Highlighting differences"},"content":{"rendered":"<p><P>\nOne of the things that is interesting to me as a student of the\nChinese languages is to recognize where the traditional and\nsimplified Chinese scripts differ.\nSince this is my program, I&#8217;m going to hard-code the color for\nsimplified Chinese script:  maroon.\n<\/P>\n<P>\nTo accomplish the highlighting, we take advantage of listview&#8217;s\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/commctls\/custdraw\/custdraw.asp\">\ncustom-draw<\/A> feature.\nCustom-draw allows you to make minor changes to the way items\nare displayed on the screen.\nIt&#8217;s a middle ground between having listview do all the work\n(via default drawing behavior) and having the program do all\nthe work (via owner-draw).\n<\/P>\n<P>\nThe custom-draw cycle for shell common controls consists of\nseries of\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/commctls\/custdraw\/messages\/nm_customdraw.asp\">\n<CODE>NM_CUSTOMDRAW<\/CODE> notifications<\/A>, starting with\nthe most general and getting more specific.\nThe reason for the break-down is multi-fold.\nFirst, it allows the listview control to short-circuit a portion\nof custom-draw behavior if the parent window does not indicate\nthat it wishes to customize a particular behavior.\nThis reduces message traffic and improves performance when large\nnumbers of items are being drawn.\nSecond, it allows the parent window to target its customizations\nto the drawing stages it is interested in.\n<\/P>\n<P>\nListviews are peculiar among the shell common controls in that\nits items sometimes (but not always) have sub-items.\nThis complicates the drawing process since it requires listview\nto accomodate both styles:\nlarge icon view does not use sub-items, but report view does.\nTo address this, the <CODE>CDDS_ITEMPREPAINT<\/CODE>\nstage is entered when an item is about to paint,\nand any changes made by the parent window are considered to\nbe effective for the entire item.\nIf you want to make changes on a per-subitem basis,\nyou have to respond to <CODE>CDDS_ITEMPREPAINT | CDDS_SUBITEM<\/CODE>\nand set your properties (or reset them if you want to return to the\ndefault) for that sub-item.\n<\/P>\n<P>\nWith those preliminary remarks settled, we can dive in.\n<\/P>\n<PRE>\nclass RootWindow : public Window\n{\n &#8230;\nprotected:\n &#8230;\n <FONT COLOR=\"blue\">LRESULT OnLVCustomDraw(NMLVCUSTOMDRAW* pcd);<\/FONT>\n &#8230;\nprivate:\n HWND m_hwndLV;\n <FONT COLOR=\"blue\">COLORREF m_clrTextNormal;<\/FONT>\n Dictionary m_dict;\n};\n<\/PRE>\n<P>\nWe declare our listview custom-draw handler as well as the member\nvariable in which we remember the normal text color so that we can\nreset it for columns we do not intend to colorize.\n<\/P>\n<PRE>\nLRESULT RootWindow::OnNotify(NMHDR *pnm)\n{\n switch (pnm-&gt;code) {\n case LVN_GETDISPINFO:\n  OnGetDispInfo(CONTAINING_RECORD(pnm, NMLVDISPINFO, hdr));\n  break;\n case NM_CUSTOMDRAW:\n  if (pnm-&gt;hwndFrom == m_hwndLV) {\n   return OnLVCustomDraw(CONTAINING_RECORD(\n                         CONTAINING_RECORD(pnm, NMCUSTOMDRAW, hdr),\n                                                NMLVCUSTOMDRAW, nmcd));\n  }\n  break;\n }\n return 0;\n}\n<\/PRE>\n<P>\nIf we receive a\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/commctls\/custdraw\/messages\/nm_customdraw.asp\">\n<CODE>NM_CUSTOMDRAW<\/CODE> notification<\/A>\nfrom the listview control, we call our new handler.\nThe multiple calls to\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/kmarch\/hh\/kmarch\/k106_6a249de6-d707-421c-9b91-96e1b14dc21b.xml.asp\">\nthe <CODE>CONTAINING_RECORD<\/CODE> macro<\/A>\nare necessary because\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/commctls\/common\/structures\/nmhdr.asp\">\nthe <CODE>NMHDR<\/CODE> structure<\/A> is nestled\ntwo levels deep inside\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/commctls\/listview\/structures\/nmlvcustomdraw.asp\">\nthe <CODE>NMLVCUSTOMDRAW<\/CODE> structure<\/A>.\n<\/P>\n<PRE>\n<FONT COLOR=\"blue\">LRESULT RootWindow::OnLVCustomDraw(NMLVCUSTOMDRAW* pcd)\n{\n switch (pcd-&gt;nmcd.dwDrawStage) {\n case CDDS_PREPAINT: return CDRF_NOTIFYITEMDRAW;\n case CDDS_ITEMPREPAINT:\n  m_clrTextNormal = pcd-&gt;clrText;\n  return CDRF_NOTIFYSUBITEMDRAW;\n case CDDS_ITEMPREPAINT | CDDS_SUBITEM:\n  pcd-&gt;clrText = m_clrTextNormal;\n  if (pcd-&gt;iSubItem == COL_SIMP &amp;&amp;\n    pcd-&gt;nmcd.dwItemSpec &lt; (DWORD)Length()) {\n    const DictionaryEntry&amp; de = Item(pcd-&gt;nmcd.dwItemSpec);\n    if (de.m_pszSimp) {\n      pcd-&gt;clrText = RGB(0x80, 0x00, 0x00);\n    }\n  }\n  break;\n }\n return CDRF_DODEFAULT;\n}<\/FONT>\n<\/PRE>\n<P>\nDuring the <CODE>CDDS_PREPAINT<\/CODE> stage, we indicate our\ndesire to receive <CODE>CDDS_ITEMPREPAINT<\/CODE> notifications.\nDuring the <CODE>CDDS_ITEMPREPAINT<\/CODE> stage,\nwe save the normal text color and indicate that we want to receive\nsub-item notifications.\nIt is in the sub-item notification\n<CODE>CDDS_ITEMPREPAINT | CDDS_SUBITEM<\/CODE> that the real work happens.\n<\/P>\n<P>\nFirst, we reset the color to the default on the assumption that we\nwill not need to colorize this column.\nBut if the column is the simplified Chinese column, if the\nitem number is valid, and if the simplified Chinese is different\nfrom the traditional Chinese, then we set the text color to maroon.\n<\/P>\n<P>\nThat&#8217;s enough with the Chinese\/English dictionary for now.\nAll this time, and we don&#8217;t even have search capability yet!\nWe&#8217;ll work on that next month.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things that is interesting to me as a student of the Chinese languages is to recognize where the traditional and simplified Chinese scripts differ. Since this is my program, I&#8217;m going to hard-code the color for simplified Chinese script: maroon. To accomplish the highlighting, we take advantage of listview&#8217;s custom-draw feature. Custom-draw [&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-34953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>One of the things that is interesting to me as a student of the Chinese languages is to recognize where the traditional and simplified Chinese scripts differ. Since this is my program, I&#8217;m going to hard-code the color for simplified Chinese script: maroon. To accomplish the highlighting, we take advantage of listview&#8217;s custom-draw feature. Custom-draw [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34953","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=34953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34953\/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=34953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=34953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=34953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}