Hi all. I realize this MeasureString topic has been
tossed around quite a bit but I can't seem to make it work
well despite trying out all the postings. I implemented
all I tried in the little function at the end of this
posting.
I've been trying to create some text separated into
columns by spaces. (I can't use tabs to make these spaces
between columns because this text ultimately goes on a
device that doesn't support tabs.) Basically what i do is
1. get the cells contents and put it into the output text
2. keep adding spaces to the text until it reaches the
column size
3. do the same for all the columns in the cells and for
each row.
Unfortunately even though MeasureString says the distance
between the first and second column is nearly the same for
all rows, it always looks like this:
first_column second_column
stuff1 455.445
otherstuff2 593.558
morestuff3 89.88
According to MeasureString the distance between the first
and second column are minimal for each row but to the eye
its quite large. Can anyone explain this is or point me
in the right direction? Thank you!
public static float GetStringWidth(string text,Font f)
{
StringFormat format= (StringFormat)
StringFormat.GenericTypographic.Clone();
format.FormatFlags=StringFormatFlags.MeasureTrailingSpaces
| StringFormatFlags.NoClip
| StringFormatFlags.NoWrap
|
StringFormatFlags.DisplayFormatControl
| StringFormatFlags.NoFontFallback;
format.Trimming=StringTrimming.None;
System.Windows.Forms.Control myControl = new
System.Windows.Forms.Control();
Graphics TempGraphic = myControl.CreateGraphics();
TempGraphic.TextRenderingHint =
TextRenderingHint.AntiAlias;
float StringSize = TempGraphic.MeasureString
(text,f,0xffff,format).Width;
TempGraphic.Dispose();
TempGraphic = null;
myControl.Dispose();
myControl = null;
return StringSize;
}
|