找回密码
 立即注册

QQ登录

只需一步,快速开始

wanghongtao

高级会员

7

主题

46

帖子

1117

积分

高级会员

积分
1117

活字格认证

[已处理] TextBox的值缩放

wanghongtao
高级会员   /  发表于:2012-7-9 16:35  /   查看:6489  /  回复:5
在报表开发中,控件TextBox的值,固定好宽和高,
如果值太长,能不有自动缩小字,在固定的宽和高之内显示出来。
这种有可能做到吗?

5 个回复

倒序浏览
iceman
社区贡献组   /  发表于:2012-7-9 17:58:00
沙发
回复 1楼wanghongtao的帖子

wanghongtao 你好,
测试代码如下:

  1. public partial class Report1 : DataDynamics.ActiveReports.ActiveReport
  2.     {
  3.         public static System.Drawing.Bitmap _image =new Bitmap(2,2);

  4.         public static System.Drawing.Graphics _hideGraphics = Graphics.FromImage(_image);

  5.         public Report1()
  6.         {
  7.             //
  8.             // Required for Windows Form Designer support
  9.             //
  10.             InitializeComponent();
  11.         }

  12.         public static TextBox GetScaleText(TextBox reportText, float controlWidth, float intervalFontSize, float minFontSize, PrintStringMode printStringMode)
  13.         {

  14.             TextBox _with1 = reportText;

  15.             if (_with1.Text == null || _with1.Text.Length == 0)
  16.             {
  17.                 return reportText;
  18.             }


  19.             int length = _with1.Text.Length;

  20.             while (GetTextWidth(_with1) > controlWidth)
  21.             {
  22.                 switch (printStringMode)
  23.                 {
  24.                     case PrintStringMode.JustPrint:

  25.                         break; // TODO: might not be correct. Was : Exit While

  26.                     case PrintStringMode.CutString:

  27.                         if (_with1.Text.Length <= 0)
  28.                         {
  29.                             break; // TODO: might not be correct. Was : Exit While
  30.                         }
  31.                         _with1.Text = _with1.Text.Substring(0, _with1.Text.Length - 1);

  32.                         break;
  33.                     case PrintStringMode.ReduceFont:

  34.                         if ((_with1.Font.Size - intervalFontSize) <= 0)
  35.                         {
  36.                             break; // TODO: might not be correct. Was : Exit While
  37.                         }
  38.                         _with1.Font = new Font(_with1.Font.Name, _with1.Font.Size - intervalFontSize);

  39.                         break;
  40.                     case PrintStringMode.DefaultValue:
  41.                     case PrintStringMode.ReduceFontThenCut:

  42.                         if (_with1.Font.Size <= minFontSize)
  43.                         {
  44.                             _with1.Font = new Font(_with1.Font.Name, minFontSize);

  45.                             if (_with1.Text.Length <= 0)
  46.                             {
  47.                                 break; // TODO: might not be correct. Was : Exit While
  48.                             }
  49.                             _with1.Text = _with1.Text.Substring(0, _with1.Text.Length - 1);
  50.                         }
  51.                         else
  52.                         {

  53.                             _with1.Font = new Font(_with1.Font.Name, _with1.Font.Size - intervalFontSize);
  54.                         }

  55.                         break;
  56.                 }

  57.             }


  58.             if (_with1.Text.Length > 0)
  59.             {
  60.                 int i = _with1.Text.Length;
  61.                 int k = 1;
  62.                 if (_with1.Text.Length < length)
  63.                 {
  64.                     if (i > 255 || i < 0)
  65.                     {
  66.                     }
  67.                     else
  68.                     {
  69.                         _with1.Text = _with1.Text.Substring(0, _with1.Text.Length - 2);
  70.                     }
  71.                 }
  72.             }
  73.             return reportText;
  74.         }

  75.         private static float GetTextWidth( TextBox reportText)
  76.         {
  77.             return _hideGraphics.MeasureString(reportText.Text, reportText.Font).Width / 100;
  78.         }

  79.         private void detail_Format(object sender, EventArgs e)
  80.         {
  81.             this.textBox1.Text = "jfkdsafjdkasfldjskafldjsfkldsjl";
  82.             GetScaleText(this.textBox1, this.textBox1.Width, 0.5F, 6, PrintStringMode.DefaultValue);
  83.         }
  84.     }
  85.     public enum PrintStringMode
  86.     {
  87.         JustPrint=0,
  88.         ReduceFontThenCut=1,
  89.         DefaultValue=2,
  90.         ReduceFont=3,
  91.         CutString=4
  92.     }
复制代码
回复 使用道具 举报
wanghongtao
高级会员   /  发表于:2012-7-9 18:01:00
板凳
好,谢谢。我试一下
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2012-7-9 18:08:00
地板
回复 使用道具 举报
sucydef
银牌会员   /  发表于:2012-7-12 19:00:00
5#
这个试过,可以啦
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2012-7-16 10:58:00
6#
回复 5楼sucydef的帖子

:share:
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部