How to word wrap an edit control

I'm looking to make a basic edit box word wrap the text in it. I've resized the edit box control to a decent size and I'd like to have the text that is typed into it word wrap. I'm using Visual Studio 2010 and I've set the multiline property to true. Searching online for this yields search results about how to set word wrap properties within the Visual Studio program, not in my actual edit control. I may be getting close with results like EditWordBreakProc and EM_SETWORDBREAKPROC but I'm not finding how to use them.
I ended up creating my own class that handles text functions like word wrapping and font changes.
If you were to go that way then you would probably want to know about GetTextExtentPoint32() which basically looks at an hdc and tells you how long and how high your string is in pixels.
Hljodulfr wrote:
I may be getting close with results like EditWordBreakProc and EM_SETWORDBREAKPROC but I'm not finding how to use them.
There is an example on this page http://support.microsoft.com/kb/109551
EditWordBreakProc and EM_SETWORDBREAKPROC is the right way to go.

Google is surprisingly quiet on this subject. naraku9333 has already pointed you at the MSDN Knowledge base article. There is another example on this page:

Edit Controls
http://msdn.microsoft.com/en-us/library/ms997530.aspx

It's the kind of thing I expected to be present on codeproject.com or codeguru.com, but I didn't get any useful hits from either of these places (both usually good places for this kind of thing, even if the people there are over fond of MFC...)

I also found this example, in case you want to seean example of actual use:
https://bmgame.googlecode.com/svn/tools/debugger/DebuggerWindow.cpp

Andy

PS Also found this:

Using Edit Controls
http://blog.yezhucn.com/shellcc/usingeditcontrols.htm

But searching Microsoft's samples at:
http://code.msdn.microsoft.com/
for EM_SETWORDBREAKPROC found nothing. :-(
Last edited on
I've been going over those links and trying to see what I can use and I'm not having any luck.

When reading this link (http://support.microsoft.com/kb/109551), I noticed this sentence:

The default wordwrap function breaks a line in a multiline edit control (MLE) at a space character.


What I am trying to figure out is if that is the default behavior, is there a setting I'm missing somewhere? Because the code on that page is custom code to get the edit control to wordwrap at the ~ character. Still, I tried to implement it (and replaced the ~ with a space in the code) with no luck. I'm thinking that I put everything where it was supposed to go from that page except for the WordBreakProc function. I put the prototype at the top, I put the SetDlgItemMessage under WM_INITDIALOG and I put the WordBreakProc function at the bottom of the page. I'm a little fuzzy there on where exactly it is supposed to go.

At any rate, when I try to use that code, I get the message that lpWrdBrkProc was undefined. I'm not sure what lpWrdBrkProc is or where it is supposed to come from but the code on that page doesn't have it declared anywhere.

I've also tried using this line from the yezhucn link:

1
2
3
4
SendMessage(hwndEdit, 
                                EM_SETWORDBREAKPROC, 
                                (WPARAM) 0, 
                                (LPARAM) (EDITWORDBREAKPROC) WordBreakProc);


No luck there either. There isn't a WordBreakProc function on that page so I used the one from the first link.

I'm not sure what to do from here. Over the past month I have been getting to where I can (just!) understand how to utilize sample code and tweak it to suit my needs.
This is just a guess, but you can try passing the address of WordBreakProc
1
2
3
4
SendMessage(hwndEdit, 
                        EM_SETWORDBREAKPROC, 
                        (WPARAM) 0, 
                        (LPARAM) (EDITWORDBREAKPROC) &WordBreakProc);
Sorry, I obviously didn't read your opening post closely enough.

If you want to wrap at spaces, then you just need to enable the default wrapping.

1. check the multiline
2. uncheck the auto horizontal scrolling

You might also need

3. want return

The WordBreakProc is only required if you want to something other than break at spaces.

Andy
Last edited on
Turning off auto horizontal scrolling did the trick!
OK

Note the "want return" setting doesn't effect the line breaking. But it does determine whether the return key insert a line break or clicks the default push button.

Andy
Topic archived. No new replies allowed.