首先,這是一個相當棘手的問題得到妥善解決 - 你可能會更好試圖找到一個現有的控制(例如,RichTextBox),解決了這個要求。
這就是說,如果你確實想這樣做,那麼這或多或少是解決這個問題的正確方法,但是如果你看看MSDN documentation,你會注意到你看到的行爲是故意的
的MeasureString方法設計用於使用與各個串 ,並且包括之前和字符串 後的額外的空間少量以允許懸垂字形。此外,DrawString方法會調整 字形點以優化顯示質量,並可能會顯示比MeasureString報告的字符串更窄的字符串 。爲了獲得合適的 爲相鄰串中的佈局的度量(例如,實施 格式化文本時),則使用MeasureCharacterRanges方法或的 MeasureString方法,需要一個一個StringFormat,並傳遞 GenericTypographic。另外,請確保TextRenderingHint爲 Graphics爲AntiAlias。
所以它聽起來像你對我應該改爲使用Graphics.MeasureCharacterRanges Method代替。
這是我準備的一個樣本,它處理呈現兩種不同顏色的文本。嘗試一下,將其粘貼到一個新的形式
protected override void OnPaint(PaintEventArgs e)
{
// This is where we wish to print our string
var region = new RectangleF(50, 50, 200, 50);
// This is the font we wish to use
var font = new Font("Times New Roman", 16.0F);
// Draw a string for comparison
DrawString(e.Graphics, "RedBlack", font, Brushes.Black, new RectangleF(50, 150, 200, 50));
// Draw the first string and keep a track of the Region it was rendered in
var first = DrawString(e.Graphics, "Red", font, Brushes.Red, region);
// Adjust the region we wish to print
region = new RectangleF(region.X + first.GetBounds(e.Graphics).Width, region.Y, region.Width, region.Height);
// Draw the second string
DrawString(e.Graphics, "Black", font, Brushes.Black, region);
base.OnPaint(e);
}
private Region DrawString(Graphics g, string s, Font font, Brush brush, RectangleF layoutRectangle)
{
var format = new StringFormat();
format.SetMeasurableCharacterRanges(new[] { new CharacterRange(0, s.Length) });
g.DrawString(s, font, brush, layoutRectangle, format);
return g.MeasureCharacterRanges(s, font, layoutRectangle, format)[0];
}
這是什麼樣子
![screenshot of form](https://i.stack.imgur.com/6C4wX.png)
請注意,你需要小心剪裁 - GDI將「打包」呈現的文本在你新的生產線在默認情況下,但是這不會工作了,你最終會像這樣的東西
![screenshot of form with incorrect line wrapping](https://i.stack.imgur.com/U123U.png)
此外,如果您嘗試打印出不同字體/字體大小的文本,那麼這些字體的「底部」將不會排列在您期望的位置。試着看看Formatting text on a common baseline關於如何處理這個問題的一些提示。
查看方法「remarks」@ http://msdn.microsoft.com/en-us/library/6xe5hazb.aspx - 它解釋了額外的間距(懸垂的字形)和注意到'MeasureCharacterRanges'是什麼用於精確的佈局計算。 – 2012-08-07 10:49:08
謝謝,它的作品。 – 2012-08-07 11:10:54