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. Displaying as much text as possible

Displaying as much text as possible

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsdebuggingperformancehelptutorial
6 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.
  • C Offline
    C Offline
    Code o mat
    wrote on last edited by
    #1

    Hello folks! Am looking for a way to do this: i have a rectangle that specifies the available space for some text. I want to display as much as possible of this text. What i mean is, if the text fits nicely in the rectangle, it should be displayed as usual, centered with wordbreaks as needed (DrawText with DT_WORDBREAK | DT_CENTER). However, when the specified rectangle is too small, the text doesn't fit into it even with wordbreaks (e.g. there are a few long words) then i'd like the text to e.g wrap around at positions where the long words would "hang out" of the rectangle. First i tried DT_CALCRECT with DrawText to check if the text fits, if yes then i simply draw it with DrawText and go on, if not, then i tokenized the string using space as separator/delimiter. Then i iterated trough the words and checked the widths of the words and if they were too wide then i'd find the character inside the word which would hang out of the rectange and inserted a space. After this, i used DrawText again with DT_WORDBREAK to display the string. This works reasonably well but not well enough because it produces things like this: (the text is "This is somewhat longer" and there's space for 3 lines)

    ---------
    |This is|
    |somewha|
    | t | <- a whole word is lost ("longer")

    I guess one alternative i can go for is wrapping the whole string

    ---------
    |This is|
    |somewha|
    |t longe| <- i see more of "longer" here

    but am not sure if doing this is the only/best way, and also i don't know how to wrap text aside of writing it character by character myself checking before each letter if it would hang out or not and "linefeeding" as needed + handling spaces specially (something like not display them if they are at a linebreak position). I have a faint memory from the good old times that something like DT_WORDWRAP or somesuch existed but i see no trace of it now so it's either gone or it never had existed at all and i have a few bit errors in my memory. Any suggestions before i go into implementing the char-by-char thing? There should be a better way (i don't wish to go into dynamically selecting font sizes, let us assume the font is fixed)... P.S: Am using GDI (CDC::DrawText and friends...)

    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got bet

    P L 2 Replies Last reply
    0
    • C Code o mat

      Hello folks! Am looking for a way to do this: i have a rectangle that specifies the available space for some text. I want to display as much as possible of this text. What i mean is, if the text fits nicely in the rectangle, it should be displayed as usual, centered with wordbreaks as needed (DrawText with DT_WORDBREAK | DT_CENTER). However, when the specified rectangle is too small, the text doesn't fit into it even with wordbreaks (e.g. there are a few long words) then i'd like the text to e.g wrap around at positions where the long words would "hang out" of the rectangle. First i tried DT_CALCRECT with DrawText to check if the text fits, if yes then i simply draw it with DrawText and go on, if not, then i tokenized the string using space as separator/delimiter. Then i iterated trough the words and checked the widths of the words and if they were too wide then i'd find the character inside the word which would hang out of the rectange and inserted a space. After this, i used DrawText again with DT_WORDBREAK to display the string. This works reasonably well but not well enough because it produces things like this: (the text is "This is somewhat longer" and there's space for 3 lines)

      ---------
      |This is|
      |somewha|
      | t | <- a whole word is lost ("longer")

      I guess one alternative i can go for is wrapping the whole string

      ---------
      |This is|
      |somewha|
      |t longe| <- i see more of "longer" here

      but am not sure if doing this is the only/best way, and also i don't know how to wrap text aside of writing it character by character myself checking before each letter if it would hang out or not and "linefeeding" as needed + handling spaces specially (something like not display them if they are at a linebreak position). I have a faint memory from the good old times that something like DT_WORDWRAP or somesuch existed but i see no trace of it now so it's either gone or it never had existed at all and i have a few bit errors in my memory. Any suggestions before i go into implementing the char-by-char thing? There should be a better way (i don't wish to go into dynamically selecting font sizes, let us assume the font is fixed)... P.S: Am using GDI (CDC::DrawText and friends...)

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got bet

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      Don't know who downvoted you, but have my 5 in compensation. It's an interesting question (even if the answer is going to be WAAAAY too long). I suspect you're going to be forced into "measure each char and draw it somewhere" stuff. Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994.

      C 1 Reply Last reply
      0
      • P Peter_in_2780

        Don't know who downvoted you, but have my 5 in compensation. It's an interesting question (even if the answer is going to be WAAAAY too long). I suspect you're going to be forced into "measure each char and draw it somewhere" stuff. Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994.

        C Offline
        C Offline
        Code o mat
        wrote on last edited by
        #3

        Thank you very much. I don't know what the problem is with my question that it needed a downvote. Anyways, for now i went with the following aproach: if the text doesn't fit, i go char by char, calculate sizes, when i hit the "line-end" then i insert a linefeed ('\n') in the string and after i hit the end of the string, i output it with DrawText without DT_WORDBREAK.

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

        1 Reply Last reply
        0
        • C Code o mat

          Hello folks! Am looking for a way to do this: i have a rectangle that specifies the available space for some text. I want to display as much as possible of this text. What i mean is, if the text fits nicely in the rectangle, it should be displayed as usual, centered with wordbreaks as needed (DrawText with DT_WORDBREAK | DT_CENTER). However, when the specified rectangle is too small, the text doesn't fit into it even with wordbreaks (e.g. there are a few long words) then i'd like the text to e.g wrap around at positions where the long words would "hang out" of the rectangle. First i tried DT_CALCRECT with DrawText to check if the text fits, if yes then i simply draw it with DrawText and go on, if not, then i tokenized the string using space as separator/delimiter. Then i iterated trough the words and checked the widths of the words and if they were too wide then i'd find the character inside the word which would hang out of the rectange and inserted a space. After this, i used DrawText again with DT_WORDBREAK to display the string. This works reasonably well but not well enough because it produces things like this: (the text is "This is somewhat longer" and there's space for 3 lines)

          ---------
          |This is|
          |somewha|
          | t | <- a whole word is lost ("longer")

          I guess one alternative i can go for is wrapping the whole string

          ---------
          |This is|
          |somewha|
          |t longe| <- i see more of "longer" here

          but am not sure if doing this is the only/best way, and also i don't know how to wrap text aside of writing it character by character myself checking before each letter if it would hang out or not and "linefeeding" as needed + handling spaces specially (something like not display them if they are at a linebreak position). I have a faint memory from the good old times that something like DT_WORDWRAP or somesuch existed but i see no trace of it now so it's either gone or it never had existed at all and i have a few bit errors in my memory. Any suggestions before i go into implementing the char-by-char thing? There should be a better way (i don't wish to go into dynamically selecting font sizes, let us assume the font is fixed)... P.S: Am using GDI (CDC::DrawText and friends...)

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got bet

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Isn't this why we have scrollbars?

          Just say 'NO' to evaluated arguments for diadic functions! Ash

          L C 2 Replies Last reply
          0
          • L Lost User

            Isn't this why we have scrollbars?

            Just say 'NO' to evaluated arguments for diadic functions! Ash

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            No, this is why we have larger monitors. :laugh:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            1 Reply Last reply
            0
            • L Lost User

              Isn't this why we have scrollbars?

              Just say 'NO' to evaluated arguments for diadic functions! Ash

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #6

              Thanks for the suggestion but not in this situation. Basicly what i have is (something similar to) a "grid" of rectangular cells, each cell displaying textual information for the user, the user can resize this "grid", and i would like to present him as much information as i can when he "shrinks" the whole thing. Displaying a scrollbar wouldn'd be really usefull here i guess. The user will have tooltips to aid him but the more he can determine by just looking at the thing the better it should be.

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Leela: Fry, you're wasting your life sitting in front of that TV. You need to get out and see the real world. Fry: But this is HDTV. It's got better resolution than the real world <

              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