Fog Creek Software
Discussion Board

Text Justification w/ RichTextBox

I don't normally post 'need advice' posts, but I figured I'd give it a try and see if anyone has experience with the following:

Has anyone implemented text justification functionality in a RichTextBox?  (Justification evenly spaces all words on a line so that the line of text spans the whole printable "line")

If you did, would you mind sharing how you did it?  (i.e. Did you use the Win32 API GetTextExtent32 and/or RichTextBox messaging or some other method?)

I have searched on the Internet/MSDN for a solution for this, but can't seem to find one.  Basically one last chance for advice before I tackle the problem myself.

Dave B.
Thursday, May 22, 2003

you must have some strange reason to want it. Text boxes are normally for entering data; it would be must unsettling to try and do it this way.

Can you let us in the secret of what you are planning?

Stephen Jones
Thursday, May 22, 2003

I vaguely remember doing something similar years ago (in VB). I think it was done using brute force and ignorance (ie by adding the correct number of spaces evenly across each line of text).

I'll dig out the source this evening if you can live with something that smells of 'nasty hack'.

The alternative is to create a custom control, with a property which implements the justification and passes everything else straight through to the encapsulated control.

Thursday, May 22, 2003

You can't.

The way I know this is that there's no UI way to do it in Microsoft WordPad, which is just a thin wrapper around the Rich Edit Control.

You may want to switch to mshtml for editing, then you could do it with html styles.

Joel Spolsky
Thursday, May 22, 2003

The reason for doing it is to make the text, when printed using the FORMATRANGE/CHARRANGE structures, align evenly on both the left and right sides of a column so as to give a pleasing appearance.

I could write my own custom routines that format the text and place it on the page, but it would save some time if I could use the RichTextBox API.

Thanks for the offer of the code Justin but I believe I will either not include this ability in the application or I'll write the custom routines necessary to format the text depending on how much time I have.

Dave B.
Thursday, May 22, 2003

Thanks Joel.  I'll look into mshtml for the editing part.

By the way, I read your article in Programmer's Paradise.  Which part of CityDesk got optimized the most? and Dim statements in the middle of a sub? I do the same thing. :)

Dave B.
Thursday, May 22, 2003

This is a reporting function, right?  Doesn't Crystal Reports have the ability to display Rich Text on a report?  If so, I can't imagine that it doesn't provide for full justification (Nit: "justification" by itself doesn't mean right and left even alignment.  There's left justification (ragged right), right justification (ragged left), center justification, and full justification, what you're looking for).

Karl Perry
Thursday, May 22, 2003

You might want to take a look at the PARAFORMAT2 structure. There's a wAlignment member in there that might work for you. I haven't used it, so it might or might not be what you're looking for.

Good luck,

Thursday, May 22, 2003


What I am trying to accomplish is not (strictly speaking) a reporting function and as such I can't use Crystal Reports.  And yea you're right If I wanted to be technical about it I should use the term "full justification"...


Thanks, I'll take a look at the PARAFORMAT2 struct.

Dave B.
Thursday, May 22, 2003

Take a look at the Text Object Model, specifically ITextPara::SetAlignment. 

Accessing TOM isn't for the faint of heart but there's tremondous power available once you get to it.  You have to send an EM_GETOLEINTERFACE message to the control to get a pointer to IRichEditOle.  Then you have to query that for ITextDocument and work from there.

Thursday, May 22, 2003

I´m working with richtextexbox tool on Visual Basic 6.0, but, i cant´n to do that full alignment in the richteextbox

Alcides Valero
Monday, April 05, 2004

*  Recent Topics

*  Fog Creek Home