网站公告列表

  没有公告

加入收藏
设为首页
在线投稿

您现在的位置: IT知识网 >> IT知识 >> 维修维护 >> 打印机维护维修知识 >> 文章正文

 

  在打印时如何度量字符串           

在打印时如何度量字符串
作者:佚名 文章来源:不详 点击数: 更新时间: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下对
    硬盘常常无缘无故就“嘟
    Epson 彩色喷墨打印机打
    为什么office打印出文件
    写给菜鸟详谈asp木马在打
    [维修]EPSON1500K打印机
    喷墨打印机五种故障现象
    如何打印画质最佳的照片
    给激光打印机硒鼓加粉
     
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    Copyright© ITZS.NET All Rights Reserved
    QQ:272895858   ICP备案编号:吉ICP备07000044号
    IT知识网 站长:博浪