I’m using C# WinForms and GDI+ to do something I hoped wouldn’t be too much problem but…
I’m basically trying to draw a string within a rectangle that has highlighted sections within the string. This all works fine when printing on one line, but I have issues when trying to wrap the text onto the next line within the rectangle.
The algorithm used is as follows: –
Split strings into a collection of highlight and not highlight.
Do
If Highlightedtext Then
DrawString(HighLightedText);
Move X position forward to next character space
Else
DrawString(NormalText);
Move X position forward to next character space
End If
Loop
I would put the code in, but it’s messy and long (i’m maintaining it). It’ll print out find if the text is one string of either highlighting or not, as it’ll wrap it within the bounds of the rectangle without issue if it’s too long. If it’s multiple highlighting and the string is bigger than the rectangle, it’ll write outside of it… this is because the “move X position forward…” just moves the rectangle on which is a problem!
I want to essentially move the point the text is printed within the original rectangle and print it on the next line if wrapping is required. Can anyone assist with this? It’s a real pain!
I’ve managed to sort this by having to make my function do one character at a time.
To do this, I made a function to get an array (which is the length of the string itself) of boolean values which have set any highlighted characters to true.
(FindMatchingTerms() is a function that will look in the string and return the matches found into a collection).
I then loop this array to draw it out to screen but keeping track of my rectangle border width. When it reduces to the relevant size, I reset the position of drawing back to the start and then move the starting Y position down a bit.
Hopefully someone else will find this useful 🙂 I’ve got some constants in there for spaceCharacter and hypenCharacter which should be self explanatory! There are custom functions to draw the string, but it should make sense nonetheless, hope it helps anyone else.