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. Listview controls

Listview controls

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 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.
  • W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #1

    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)?

    J G S 3 Replies Last reply
    0
    • W Waldermort

      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)?

      J Offline
      J Offline
      Joe Woodbury
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • W Waldermort

        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)?

        G Offline
        G Offline
        Gavin Taylor
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • W Waldermort

          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)?

          S Offline
          S Offline
          Sebastian Pipping
          wrote on last edited by
          #4

          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 (unlike LVSCW_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

          W 1 Reply Last reply
          0
          • S Sebastian Pipping

            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 (unlike LVSCW_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

            W Offline
            W Offline
            Waldermort
            wrote on last edited by
            #5

            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.

            S 1 Reply Last reply
            0
            • W Waldermort

              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.

              S Offline
              S Offline
              Sebastian Pipping
              wrote on last edited by
              #6

              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 is

              const 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

              W 1 Reply Last reply
              0
              • S Sebastian Pipping

                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 is

                const 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

                W Offline
                W Offline
                Waldermort
                wrote on last edited by
                #7

                Thanks Sebastian, you have answered my questions clearly and with precission, very much appreciated :)

                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