Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to (fast) fill a CListCtrl in C++ (MFC)?

Tags:

c++

mfc

clistctrl

in my application I have a few CListCtrl tables. I fill/refresh them with data from an array with a for-loop. Inside the loop I have to make some adjustments on how I display the values so data binding in any way is not possible at all.

The real problem is the time it takes to fill the table since it is redrawn row by row. If I turn the control invisible while it is filled and make it visible again when the loop is done the whole method is a lot faster!

Now I am looking for a way to stop the control from repainting until it is completely filled. Or any other way to speed things up.

like image 733
TalkingCode Avatar asked Jul 10 '09 13:07

TalkingCode


2 Answers

Look into the method SetRedraw. Call SetRedraw(FALSE) before starting to fill the control, SetRedraw(TRUE) when finished.

I would also recommend using RAII for this:

class CFreezeRedraw
{
public:
   CFreezeRedraw(CWnd & wnd) : m_Wnd(wnd) { m_Wnd.SetRedraw(FALSE); }
   ~CFreezeRedraw() { m_Wnd.SetRedraw(TRUE); }
private:
   CWnd & m_Wnd;
};

Then use like:

CFreezeRedraw freezeRedraw(myListCtrl);
//... populate control ...

You can create an artificial block around the code where you populate the list control if you want freezeRedraw to go out of scope before the end of the function.

like image 128
Nick Meyer Avatar answered Sep 22 '22 18:09

Nick Meyer


If you have a lot of records may be it is more appropriate to use virtual list style (LVS_OWNERDATA). You could find more information here.

like image 27
Kirill V. Lyadvinsky Avatar answered Sep 24 '22 18:09

Kirill V. Lyadvinsky