| 在打印时如何度量字符串 |
|
|
|
作者:佚名 文章来源:不详 点击数: 更新时间:2006-12-5 20:09:26  |
在打印时如何度量字符串 在打印时如何度量字符串 <Question> When programming printing code, how to measure string? <Answers> Yang Ning: You can't use Graphics.MeasureString Function, and must use typographic StringFormat object. Reason is: when printing string size is resolution-dependent. And if you using MeasureString function or use defaultgraphic StringFormat object, it is resolution-independent, the result will be smaller than the true one. Below is sample code: Public Shared Function GetTextSize(ByVal g As Graphics, _ ByVal text As String, _ ByVal textFont As Font) As SizeF If text.Length = 0 Then Return New SizeF(0, 0) Dim s As StringFormat = StringFormat.GenericTypographic s.FormatFlags = StringFormatFlags.MeasureTrailingSpaces Dim textRect As RectangleF Dim characterRanges As CharacterRange() = {New CharacterRange(0, text.Length)} s.SetMeasurableCharacterRanges(characterRanges) textRect = g.MeasureCharacterRanges(text, textFont, New RectangleF(0, 0, 4000, 4000), s)(0).GetBounds(g) Return New SizeF(textRect.Right, textRect.Bottom) End Function BTW: We found a bug when we use code like above. The bug is FlexGrid's column caption will display disorder. So We use following code instead Public Shared Function GetTextSize(ByVal g As Graphics, _ ByVal text As String, _ ByVal textFont As Font) As SizeF If text.Length = 0 Then Return New SizeF(0, 0) Dim s As StringFormat = StringFormat.GenericTypographic Dim oldFlags As StringFormatFlags oldFlags = s.FormatFlags s.FormatFlags = StringFormatFlags.MeasureTrailingSpaces Try Dim textRect As RectangleF Dim characterRanges As CharacterRange() = {New CharacterRange(0, text.Length)} s.SetMeasurableCharacterRanges(characterRanges) textRect = g.MeasureCharacterRanges(text, textFont, New RectangleF(0, 0, 4000, 4000), s)(0).GetBounds(g) Return New SizeF(textRect.Right, textRect.Bottom) Finally StringFormat.GenericTypographic.FormatFlags = oldFlags End Try End Function
|
| 文章录入:bolang 责任编辑:bolang |
|
上一篇文章: WEB图片高清晰浏览同打印 下一篇文章: [票据打印]打印走纸控制 |
| 【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |