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. CEdit::LineLength returning zero

CEdit::LineLength returning zero

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelp
15 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I want to limit the number of characters on a line in a multiline edit control I figured I would do CEdit::linelength and divide that by rect.right From getclientrect that would get me the number of pixels per character I would then multiply it by the number of characters I want on a line however Cedit:Linelength return zero my edit control is declared CEdit mycontrol I know under the covers it does a SendMessage EM_LINELENGTH Is there a issue that I dont have a message map The character index is 0

    R L D 3 Replies Last reply
    0
    • F ForNow

      Hi I want to limit the number of characters on a line in a multiline edit control I figured I would do CEdit::linelength and divide that by rect.right From getclientrect that would get me the number of pixels per character I would then multiply it by the number of characters I want on a line however Cedit:Linelength return zero my edit control is declared CEdit mycontrol I know under the covers it does a SendMessage EM_LINELENGTH Is there a issue that I dont have a message map The character index is 0

      R Offline
      R Offline
      Rick York
      wrote on last edited by
      #2

      Did you pass a valid line number to it like 0? If you pass it -1 "the return value is the number of unselected characters in the lines that contain selected characters" according to the documentation. You will have to do the length limiting of each line yourself because LimitText and SetLimitText define how many total characters the control can accept, not just on one line. Also, to get the number of pixels per character you should call GetTextExtent which is a member of the CDC class. The typical way is to generate a string with all upper and lower case letters, call GetTextExtent with that string, and then divide the resulting width by the string length to get an average for each character. There are lots of examples that show how to do this.

      F 1 Reply Last reply
      0
      • R Rick York

        Did you pass a valid line number to it like 0? If you pass it -1 "the return value is the number of unselected characters in the lines that contain selected characters" according to the documentation. You will have to do the length limiting of each line yourself because LimitText and SetLimitText define how many total characters the control can accept, not just on one line. Also, to get the number of pixels per character you should call GetTextExtent which is a member of the CDC class. The typical way is to generate a string with all upper and lower case letters, call GetTextExtent with that string, and then divide the resulting width by the string length to get an average for each character. There are lots of examples that show how to do this.

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        I did pass 0 is the index and still it returned 0 I do use The GetTextExtent but thought that LineLength would be easier This is the code I used to figured out how much space I needed per line I was looking to allow the user up to 8 hex characters (by that I mean 0 -F ) It just didn't come up exactly the way I figured

        CSize mycsize = gprdc->GetTextExtent(_T("0123456789ABCDEF"), 16);
        mycsize.cx = mycsize.cx / 16;
        RECT gprrect, gprrect1, accessrect;

        WINDOWINFO dlgwin;
        GetWindowInfo(&dlgwin);
        asidgprs.GetWindowRect(&gprrect);
        asidaccess.GetWindowRect(&accessrect);
        int height = gprrect.bottom - gprrect.top;
        gprrect.left = (gprrect.left - dlgwin.rcWindow.left) + dlgwin.cxWindowBorders;
        accessrect.left = (accessrect.left - dlgwin.rcWindow.left) + dlgwin.cxWindowBorders;
        gprrect.top = gprrect.top - ( dlgwin.rcWindow.top + dlgwin.cyWindowBorders + 20) ;
        accessrect.top = accessrect.top - ( dlgwin.rcWindow.top + dlgwin.cyWindowBorders + 20);
        gprrect.bottom = ( gprrect.top + height) -20 ;
        accessrect.bottom = (accessrect.top + height) - 20;
        gprrect.right = gprrect.left + (mycsize.cx * 10);
        accessrect.right = accessrect.left + (mycsize.cx * 10);

        mycsize.cx represents the number of pixels per character originally I multiplied by 8 figuring 8 characters per line but I ended up multiplying by 10 to get 8 characters to fit asidgprs and asidacess are 2 multiline edit controls in the Dialog I ended doing a lot tweeking wit this to get it to the right size

        R 1 Reply Last reply
        0
        • F ForNow

          I did pass 0 is the index and still it returned 0 I do use The GetTextExtent but thought that LineLength would be easier This is the code I used to figured out how much space I needed per line I was looking to allow the user up to 8 hex characters (by that I mean 0 -F ) It just didn't come up exactly the way I figured

          CSize mycsize = gprdc->GetTextExtent(_T("0123456789ABCDEF"), 16);
          mycsize.cx = mycsize.cx / 16;
          RECT gprrect, gprrect1, accessrect;

          WINDOWINFO dlgwin;
          GetWindowInfo(&dlgwin);
          asidgprs.GetWindowRect(&gprrect);
          asidaccess.GetWindowRect(&accessrect);
          int height = gprrect.bottom - gprrect.top;
          gprrect.left = (gprrect.left - dlgwin.rcWindow.left) + dlgwin.cxWindowBorders;
          accessrect.left = (accessrect.left - dlgwin.rcWindow.left) + dlgwin.cxWindowBorders;
          gprrect.top = gprrect.top - ( dlgwin.rcWindow.top + dlgwin.cyWindowBorders + 20) ;
          accessrect.top = accessrect.top - ( dlgwin.rcWindow.top + dlgwin.cyWindowBorders + 20);
          gprrect.bottom = ( gprrect.top + height) -20 ;
          accessrect.bottom = (accessrect.top + height) - 20;
          gprrect.right = gprrect.left + (mycsize.cx * 10);
          accessrect.right = accessrect.left + (mycsize.cx * 10);

          mycsize.cx represents the number of pixels per character originally I multiplied by 8 figuring 8 characters per line but I ended up multiplying by 10 to get 8 characters to fit asidgprs and asidacess are 2 multiline edit controls in the Dialog I ended doing a lot tweeking wit this to get it to the right size

          R Offline
          R Offline
          Rick York
          wrote on last edited by
          #4

          Is there a reason you need to use multi-line edit controls of a fixed width? It seems like an array of single line edit controls might do what you need and it is easier to limit the length of data in them.

          F 1 Reply Last reply
          0
          • R Rick York

            Is there a reason you need to use multi-line edit controls of a fixed width? It seems like an array of single line edit controls might do what you need and it is easier to limit the length of data in them.

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            If they are all adjacent to each other they look like multi line ?

            1 Reply Last reply
            0
            • F ForNow

              Hi I want to limit the number of characters on a line in a multiline edit control I figured I would do CEdit::linelength and divide that by rect.right From getclientrect that would get me the number of pixels per character I would then multiply it by the number of characters I want on a line however Cedit:Linelength return zero my edit control is declared CEdit mycontrol I know under the covers it does a SendMessage EM_LINELENGTH Is there a issue that I dont have a message map The character index is 0

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

              This message returns the length of the existing text at that line, not the amount of space available. How many characters are in the control when you call this?

              F 1 Reply Last reply
              0
              • L Lost User

                This message returns the length of the existing text at that line, not the amount of space available. How many characters are in the control when you call this?

                F Offline
                F Offline
                ForNow
                wrote on last edited by
                #7

                Thanks that’s it I wanted to calculate the number of characters the control can hold on a line

                V 1 Reply Last reply
                0
                • F ForNow

                  Thanks that’s it I wanted to calculate the number of characters the control can hold on a line

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #8

                  ForNow wrote:

                  I wanted to calculate the number of characters the control can hold on a line

                  It depends on the font your control is currently using.

                  F 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    ForNow wrote:

                    I wanted to calculate the number of characters the control can hold on a line

                    It depends on the font your control is currently using.

                    F Offline
                    F Offline
                    ForNow
                    wrote on last edited by
                    #9

                    using the current font

                    V 1 Reply Last reply
                    0
                    • F ForNow

                      using the current font

                      V Offline
                      V Offline
                      Victor Nijegorodov
                      wrote on last edited by
                      #10

                      If your current font is a "proportional" one (like Arial, Times New, ...) then it is not possible to calculate the number of characters in a line in a common case. It will always depend of the text itself. Example: the text "WWW" needs much more pixels than "111". But for non-proportional fonts (like Courier New) both need the same number of pixels.

                      F 1 Reply Last reply
                      0
                      • V Victor Nijegorodov

                        If your current font is a "proportional" one (like Arial, Times New, ...) then it is not possible to calculate the number of characters in a line in a common case. It will always depend of the text itself. Example: the text "WWW" needs much more pixels than "111". But for non-proportional fonts (like Courier New) both need the same number of pixels.

                        F Offline
                        F Offline
                        ForNow
                        wrote on last edited by
                        #11

                        I understand that this is the font statement from resource file dialog definition "FONT 8, "MS Shell Dlg", 400, 0, 0x1" I basically editing for hex characters so my GetTTextExtent string is "0123456789ABCDEF" these characters seem like the size even without a Font like courier new

                        L 1 Reply Last reply
                        0
                        • F ForNow

                          Hi I want to limit the number of characters on a line in a multiline edit control I figured I would do CEdit::linelength and divide that by rect.right From getclientrect that would get me the number of pixels per character I would then multiply it by the number of characters I want on a line however Cedit:Linelength return zero my edit control is declared CEdit mycontrol I know under the covers it does a SendMessage EM_LINELENGTH Is there a issue that I dont have a message map The character index is 0

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          Have you considered deriving your own class from CEdit, something like:

                          class MyCustomEdit : public CEdit
                          {
                          }

                          Then as each character is typed into the control but before the control is updated, figure out what line the cursor is on (I haven't used MFC in a few years so I'm not sure what these two messages would be). If there is room, let the character through, otherwise not.

                          "One man's wage rise is another man's price increase." - Harold Wilson

                          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                          F 2 Replies Last reply
                          0
                          • D David Crow

                            Have you considered deriving your own class from CEdit, something like:

                            class MyCustomEdit : public CEdit
                            {
                            }

                            Then as each character is typed into the control but before the control is updated, figure out what line the cursor is on (I haven't used MFC in a few years so I'm not sure what these two messages would be). If there is room, let the character through, otherwise not.

                            "One man's wage rise is another man's price increase." - Harold Wilson

                            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                            F Offline
                            F Offline
                            ForNow
                            wrote on last edited by
                            #13

                            probably WM_CHAR and wparam is the keystroke truth that would also save me time on the editing as I can see if it is a valid hex character was entered rather then do GetWindowText to read the entire control Thanks

                            1 Reply Last reply
                            0
                            • F ForNow

                              I understand that this is the font statement from resource file dialog definition "FONT 8, "MS Shell Dlg", 400, 0, 0x1" I basically editing for hex characters so my GetTTextExtent string is "0123456789ABCDEF" these characters seem like the size even without a Font like courier new

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

                              That's a proportional font, a 1 will take far less space than an E. If spacing is important then you should use a fixed font in your edit control.

                              1 Reply Last reply
                              0
                              • D David Crow

                                Have you considered deriving your own class from CEdit, something like:

                                class MyCustomEdit : public CEdit
                                {
                                }

                                Then as each character is typed into the control but before the control is updated, figure out what line the cursor is on (I haven't used MFC in a few years so I'm not sure what these two messages would be). If there is room, let the character through, otherwise not.

                                "One man's wage rise is another man's price increase." - Harold Wilson

                                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                                F Offline
                                F Offline
                                ForNow
                                wrote on last edited by
                                #15

                                This class may provide all the functionality of a DDV macro

                                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