找回密码
 立即注册

QQ登录

只需一步,快速开始

ZenosZeng 讲师达人认证 悬赏达人认证
超级版主   /  发表于:2012-10-31 19:00  /   查看:4972  /  回复:0
在多数系统中,我们经常会判断在指定的显示范围内是否能够显示下指定的文本长度,此时我们会用到GDI+提供的MeasureString方法。

在 TX Text Control 中测量文本长度的方法稍微有所不同,TX Text Control 有自己的的文本渲染机制,通过这种所见即所得的渲染机制才使得TX的文本编辑功能如此的优秀。
在 TX Text Control X8 版本中,WinForms和WPF系统中可以使用一个不可见的ServerTextControl组件,通过该组件可以得到文本的实际长度,下面的代码简单演示了该功能:
  1. public int MeasureTextControlString(Selection Selection, string FormattingPrinter)
  2. {
  3.      int iLength;
  4.       using (ServerTextControl tx = new ServerTextControl())
  5.      {
  6.          tx.Create();
  7.          tx.PageSize.Width = 10000;
  8.          tx.FormattingPrinter = FormattingPrinter;
  9.           foreach (PropertyInfo property in Selection.GetType().GetProperties())
  10.          {
  11.              if (property.GetValue(Selection, null).ToString() == "")
  12.                 continue;

  13.               property.SetValue(tx.Selection,
  14.                   property.GetValue(Selection, null), null);
  15.          }

  16.           tx.SelectAll();

  17.           return iLength = tx.TextChars[tx.Selection.Length].Bounds.Right -
  18.                  tx.TextChars[tx.Selection.Start + 1].Bounds.Left;
  19.      }
  20. }
复制代码


下面的代码演示了如何使用以上方法测量出文本的长度:
  1. Selection newSelection = new Selection();
  2. newSelection.Text = "Hello dWorld";
  3. newSelection.FontSize = 600;
  4. newSelection.FontName = "Wingdings";

  5. int length = MeasureTextControlString(newSelection, textControl1.FormattingPrinter);
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部