Listview controls
-
I have created a Listview box which would display data in the Report mode. When filling in the header columns is it possible to set the width to correctly display the full text string? Most of the examples I have found on the net either give the width a static value or a percentage of the total. I would prefer it if I could simply find the length in dialog units or pixels of the max string to be displayed and set the width according to that. So far I have found no function which is able to find the length of a string in this manner? Is there a method to doing this or would it be easier to do something like (chars_in_string * static_size)?
-
I have created a Listview box which would display data in the Report mode. When filling in the header columns is it possible to set the width to correctly display the full text string? Most of the examples I have found on the net either give the width a static value or a percentage of the total. I would prefer it if I could simply find the length in dialog units or pixels of the max string to be displayed and set the width according to that. So far I have found no function which is able to find the length of a string in this manner? Is there a method to doing this or would it be easier to do something like (chars_in_string * static_size)?
It's been a while since I did this, so if I don't get it exactly right, I apologize. There is a member function of CListCtrl::GetStringWidth(), though I seem to recall it doesn't quite work as stated in the documentation. I seem to recall having to pad it slightly, but I may be mistaken. Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
I have created a Listview box which would display data in the Report mode. When filling in the header columns is it possible to set the width to correctly display the full text string? Most of the examples I have found on the net either give the width a static value or a percentage of the total. I would prefer it if I could simply find the length in dialog units or pixels of the max string to be displayed and set the width according to that. So far I have found no function which is able to find the length of a string in this manner? Is there a method to doing this or would it be easier to do something like (chars_in_string * static_size)?
Following on from Joes comment above. Using the LVM_SETCOLUMNWIDTH[^] message, pass
LVSCW_AUTOSIZE
as the column width and the ListView will automaticaly resize the column to fit the longest string. Gavin Taylor w: http://www.gavspace.com -
I have created a Listview box which would display data in the Report mode. When filling in the header columns is it possible to set the width to correctly display the full text string? Most of the examples I have found on the net either give the width a static value or a percentage of the total. I would prefer it if I could simply find the length in dialog units or pixels of the max string to be displayed and set the width according to that. So far I have found no function which is able to find the length of a string in this manner? Is there a method to doing this or would it be easier to do something like (chars_in_string * static_size)?
funny we are working on the same thing at the same time. i found a function calculating the width of a string on MSDN. the column here contains numbers only so my code is
HDC hdc = GetDC( _hView );
SIZE size;
BOOL res = GetTextExtentPoint32( hdc, TEXT( "0" ), 1, &size );
ReleaseDC( _hView, hdc );
const int iWidth = res ? ( size.cx * _iDigits ) : 120;this function can be still helpfull when you want to size for the largest string of all, not just the visible ones (which
LVSCW_AUTOSIZE
does). also i found out you can size the last column to take as much space as possible without adding the horizontal scrollbar (unlikeLVSCW_AUTOSIZE_USEHEADER
):RECT r;
GetClientRect( _hView, &r );
const int iWidth = ListView_GetColumnWidth( _hView, 0 );
ListView_SetColumnWidth( _hView, 1, r.right - r.left - iWidth );best regards, sebastian ------------------------------------------- My website: http://www.hartwork.org
-
funny we are working on the same thing at the same time. i found a function calculating the width of a string on MSDN. the column here contains numbers only so my code is
HDC hdc = GetDC( _hView );
SIZE size;
BOOL res = GetTextExtentPoint32( hdc, TEXT( "0" ), 1, &size );
ReleaseDC( _hView, hdc );
const int iWidth = res ? ( size.cx * _iDigits ) : 120;this function can be still helpfull when you want to size for the largest string of all, not just the visible ones (which
LVSCW_AUTOSIZE
does). also i found out you can size the last column to take as much space as possible without adding the horizontal scrollbar (unlikeLVSCW_AUTOSIZE_USEHEADER
):RECT r;
GetClientRect( _hView, &r );
const int iWidth = ListView_GetColumnWidth( _hView, 0 );
ListView_SetColumnWidth( _hView, 1, r.right - r.left - iWidth );best regards, sebastian ------------------------------------------- My website: http://www.hartwork.org
Thanks for the info. You say that LVSCW_AUTOSIZE only allows for the width of the visible items, I'm not sure I understand cleary. 1. The header items only? 2. Those which are in view due to scroll limits? I will play around with it when I get home, thanks again.
-
Thanks for the info. You say that LVSCW_AUTOSIZE only allows for the width of the visible items, I'm not sure I understand cleary. 1. The header items only? 2. Those which are in view due to scroll limits? I will play around with it when I get home, thanks again.
i'm not sure if it got you right but i guess number 2 is what i meant i'll try again with more details: my column contains numbers from 1 to 4000. when the current view is at the very top you can only see numbers with one or tow digits. applying
LVSCW_AUTOSIZE
now sets the size to two digits not four since the big number are out of view and ignored. maybe i should mention my column header has "#" as title so it is even shorter than the numbers. i'm not sure what happens if the header text is longer than all the row text. i'm using the control in virtual/getdispinfo mode maybe that's also playing in here!? btw i had trouble with two-digit numbers shown as "1..." so the current text width code isconst int iWidth = res ? ( int )( size.cx * ( _iDigits + 0.25f ) ) : 120;
i hope i put it clearly. i'm not a native speaker. best regards, sebastian ------------------------------------------- My website: http://www.hartwork.org
-
i'm not sure if it got you right but i guess number 2 is what i meant i'll try again with more details: my column contains numbers from 1 to 4000. when the current view is at the very top you can only see numbers with one or tow digits. applying
LVSCW_AUTOSIZE
now sets the size to two digits not four since the big number are out of view and ignored. maybe i should mention my column header has "#" as title so it is even shorter than the numbers. i'm not sure what happens if the header text is longer than all the row text. i'm using the control in virtual/getdispinfo mode maybe that's also playing in here!? btw i had trouble with two-digit numbers shown as "1..." so the current text width code isconst int iWidth = res ? ( int )( size.cx * ( _iDigits + 0.25f ) ) : 120;
i hope i put it clearly. i'm not a native speaker. best regards, sebastian ------------------------------------------- My website: http://www.hartwork.org
Thanks Sebastian, you have answered my questions clearly and with precission, very much appreciated :)