Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Insert new row in CListCtrl: need to improve the speed

Insert new row in CListCtrl: need to improve the speed

Scheduled Pinned Locked Moved C / C++ / MFC
performancejsonquestioncode-review
16 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S SandipG

    why you want to maintain STL is there you just have to use it :)

    Regards, Sandip.

    T Offline
    T Offline
    tataxin
    wrote on last edited by
    #7

    STL? is that Standard Template Library? sorry SandipG, I really don't understand what you mean. Can you explain more detail, :sigh: :confused:

    S 1 Reply Last reply
    0
    • T tataxin

      do you mean that:

      SetRedraw(false);

      // insert data here
      ....

      SetRedraw(true);
      myList.RedrawWindow();

      I applied it, it looks better but the actual time is the same with previous. And I'll try virtual list control now, thks, :)

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #8

      tataxin wrote:

      do you mean that:

      Hope you are adding this above and after the "for" loop. :) Is it necessary to call RedrawWindow?

      tataxin wrote:

      I applied it, it looks better but the actual time is the same with previous. And I'll try virtual list control now, thks, [Smile]

      It's better to use virtual list if volume of data is high!

      Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

      T 1 Reply Last reply
      0
      • N Nibu babu thomas

        tataxin wrote:

        do you mean that:

        Hope you are adding this above and after the "for" loop. :) Is it necessary to call RedrawWindow?

        tataxin wrote:

        I applied it, it looks better but the actual time is the same with previous. And I'll try virtual list control now, thks, [Smile]

        It's better to use virtual list if volume of data is high!

        Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

        T Offline
        T Offline
        tataxin
        wrote on last edited by
        #9

        yes, for loops are inside, like this

        SetRedraw(false);

        // insert data here
        for()
        {
        ......
        for()
        {
        ......
        }
        }

        SetRedraw(true);
        myList.RedrawWindow();

        it takes same time with the previous, in my project. And, without RedrawWindow(), nothing is showed, myList is empty. Ahh, the idea of virtual list is very nice. I'm reading now, :)

        1 Reply Last reply
        0
        • T tataxin

          STL? is that Standard Template Library? sorry SandipG, I really don't understand what you mean. Can you explain more detail, :sigh: :confused:

          S Offline
          S Offline
          SandipG
          wrote on last edited by
          #10

          Yes tataxin i mean Standard template library. instead of using CArray you can use stack which is LIFO so you dont need to move elements down after every element added.

          Regards, Sandip.

          T 2 Replies Last reply
          0
          • T tataxin

            I have a CListCtrl, each row has a specified color.

            // the list ctrl
            CListCtrl myList;

            // function to set color to a row
            myList.SetRowColor(j, crRows.GetAt(j));

            Then I need to insert some new rows, the latest is at top. Here is source code

            CArray crRows;
            CArray newData;
            int nNew = newData.GetCount();
            for (int i=0; i		// insert new row at top
            	CString txt = newData.GetAt(i);
            	myList.InsertItem(0, txt);
            
            	// get color, depends on the real data
            	COLORREF aColor = GetRowColor(txt);
            	crRows.InsertAt(0, aColor)		
            
            	// change the rest of list ctrl
            	int nCnt = myList.GetItemCount();
            	for (int j = 0; j                     myList.SetRowColor(j, crRows.GetAt(j));
            	}
            }
            

            I works, but because of 2 loop for, the performance speed is very slow, so I have to improve it Does anyone know what should I do in this case?? Thanks in advance :)

            J Offline
            J Offline
            Jijo Raj
            wrote on last edited by
            #11

            tataxin wrote:

            // get color, depends on the real data COLORREF aColor = GetRowColor(txt); crRows.InsertAt(0, aColor)

            containers like CArray, vector are not suitable for insertion. Because those containers keep the data in contagious memory block and if you want to insert one item to middle, they've do a lot of memory moving to keep the items contagious. Go for other insertion friendly containers such as std::list. Check there about how to use it - http://www.cplusplus.com/reference/stl/list/[^]

            tataxin wrote:

            // get color, depends on the real data COLORREF aColor = GetRowColor(txt); crRows.InsertAt(0, aColor)

            If you've lot of items to be added to your listctrl, better lock the control by LockWindowUpdate() then after insertion of all items, unlock by UnlockWindowUpdate(). It will avoid the flicker. Check this - http://weseetips.com/2008/04/19/how-to-update-bulk-amount-of-data-to-gui-controls-without-flicker/[^] Regards, Jijo.

            _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

            T 1 Reply Last reply
            0
            • J Jijo Raj

              tataxin wrote:

              // get color, depends on the real data COLORREF aColor = GetRowColor(txt); crRows.InsertAt(0, aColor)

              containers like CArray, vector are not suitable for insertion. Because those containers keep the data in contagious memory block and if you want to insert one item to middle, they've do a lot of memory moving to keep the items contagious. Go for other insertion friendly containers such as std::list. Check there about how to use it - http://www.cplusplus.com/reference/stl/list/[^]

              tataxin wrote:

              // get color, depends on the real data COLORREF aColor = GetRowColor(txt); crRows.InsertAt(0, aColor)

              If you've lot of items to be added to your listctrl, better lock the control by LockWindowUpdate() then after insertion of all items, unlock by UnlockWindowUpdate(). It will avoid the flicker. Check this - http://weseetips.com/2008/04/19/how-to-update-bulk-amount-of-data-to-gui-controls-without-flicker/[^] Regards, Jijo.

              _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

              T Offline
              T Offline
              tataxin
              wrote on last edited by
              #12

              ah, thank you Jijo, I will check it now

              1 Reply Last reply
              0
              • S SandipG

                Yes tataxin i mean Standard template library. instead of using CArray you can use stack which is LIFO so you dont need to move elements down after every element added.

                Regards, Sandip.

                T Offline
                T Offline
                tataxin
                wrote on last edited by
                #13

                Nice SandigG, that's what I want, :) but I dont know how to implement this. Can you show me more details.

                1 Reply Last reply
                0
                • S SandipG

                  Yes tataxin i mean Standard template library. instead of using CArray you can use stack which is LIFO so you dont need to move elements down after every element added.

                  Regards, Sandip.

                  T Offline
                  T Offline
                  tataxin
                  wrote on last edited by
                  #14

                  can anyone give me more details or give me some document to read about this? thanks in advance !!

                  S 1 Reply Last reply
                  0
                  • T tataxin

                    can anyone give me more details or give me some document to read about this? thanks in advance !!

                    S Offline
                    S Offline
                    SandipG
                    wrote on last edited by
                    #15

                    Look here [^] Few more.. [^] [^]

                    Regards, Sandip.

                    1 Reply Last reply
                    0
                    • T tataxin

                      I have a CListCtrl, each row has a specified color.

                      // the list ctrl
                      CListCtrl myList;

                      // function to set color to a row
                      myList.SetRowColor(j, crRows.GetAt(j));

                      Then I need to insert some new rows, the latest is at top. Here is source code

                      CArray crRows;
                      CArray newData;
                      int nNew = newData.GetCount();
                      for (int i=0; i		// insert new row at top
                      	CString txt = newData.GetAt(i);
                      	myList.InsertItem(0, txt);
                      
                      	// get color, depends on the real data
                      	COLORREF aColor = GetRowColor(txt);
                      	crRows.InsertAt(0, aColor)		
                      
                      	// change the rest of list ctrl
                      	int nCnt = myList.GetItemCount();
                      	for (int j = 0; j                     myList.SetRowColor(j, crRows.GetAt(j));
                      	}
                      }
                      

                      I works, but because of 2 loop for, the performance speed is very slow, so I have to improve it Does anyone know what should I do in this case?? Thanks in advance :)

                      T Offline
                      T Offline
                      tataxin
                      wrote on last edited by
                      #16

                      hi all, I found a solution, just use the custom draw, do not use a SetRowColor() method. So I just insert new row at top, like this

                          CArray crRows;
                      CArray newData;
                      int nNew = newData.GetCount();
                      for (int i=0; i<nnew;>		// insert new row at top
                      	CString txt = newData.GetAt(i);
                      	myList.InsertItem(0, txt);
                      
                      	// get color, depends on the real data
                      	COLORREF aColor = GetRowColor(txt);
                      	crRows.InsertAt(0, aColor)		
                      
                      	// change the rest of list ctrl
                      }
                      

                      and then, in custom draw of the list control, I implement like this:

                      void CExample1Dlg::OnNMCustomdrawList2(NMHDR *pNMHDR, LRESULT *pResult)
                      {
                      NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<nmlvcustomdraw*>( pNMHDR );

                      int nItem = static\_cast (pLVCD->nmcd.dwItemSpec);
                      
                      pLVCD->clrTextBk = RGB(0, 0, 0); // select background color
                      
                      \*pResult = CDRF\_DODEFAULT;
                      
                      // First thing - check the draw stage. If it's the control's prepaint
                      
                      // stage, then tell Windows we want messages for every item.
                      
                      
                      if ( CDDS\_PREPAINT == pLVCD->nmcd.dwDrawStage )
                      {
                      	\*pResult = CDRF\_NOTIFYITEMDRAW;
                      }
                      else if ( CDDS\_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
                      {
                      	// This is the prepaint stage for an item. Here's where we set the
                      
                      	// item's text color. Our return value will tell Windows to draw the
                      
                      	// item itself, but it will use the new color we set here.
                      
                      	// We'll cycle the colors through red, green, and light blue.
                      
                      
                      	COLORREF aColor = crText.GetAt(nItem);		
                      	pLVCD->clrText = aColor;
                      	
                      	\*pResult = CDRF\_DODEFAULT;
                      }	
                      

                      }

                      that's all, and it doesn't take any time to change the color (time is almost 0) Thank you everyone :)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups