{"id":43033,"date":"2003-07-29T18:48:00","date_gmt":"2003-07-29T18:48:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2003\/07\/29\/scrollbars-part-3-optimizing-the-paint-cycle\/"},"modified":"2003-07-29T18:48:00","modified_gmt":"2003-07-29T18:48:00","slug":"scrollbars-part-3-optimizing-the-paint-cycle","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20030729-00\/?p=43033","title":{"rendered":"Scrollbars, part 3: Optimizing the paint cycle"},"content":{"rendered":"<p>\n        Observe that we paint all 100 lines in our paint handler, even though most of them\n        aren&#8217;t visible. This is a problem if there are a large number of items, or if painting\n        an item is time-consuming.\n    <\/p>\n<p>\n        So instead, we optimize our paint cycle so as to paint only the elements which intersect\n        the paint rectangle.\n    <\/p>\n<pre>void\nPaintSimpleContent(HWND hwnd, PAINTSTRUCT *pps)\n{\n    HFONT hfPrev = SelectFont(pps-&gt;hdc, g_hfList);  \/* Use the right font *\/\n    int iMin = max(pps-&gt;rcPaint.top \/ g_cyLine, 0);\n    int iMax = min((pps-&gt;rcPaint.bottom + g_cyLine - 1) \/ g_cyLine, g_cItems);\n    for (int i = iMin; i &lt; iMax; i++) {\n        char szLine[256];\n        int cch = wsprintf(szLine, \"This is line %d\", i);\n        TextOut(pps-&gt;hdc, 0, i * g_cyLine, szLine, cch);\n    }\n    SelectFont(pps-&gt;hdc, hfPrev);\n}\n<\/pre>\n<p>\n        Exercise: Explain the formulas for <i>iMin<\/i> and <i>iMax<\/i>. Explain why the seemingly\n        equivalent formula\n    <\/p>\n<pre>    int iMax = min((pps-&gt;rcPaint.bottom - 1) \/ g_cyLine + 1, g_cItems);\n<\/pre>\n<p>\n        is wrong. Then explain why it doesn&#8217;t really matter too much.\n    <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Observe that we paint all 100 lines in our paint handler, even though most of them aren&#8217;t visible. This is a problem if there are a large number of items, or if painting an item is time-consuming. So instead, we optimize our paint cycle so as to paint only the elements which intersect the paint [&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-43033","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Observe that we paint all 100 lines in our paint handler, even though most of them aren&#8217;t visible. This is a problem if there are a large number of items, or if painting an item is time-consuming. So instead, we optimize our paint cycle so as to paint only the elements which intersect the paint [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43033","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=43033"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/43033\/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=43033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=43033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=43033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}