{"id":2103,"date":"2014-01-13T07:00:00","date_gmt":"2014-01-13T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/01\/13\/creating-a-listview-with-checkboxes-on-some-items-but-not-others\/"},"modified":"2014-01-13T07:00:00","modified_gmt":"2014-01-13T07:00:00","slug":"creating-a-listview-with-checkboxes-on-some-items-but-not-others","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140113-00\/?p=2103","title":{"rendered":"Creating a listview with checkboxes on some items but not others"},"content":{"rendered":"<p>\nToday&#8217;s Little Program creates a listview with checkboxes\non some items but not other.\n<\/p>\n<p>\nThe\n<code>LVS_EX_CHECK&shy;BOXES<\/code>\nextended style is really just a convenience style.\nEverything it does you could have done yourself,\nwith a bit more typing.\n<\/p>\n<ul>\n<li>It creates a state image list consisting of\n    an unchecked box (state 1) and a checked box (state 2).\n    You could have done this yourself with\n    <code>Image&shy;List_Create<\/code> followed by\n    a few calls to\n    <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/08\/01\/445998.aspx\">\n    <code>Draw&shy;Frame&shy;Control<\/code><\/a>.<\/p>\n<li>When you hit the space bar or click on the check box,\n    the state image toggles between 1 and 2.\n    You could have done this yourself by responding to\n    <code>LVN_KEY&shy;DOWN<\/code> (for the space bar),\n    and the mouse notification messages for the clicks.\n    (For the mouse notifications, see if the click was on\n    <code>LVHT_ON&shy;ITEM&shy;STATE&shy;ICON<\/code>.)\n<\/ul>\n<p>\nBut still, it&#8217;s convenient having the listview control do\nthis grunt work for you.\nBut what if you want to remove the check box from some items?\n<\/p>\n<p>\nThe listview control turns on the state image and toggles\nit by doing the moral equivalent of a\n<code>List&shy;View_Set&shy;Check&shy;State<\/code>\non the item,\nso all you have to do is respond to the\n<code>LVN_ITEM&shy;CHANGING<\/code> that comes with\nany item change and reject the state change.\n<\/p>\n<p>\nStart with our\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a>\nand make these changes.\nRemember, Little Programs do little or no error checking.\n<\/p>\n<pre>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n  <font COLOR=\"blue\">g_hwndChild = CreateWindow(WC_LISTVIEW, NULL,\n        WS_CHILD | WS_VISIBLE | LVS_REPORT,\n        0, 0, 0, 0, hwnd, (HMENU)1, g_hinst, 0);\n  ListView_SetExtendedListViewStyle(g_hwndChild,\n                                    LVS_EX_CHECKBOXES);\n  LVCOLUMN col;\n  col.mask = LVCF_TEXT | LVCF_WIDTH;\n  col.cx = 200;\n  col.pszText = TEXT(\"Name\");\n  ListView_InsertColumn(g_hwndChild, 0, &amp;col);\n  LVITEM item;\n  item.mask = LVIF_TEXT;\n  item.iSubItem = 0;\n  item.pszText = TEXT(\"Alpha\");\n  ListView_InsertItem(g_hwndChild, &amp;item);\n  item.pszText = TEXT(\"Beta\");\n  ListView_InsertItem(g_hwndChild, &amp;item);\n  item.pszText = TEXT(\"Gamma\");\n  ListView_InsertItem(g_hwndChild, &amp;item);\n  item.pszText = TEXT(\"Delta\");\n  ListView_InsertItem(g_hwndChild, &amp;item);<\/font>\n  return TRUE;\n}\n<\/pre>\n<p>\nOkay, so far the program adds four items,\neach with a check box.\nBut let&#8217;s say we want to remove the check boxes\nfrom the even-numbered items.\n<\/p>\n<pre>\n<font COLOR=\"blue\">LRESULT\nOnNotify(HWND hwnd, int idFrom, NMHDR *pnm)\n{\n  if (idFrom == 1) {\n    switch (pnm-&gt;code) {\n    case LVN_ITEMCHANGING:\n      {\n        LPNMLISTVIEW pnmlv = CONTAINING_RECORD(pnm, NMLISTVIEW, hdr);\n        if (pnmlv-&gt;iItem &gt;= 0 &amp;&amp;\n        if (pnmlv-&gt;iItem % 2 == 0 &amp;&amp;\n            (pnmlv-&gt;uChanged &amp; LVIF_STATE)) {\n         return TRUE; \/\/ reject changes to even-numbered items\n        }\n      }\n      break;\n    }\n  }\n  return 0;\n}\n    HANDLE_MSG(hwnd, WM_NOTIFY, OnNotify);<\/font>\n<\/pre>\n<p>\nWe add a handler for\n<code>LVN_ITEM&shy;CHANGING<\/code> that says,\n&#8220;If this is a notification for an even-numbered item,\nand they want to change the state,\nthen block the state change.&#8221;\nThis ensures that nobody can turn on the state image,\nwhich means that the checkbox never shows up.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program creates a listview with checkboxes on some items but not other. The LVS_EX_CHECK&shy;BOXES extended style is really just a convenience style. Everything it does you could have done yourself, with a bit more typing. It creates a state image list consisting of an unchecked box (state 1) and a checked box (state [&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-2103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program creates a listview with checkboxes on some items but not other. The LVS_EX_CHECK&shy;BOXES extended style is really just a convenience style. Everything it does you could have done yourself, with a bit more typing. It creates a state image list consisting of an unchecked box (state 1) and a checked box (state [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2103","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=2103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2103\/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=2103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}