Wrapped text hittest: Good way to do it?

G

goldpython

I'm working in C#, .NET framework 2.0.

I'm writing a text editor and wanted a finer degree of control over the
window that a normal edit box will allow, and so decided to build the
display beginning with a plain window and handle all the drawing
myself.

All the text in the screen is drawn with a single DrawString because I
am attempting to take advantage of Windows wrapping the text for me
when I draw a long string in a bounding rectangle. (I'm wondering if
that's really best now.)

The issue is writing the code to take a point-usually from a mouse
click-and convert it to the position in the string of text contained
in the window, for example, point(50,20) lands on txt[8].

I'm using MeasureCharacterRanges and binary searching the string and
that works fine when the hit is on the text proper.

Things become problematic when the click lands in the right margin and
the cursor should be placed at the end of a line. The problem arises
because it is tedious to figure out where Windows did the word wrap.
Now, if I measure a single character, if that character is the one
where the wrap occurs, a call to GetRegionScans returns an empty array
of rectangles. Using that fact, one can find the line ending.

Blank lines are an issue as well, as when measured, GetRegionScans
returns an empty array of rectangles for them, too. Some scheme
involving knowing rectangles above or below the blank lines is
necessary to locate the line and test for the hit.

So. . . The question: Is there an easier way?

On large strings, say 2000 characters, things get slow because of all
the measures. I can pre-measure the text as a set of small regions,
perhaps 64 characters, and that will speed things up quite a bit, but
the blank lines and right margins are just plain tedious.

Any help is very much appreciated.

Thanks,
gp
 
G

goldpython

For the sake of closure: It turned out much simpler to do my own word
wrapping. That way I know exactly what's on each line.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top