3
votes

I need to print data of many lines. Each line could have text wrapping so the height varies. To do scrolling, I need to derive the total height. Since each line could have different height, I can only go through every line to add up the heights, like below.

void CMyScrollView::OnInitialUpdate()
{
    ...
    for (auto &l: lines)
    {
        DrawText(dc, l.text, &rc, DT_EDITCONTROL | DT_WORDBREAK | DT_CALCRECT);
        total_height += rc.Height();
    }
    SetScrollSizes(MM_TEXT, CRect(..., total_height));
}

What I've found is that "DrawText(... DT_CALCRECT)" is pretty costly even without actual drawing. Is there way to accelerate the process in such case?

Not an actual acceleration but doing this calculation in chunks would at least make your program responsive. This would involve defining a custom message and processing some number of lines each time the message is received, keep posting the message so long as you haven't reached the end of your data.SoronelHaetir
Thanks, it works well!user180574
I should add that if this is going to take more than a second or two that it would be a good idea to add some sort of progress indicator to the status bar.SoronelHaetir
@SoronelHaetir, Good point, I am adding it...user180574
@SoronelHaetir, chunks are new for me, can you add this comment to answer with a little codefragment. Thanks.Tom Tom