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. The Lounge
  3. Wrapping text within a CListBox

Wrapping text within a CListBox

Scheduled Pinned Locked Moved The Lounge
c++helpquestion
6 Posts 5 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.
  • B Offline
    B Offline
    Baz
    wrote on last edited by
    #1

    Wondering if anyone can help....:confused: I am using the standard CListBox and have realised that some of the text I use does not fit on a single line. Is there any (and hopefully easy :-D ) way to wrap the text onto the next line, but still have the whole of that text message highlighted when selected, even though it may be on separate lines? Being a relative new-comer to MFC and Windows programming, I don't really know where to start. I thought of using FontMetrics and the size of the text string, and then using that with the size of the box, but then my head hurt.... there must be another, easier way - surely!! Please help!

    L S 2 Replies Last reply
    0
    • B Baz

      Wondering if anyone can help....:confused: I am using the standard CListBox and have realised that some of the text I use does not fit on a single line. Is there any (and hopefully easy :-D ) way to wrap the text onto the next line, but still have the whole of that text message highlighted when selected, even though it may be on separate lines? Being a relative new-comer to MFC and Windows programming, I don't really know where to start. I thought of using FontMetrics and the size of the text string, and then using that with the size of the box, but then my head hurt.... there must be another, easier way - surely!! Please help!

      L Offline
      L Offline
      l a u r e n
      wrote on last edited by
      #2

      i dont think you can wrap the text in a listbox onto more than one line ... thats why they gave it a horiz scroll bar and info tips / tool tips someone might know better than i on this --- "every year we invent better idiot proof systems and every year they invent better idiots"

      1 Reply Last reply
      0
      • B Baz

        Wondering if anyone can help....:confused: I am using the standard CListBox and have realised that some of the text I use does not fit on a single line. Is there any (and hopefully easy :-D ) way to wrap the text onto the next line, but still have the whole of that text message highlighted when selected, even though it may be on separate lines? Being a relative new-comer to MFC and Windows programming, I don't really know where to start. I thought of using FontMetrics and the size of the text string, and then using that with the size of the box, but then my head hurt.... there must be another, easier way - surely!! Please help!

        S Offline
        S Offline
        Sir Gras of Berger
        wrote on last edited by
        #3

        The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.

        T J B 3 Replies Last reply
        0
        • S Sir Gras of Berger

          The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.

          T Offline
          T Offline
          Thomas Freudenberg
          wrote on last edited by
          #4

          In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text.


          You have to use the flag DT_CALCRECT. MSDN says:

          DT_CALCRECT Determines the width and height of the rectangle. If there are multiple lines of text, DrawText will use the width of the rectangle pointed to by lpRect and extend the base of the rectangle to bound the last line of text. If there is only one line of text, DrawText will modify the right side of the rectangle so that it bounds the last character in the line. In either case, DrawText returns the height of the formatted text, but does not draw the text.

          HTH, Thomas


          I am a signature virus! Help me spread and copy me to your sig!

          1 Reply Last reply
          0
          • S Sir Gras of Berger

            The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.

            J Offline
            J Offline
            Jonas Larsson
            wrote on last edited by
            #5

            Or you could make it customdraw. I think there's an article or 2 here that addresses customdraw.

            1 Reply Last reply
            0
            • S Sir Gras of Berger

              The easiest way I can think of is to make it an owner-draw list box (LBS_OWNERDRAWVARIBLE). In MeasureItem(), create a compatible DC and use DrawText() with a rectangle the proper width, but the maximum height you think you'll need. If I read the docs correctly, DrawText() should return the height needed to draw the text. In DrawItem(), use DrawText() with the actual DC to wrap the text inside the available rectangle. In both cases, you should probably select the windows font into the DC before drawing, and restore it afterward. Unfortunately, you're going to have to do all the text/background color management yourself too, not to mention the focus rectangle.

              B Offline
              B Offline
              Baz
              wrote on last edited by
              #6

              Cheers for that Guys! My problem now is getting the text to wrap! I have got the text/background size correct and can change the text/background colour when the item is selected, and also add the focus rectangle no probs... just don't know how to make the text I'm adding wrap round properly so it is not clipped Any more ideas?

              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