文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
 websphere   .net framework
您现在的位置: IT文谷 >> 开发平台 >> .net framework >> VB.NET >> 文章正文
在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?
在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?
在打印时如何度量字符串?

在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?
   <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

在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?


  • 上一篇文章:

  • 下一篇文章:
  • 进入论坛讨论

    相关文章
    基于.NET的多线程编程入门
    Microsoft .NET 框架资源基础
    VS2005常用插件搜罗
    .net中用GDI+提高gif图片保存画质
    VS.NET:通过Web服务瞄准电子商务
    对象设计中创建VS使用
    安装/初始化/个性化DotNetNuke
    介绍dotnet原动力(DNN)
    GDI+编程10个基本技巧
    DTE .NET工程的自动化编译
    .Net中如何操作IIS的原理分析
    VS.NETRC5无法创建Add-IN工程
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 在本网BBS上发表言论者,文责自负。
    ③ 如您因版权等问题需要与本网联络,请在30日内联系 。
    在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?
    在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?2006-7-15在打印时如何度量字符串?

    .net framework专题
    人气排行
  • 此栏目下没有文章
  • 最近更新
    普通文章技术介绍 新数据网格简介
    普通文章.NET 开发人员该下载的十个必备
    普通文章深入剖析Asp.net资源文件
    普通文章通过事例学习.net的WebForms技
    普通文章ASP.NET Tips1---合并多个字段
    普通文章微软明年1月份出台.NET专业开发
    普通文章微软下月底前完成Vista大部分功
    普通文章揭开微软 .NET 认证考试的面纱
    普通文章Visual Studio:Microsoft开发工
    普通文章Visual Studio:Microsoft开发工
    全站热点       
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷社区
    Tel: 0577-65690019    E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2006 wengu.org 文谷 All Rights Reserved
    浙ICP备05000327号