{"id":38933,"date":"2004-06-10T07:03:00","date_gmt":"2004-06-10T07:03:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/06\/10\/speeding-up-adding-items-to-a-combobox-or-listbox\/"},"modified":"2004-06-10T07:03:00","modified_gmt":"2004-06-10T07:03:00","slug":"speeding-up-adding-items-to-a-combobox-or-listbox","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040610-00\/?p=38933","title":{"rendered":"Speeding up adding items to a combobox or listbox"},"content":{"rendered":"<p>\nJust a little tip:  If you&#8217;re going to be adding a lot of items\nto a listbox or combobox, there are a few little things you can do\nto improve the performance significantly.\n(Note: The improvements work only if you have a <strong>lot<\/strong>\nof items, like hundreds.  Of course, the usability of a listbox\nwith a hundred items is questionable, but I&#8217;m assuming you have\na <strong>really good reason<\/strong> for doing this.)\n<\/p>\n<p>\nFirst, you can disable redraw while you add the items.\n(This tip works for all controls, actually.)\n<\/p>\n<pre>\n  SetWindowRedraw(hwnd, FALSE);\n  ... add the items ...\n  SetWindowRedraw(hwnd, TRUE);\n  InvalidateRect(hwnd, NULL, TRUE);\n<\/pre>\n<p>\n<code>SetWindowRedraw<\/code> is defined in the\n&lt;windowsx.h&gt; header file.\n<\/p>\n<p>\nThis suppresses the control redrawing itself each time you add\nan item.  But there is still something else you can do:\n<\/p>\n<pre>\n  SendMessage(hwndCombo, <a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/commctls\/comboboxes\/comboboxreference\/comboboxmessages\/cb_initstorage.asp\">CB_INITSTORAGE<\/a>, cItems, cbStrings);\n  ... add the items ...\n<\/pre>\n<p>\n(For listbox controls, use the\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/commctls\/listboxes\/listboxreference\/listboxmessages\/lb_initstorage.asp\">LB_INITSTORAGE<\/a>\nmessage.)\n<\/p>\n<p>\n<code>cItems<\/code> is the number of items you intend to add,\nand\n<code>cbStrings<\/code> is the total amount of memory (in bytes)\nrequired by the strings you intend to add.\n<\/p>\n<p>\nIt&#8217;s okay if these values are merely estimates.\nIf you are too low, then there will still be some reallocation\nfor the extra items.  If you are too high, then some memory will\nbe allocated but remain unused.\n<\/p>\n<p>\n<a HREF=\"http:\/\/weblogs.asp.net\/JDanforth\/archive\/2004\/03\/12\/88458.aspx\">\nSome people will recommend using LockWindowUpdate<\/a>, but this is\n<strong>wrong<\/strong>.\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/gdi\/pantdraw_4i5h.asp\">\nLockWindowUpdate<\/a> disables drawing in the window you specify,\nbut suppressing flickering during updates is not what it was designed for.\n<\/p>\n<p>\nOne clear limitation limitation of LockWindowUpdate is that only one\nwindow can be locked for update at a time.\nSo if two windows each try the LockWindowUpdate trick at the same\ntime, only one of them will succeed.  That&#8217;s not particularly reliable,\nnow, is it.\n<\/p>\n<p>\nThe purpose of\nLockWindowUpdate is to assist code that is drawing drag\/drop feedback.\nIf you are drawing the cursor for a drag\/drop operation, you don&#8217;t\nwant the window beneath the cursor to start drawing (and thereby\noverwrite your beautiful cursor).  So you lock the window while you draw\nthe cursor and unlock it when the cursor leaves the window.\n<\/p>\n<p>\nThat&#8217;s why there is only one locked window at a time:\nThere can be only one drag\/drop operation active at a time,\nsince there is only one mouse.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a little tip: If you&#8217;re going to be adding a lot of items to a listbox or combobox, there are a few little things you can do to improve the performance significantly. (Note: The improvements work only if you have a lot of items, like hundreds. Of course, the usability of a listbox with [&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-38933","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Just a little tip: If you&#8217;re going to be adding a lot of items to a listbox or combobox, there are a few little things you can do to improve the performance significantly. (Note: The improvements work only if you have a lot of items, like hundreds. Of course, the usability of a listbox with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38933","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=38933"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38933\/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=38933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=38933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=38933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}