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. CListCtrl and integral number of rows

CListCtrl and integral number of rows

Scheduled Pinned Locked Moved C / C++ / MFC
5 Posts 2 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.
  • H Offline
    H Offline
    Harold Bamford
    wrote on last edited by
    #1

    Greetings, I am trying to do something that ought to be simple but it is eluding me. I want to resize a CListCtrl (in Report mode) from within OnInitialUpdate() such that exactly N rows are displayed (where N is already known to be small enough to fit on the screen without needing scroll bars). I can use GetCountPerPage() but that doesn't count partial lines and one of the things I want to eliminate is partially viewed lines. I must be missing something elementary so a code fragment would be very nice! Thanks.

    C 1 Reply Last reply
    0
    • H Harold Bamford

      Greetings, I am trying to do something that ought to be simple but it is eluding me. I want to resize a CListCtrl (in Report mode) from within OnInitialUpdate() such that exactly N rows are displayed (where N is already known to be small enough to fit on the screen without needing scroll bars). I can use GetCountPerPage() but that doesn't count partial lines and one of the things I want to eliminate is partially viewed lines. I must be missing something elementary so a code fragment would be very nice! Thanks.

      C Offline
      C Offline
      Chris Richardson
      wrote on last edited by
      #2

      I haven't tried it, but you can take a look at CListCtrl::ApproximateViewRect. In the event that this doesn't work, I think it should be possible just to use GetItemRect with LVSIL_BOUNDS, then take the height of the rectangle and multiply it by the number of items you have, and that will be the needed height. Chris Richardson C/C++ Include Finder[^]

      H 1 Reply Last reply
      0
      • C Chris Richardson

        I haven't tried it, but you can take a look at CListCtrl::ApproximateViewRect. In the event that this doesn't work, I think it should be possible just to use GetItemRect with LVSIL_BOUNDS, then take the height of the rectangle and multiply it by the number of items you have, and that will be the needed height. Chris Richardson C/C++ Include Finder[^]

        H Offline
        H Offline
        Harold Bamford
        wrote on last edited by
        #3

        Chris Richardson wrote: haven't tried it, but you can take a look at CListCtrl::ApproximateViewRect. In the event that this doesn't work, I think it should be possible just to use GetItemRect with LVSIL_BOUNDS, then take the height of the rectangle and multiply it by the number of items you have, and that will be the needed height. Actually I tried this but I must have something still basically wrong:

        <... do initialization ...>
        CSize sz = m_list.ApproximateViewRect();
        m_list.PostMessage(WM_SIZE, sz.cx, sz.cy);
        

        This seems to have no effect. I think I'm having a senior moment here...

        C 1 Reply Last reply
        0
        • H Harold Bamford

          Chris Richardson wrote: haven't tried it, but you can take a look at CListCtrl::ApproximateViewRect. In the event that this doesn't work, I think it should be possible just to use GetItemRect with LVSIL_BOUNDS, then take the height of the rectangle and multiply it by the number of items you have, and that will be the needed height. Actually I tried this but I must have something still basically wrong:

          <... do initialization ...>
          CSize sz = m_list.ApproximateViewRect();
          m_list.PostMessage(WM_SIZE, sz.cx, sz.cy);
          

          This seems to have no effect. I think I'm having a senior moment here...

          C Offline
          C Offline
          Chris Richardson
          wrote on last edited by
          #4

          That's not the correct way to size a window. Use this instead:

          m_list.SetWindowPos( NULL, 0, 0, sz.cx, sz.cy, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE );

          Chris Richardson C/C++ Include Finder[^]

          H 1 Reply Last reply
          0
          • C Chris Richardson

            That's not the correct way to size a window. Use this instead:

            m_list.SetWindowPos( NULL, 0, 0, sz.cx, sz.cy, SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE );

            Chris Richardson C/C++ Include Finder[^]

            H Offline
            H Offline
            Harold Bamford
            wrote on last edited by
            #5

            That's it! I know about and use SetWindowPos() but I just couldn't think of it. Thanks! And here is my working implementation (if anybody wanted to see actual code):

            /////////////////////////////////////////////////////////////////////////////////
            // This assumes a REPORT-style CListCtrl.
            //
            // Resize the control. This works correctly only if scrolling is disabled. If
            // there is scrolling, then setting to the size from ApproximateViewRect() will
            // always give scroll bars showing. Which is irritating.
            //
            // We need to adjust the vertical size from what ApproximateViewRect() returns
            // by one row minus border width
            //////////////////////////////////////////////////////////////////////////////////
            CSize sz = m_list.ApproximateViewRect();	// always adds room for a new row
            	
            CRect itRect;	// Get the height of a single row (there had better *be* a row!)
            m_list.GetItemRect(0, &itRect, LVIR_BOUNDS);
            	
            int vOffset = itRect.Height() - 3;	// leave a little 'cuz it looks better
            m_list.SetWindowPos(NULL, 0, 0, sz.cx, sz.cy - vOffset,
            	SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE);
            
            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