CListCtrl and integral number of rows
-
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 useGetCountPerPage()
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. -
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 useGetCountPerPage()
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.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[^]
-
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[^]
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...
-
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...
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[^]
-
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[^]
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);