回复 1楼wanghongtao的帖子
wanghongtao 你好,
测试代码如下:
- public partial class Report1 : DataDynamics.ActiveReports.ActiveReport
- {
- public static System.Drawing.Bitmap _image =new Bitmap(2,2);
- public static System.Drawing.Graphics _hideGraphics = Graphics.FromImage(_image);
- public Report1()
- {
- //
- // Required for Windows Form Designer support
- //
- InitializeComponent();
- }
- public static TextBox GetScaleText(TextBox reportText, float controlWidth, float intervalFontSize, float minFontSize, PrintStringMode printStringMode)
- {
- TextBox _with1 = reportText;
- if (_with1.Text == null || _with1.Text.Length == 0)
- {
- return reportText;
- }
- int length = _with1.Text.Length;
- while (GetTextWidth(_with1) > controlWidth)
- {
- switch (printStringMode)
- {
- case PrintStringMode.JustPrint:
- break; // TODO: might not be correct. Was : Exit While
- case PrintStringMode.CutString:
- if (_with1.Text.Length <= 0)
- {
- break; // TODO: might not be correct. Was : Exit While
- }
- _with1.Text = _with1.Text.Substring(0, _with1.Text.Length - 1);
- break;
- case PrintStringMode.ReduceFont:
- if ((_with1.Font.Size - intervalFontSize) <= 0)
- {
- break; // TODO: might not be correct. Was : Exit While
- }
- _with1.Font = new Font(_with1.Font.Name, _with1.Font.Size - intervalFontSize);
- break;
- case PrintStringMode.DefaultValue:
- case PrintStringMode.ReduceFontThenCut:
- if (_with1.Font.Size <= minFontSize)
- {
- _with1.Font = new Font(_with1.Font.Name, minFontSize);
- if (_with1.Text.Length <= 0)
- {
- break; // TODO: might not be correct. Was : Exit While
- }
- _with1.Text = _with1.Text.Substring(0, _with1.Text.Length - 1);
- }
- else
- {
- _with1.Font = new Font(_with1.Font.Name, _with1.Font.Size - intervalFontSize);
- }
- break;
- }
- }
- if (_with1.Text.Length > 0)
- {
- int i = _with1.Text.Length;
- int k = 1;
- if (_with1.Text.Length < length)
- {
- if (i > 255 || i < 0)
- {
- }
- else
- {
- _with1.Text = _with1.Text.Substring(0, _with1.Text.Length - 2);
- }
- }
- }
- return reportText;
- }
- private static float GetTextWidth( TextBox reportText)
- {
- return _hideGraphics.MeasureString(reportText.Text, reportText.Font).Width / 100;
- }
- private void detail_Format(object sender, EventArgs e)
- {
- this.textBox1.Text = "jfkdsafjdkasfldjskafldjsfkldsjl";
- GetScaleText(this.textBox1, this.textBox1.Width, 0.5F, 6, PrintStringMode.DefaultValue);
- }
- }
- public enum PrintStringMode
- {
- JustPrint=0,
- ReduceFontThenCut=1,
- DefaultValue=2,
- ReduceFont=3,
- CutString=4
- }
复制代码 |