I'm using custom drawing to draw a string in a WinForms application. The classic Graphics.DrawString method is used for that:
g.DrawString(text, font, brush, rect, stringFormat);
The output string text
can be clipped by the rectangle rect
, and I need to count the number of characters fully visible in rect
. How to do that using the .NET Framework built-in tools?
Please, don't suggest using other methods to output text. I need to solve this task for GDI+ Graphics.DrawString.
UPDATE: Yes, sure, I can use Graphics.MeasureString in a loop in which I sum the width of every character from the beginning of the string, but I need something more efficient like the DT_MODIFYSTRING
flag in the WinAPI DrawText function.
Add to this that Graphics.MeasureString
does not calculate the required width properly. Fore more info, see this article:
http://www.codeproject.com/Articles/2118/Bypass-Graphics-MeasureString-limitations