{"id":42463,"date":"2003-09-17T11:30:00","date_gmt":"2003-09-17T11:30:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2003\/09\/17\/scrollbars-part-12-applying-wm_nccalcsize-to-our-scrollbar-sample\/"},"modified":"2003-09-17T11:30:00","modified_gmt":"2003-09-17T11:30:00","slug":"scrollbars-part-12-applying-wm_nccalcsize-to-our-scrollbar-sample","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20030917-00\/?p=42463","title":{"rendered":"Scrollbars part 12: Applying WM_NCCALCSIZE to our scrollbar sample"},"content":{"rendered":"<p>\n        Now that we have learned about the intricacies of the <code>WM_NCCALCSIZE<\/code> message,\n        we can use it to get rid of the flicker in our resizing code. We just take the trick\n        we used above and apply it to the scroll program.\n    <\/p>\n<p>\n        First, we need to get rid of the bad flickery resize, so return the <i>OnWindowPosChanging<\/i> function\n        to the version before we tried doing metaphor work:\n    <\/p>\n<pre>BOOL OnWindowPosChanging(HWND hwnd, LPWINDOWPOS pwp)\n{\n    if (!(pwp-&gt;flags &amp; SWP_NOSIZE)) {\n        RECT rc = { 0, 0, pwp-&gt;cx, pwp-&gt;cy };\n        AdjustSizeRectangle(hwnd, WMSZ_BOTTOM, &amp;rc);\n        pwp-&gt;cy = rc.bottom;\n    }\n    return 0;\n}\n<\/pre>\n<p>\n        Instead, our work will happen in the <code>WM_NCCALCSIZE<\/code> handler.\n    <\/p>\n<pre>UINT OnNcCalcSize(HWND hwnd, BOOL fCalcValidRects,\n                  NCCALCSIZE_PARAMS *pcsp)\n{\n    UINT uRc = (UINT)FORWARD_WM_NCCALCSIZE(hwnd,\n                        fCalcValidRects, pcsp, DefWindowProc);\n    if (fCalcValidRects) {\n        \/\/  Give names to these things\n        RECT *prcClientNew = &amp;pcsp-&gt;rgrc[0];\n        RECT *prcValidDst  = &amp;pcsp-&gt;rgrc[1];\n        RECT *prcValidSrc  = &amp;pcsp-&gt;rgrc[2];\n        int dpos;\n        int pos;\n        \/\/ Did we drag the top edge enough to scroll?\n        if (prcClientNew-&gt;bottom == prcValidSrc-&gt;bottom &amp;&amp;\n            g_cyLine &amp;&amp;\n            (dpos = (prcClientNew-&gt;top - prcValidSrc-&gt;top)\n                                            \/ g_cyLine) != 0 &amp;&amp;\n            (pos = ClampScrollPos(g_yOrigin + dpos)) != g_yOrigin) {\n            *prcValidDst = *prcClientNew;\n            ScrollTo(hwnd, pos, FALSE);\n            prcValidDst-&gt;top -= dpos * g_cyLine;\n            uRc = WVR_VALIDRECTS;\n        }\n    }\n    return uRc;\n}\n    \/* Add to WndProc *\/\n    HANDLE_MSG(hwnd, WM_NCCALCSIZE, OnNcCalcSize);\n<\/pre>\n<p>\n        This uses a new helper function which we extracted from the <i>ScrollTo<\/i> function.\n        (If I had planned this better, this would have been factored out when we first wrote <i>ScrollTo<\/i>.)\n    <\/p>\n<pre>int ClampScrollPos(int pos)\n{\n    \/*\n     *  Keep the value in the range 0 .. (g_cItems - g_cyPage).\n     *\/\n    pos = max(pos, 0);\n    pos = min(pos, g_cItems - g_cyPage);\n    return pos;\n}\n<\/pre>\n<p>\n        I am not entirely happy with this code, however. It is my personal opinion that the <code>WM_NCCALCSIZE<\/code> handler\n        should be stateless. Notice that this one modifies state (by calling <i>ScrollTo<\/i>).\n        If I had more time (sorry, I&#8217;m rushed now &#8211; you&#8217;ll learn why soon), I would have put\n        the state modification into the <code>WM_WINDOWPOSCHANGING<\/code> message. So I will\n        leave that as another exercise.\n    <\/p>\n<p>        <b>Exercise<\/b>: Keep the <code>WM_NCCALCSIZE<\/code> message stateless.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that we have learned about the intricacies of the WM_NCCALCSIZE message, we can use it to get rid of the flicker in our resizing code. We just take the trick we used above and apply it to the scroll program. First, we need to get rid of the bad flickery resize, so return the [&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-42463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Now that we have learned about the intricacies of the WM_NCCALCSIZE message, we can use it to get rid of the flicker in our resizing code. We just take the trick we used above and apply it to the scroll program. First, we need to get rid of the bad flickery resize, so return the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/42463","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=42463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/42463\/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=42463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=42463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=42463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}